-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInversionMutation.java
More file actions
121 lines (107 loc) · 3.64 KB
/
Copy pathInversionMutation.java
File metadata and controls
121 lines (107 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.aim.project.ssp.heuristics.mutation;
import AbstractClasses.ProblemDomain;
import com.aim.project.ssp.heuristics.HeuristicOperators;
import com.aim.project.ssp.interfaces.HeuristicInterface;
import com.aim.project.ssp.interfaces.SSPSolutionInterface;
import com.aim.project.ssp.interfaces.SolutionRepresentationInterface;
import java.util.Random;
/**
* Inversion Mutation heuristic.
* This mutation selects a random segment of the tour and reverses it.
*
* <p>The number of inversions is determined by the intensity of mutation (iom).</p>
*
* @author Sushant Nepal
* @since 17/03/25
*/
public class InversionMutation extends HeuristicOperators implements HeuristicInterface {
/**
* Constructor for the InversionMutation heuristic.
*
* @param random Random instance for stochastic behaviour.
*/
public InversionMutation(Random random) {
super(random);
}
/**
* Applies the inversion mutation on the given solution.
* Reverses a number of randomly selected segments in the tour.
*
* @param solution The solution to be mutated.
* @param dDepthOfSearch Unused parameter for this mutation.
* @param dIntensityOfMutation Controls the number of inversions.
* @return The updated objective function value after mutation.
*/
@Override
public double apply(SSPSolutionInterface solution, double dDepthOfSearch, double dIntensityOfMutation) {
// Retrieve the tour and its length
SolutionRepresentationInterface rep = solution.getSolutionRepresentation();
int[] tour = rep.getSolutionRepresentation();
int n = tour.length;
// Determine how many inversion operations to perform
int numInversions = getInversionCount(dIntensityOfMutation);
for (int i = 0; i < numInversions; i++) {
int startIndex, endIndex;
// Generate a valid segment where startIndex < endIndex
startIndex = m_oRandom.nextInt(n);
endIndex = m_oRandom.nextInt(startIndex, n); // exclusive upper bound
// Reverse the segment in place
reverseSegment(tour, startIndex, endIndex);
}
// Update the solution's representation and calculate its new cost
rep.setSolutionRepresentation(tour);
double newCost = m_oObjectiveFunction.getObjectiveFunctionValue(rep);
solution.setObjectiveFunctionValue(newCost);
return newCost;
}
/**
* Determines the number of inversions to perform based on intensity of mutation (IOM).
*
* @param iom the intensity of mutation value [0, 1]
* @return the number of inversion operations
*/
private int getInversionCount(double iom) {
if (iom == 1) return 32;
else if (iom >= 0.8) return 16;
else if (iom >= 0.6) return 8;
else if (iom >= 0.4) return 4;
else if (iom >= 0.2) return 2;
else return 1;
}
/**
* This heuristic is not a crossover operator.
*
* @return false
*/
@Override
public boolean isCrossover() {
return false;
}
/**
* This heuristic uses intensity of mutation.
*
* @return true
*/
@Override
public boolean usesIntensityOfMutation() {
return true;
}
/**
* This heuristic does not use depth of search.
*
* @return false
*/
@Override
public boolean usesDepthOfSearch() {
return false;
}
/**
* Returns the type of heuristic: MUTATION.
*
* @return HeuristicType.MUTATION
*/
@Override
public ProblemDomain.HeuristicType getType() {
return ProblemDomain.HeuristicType.MUTATION;
}
}