Skip to content

Commit

Permalink
Adapt CXCrossover and ScrambleMutation to TSP.
Browse files Browse the repository at this point in the history
Add TSP to list of problems.
  • Loading branch information
YevheniiSemendiak committed Jan 17, 2020
1 parent a0035e8 commit 2837195
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
9 changes: 5 additions & 4 deletions jmetal/operator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from .crossover import NullCrossover, SBXCrossover, SPXCrossover, DifferentialEvolutionCrossover
from .crossover import NullCrossover, SBXCrossover, SPXCrossover, DifferentialEvolutionCrossover, \
PMXCrossover, CXCrossover
from .mutation import NullMutation, BitFlipMutation, PolynomialMutation, IntegerPolynomialMutation, UniformMutation, \
SimpleRandomMutation
SimpleRandomMutation, ScrambleMutation, PermutationSwapMutation
from .selection import BestSolutionSelection, BinaryTournamentSelection, BinaryTournament2Selection, \
RandomSolutionSelection, NaryRandomSolutionSelection, RankingAndCrowdingDistanceSelection

__all__ = [
'NullCrossover', 'SBXCrossover', 'SPXCrossover', 'DifferentialEvolutionCrossover',
'NullCrossover', 'SBXCrossover', 'SPXCrossover', 'DifferentialEvolutionCrossover', 'PMXCrossover', 'CXCrossover',
'NullMutation', 'BitFlipMutation', 'PolynomialMutation', 'IntegerPolynomialMutation', 'UniformMutation',
'SimpleRandomMutation',
'SimpleRandomMutation', 'ScrambleMutation', 'PermutationSwapMutation',
'BestSolutionSelection', 'BinaryTournamentSelection', 'BinaryTournament2Selection', 'RandomSolutionSelection',
'NaryRandomSolutionSelection', 'RankingAndCrowdingDistanceSelection'
]
25 changes: 12 additions & 13 deletions jmetal/operator/crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,21 @@ def execute(self, parents: List[PermutationSolution]) -> List[PermutationSolutio
rand = random.random()

if rand <= self.probability:
for i in range(parents[0].number_of_variables):
idx = random.randint(0, len(parents[0].variables[i]) - 1)
curr_idx = idx
cycle = []
idx = random.randint(0, len(parents[0].variables) - 1)
curr_idx = idx
cycle = []

while True:
cycle.append(curr_idx)
curr_idx = parents[0].variables[i].index(parents[1].variables[i][curr_idx])
while True:
cycle.append(curr_idx)
curr_idx = parents[0].variables.index(parents[1].variables[curr_idx])

if curr_idx == idx:
break
if curr_idx == idx:
break

for j in range(len(parents[0].variables[i])):
if j in cycle:
offspring[0].variables[i][j] = parents[0].variables[i][j]
offspring[1].variables[i][j] = parents[0].variables[i][j]
for j in range(len(parents[0].variables)):
if j in cycle:
offspring[0].variables[j] = parents[0].variables[j]
offspring[1].variables[j] = parents[0].variables[j]

return offspring

Expand Down
25 changes: 12 additions & 13 deletions jmetal/operator/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,23 +230,22 @@ def get_name(self):
class ScrambleMutation(Mutation[PermutationSolution]):

def execute(self, solution: PermutationSolution) -> PermutationSolution:
for i in range(solution.number_of_variables):
rand = random.random()
rand = random.random()

if rand <= self.probability:
point1 = random.randint(0, len(solution.variables[i]))
point2 = random.randint(0, len(solution.variables[i]) - 1)
if rand <= self.probability:
point1 = random.randint(0, len(solution.variables))
point2 = random.randint(0, len(solution.variables) - 1)

if point2 >= point1:
point2 += 1
else:
point1, point2 = point2, point1
if point2 >= point1:
point2 += 1
else:
point1, point2 = point2, point1

if point2 - point1 >= 20:
point2 = point1 + 20
if point2 - point1 >= 20:
point2 = point1 + 20

values = solution.variables[i][point1:point2]
solution.variables[i][point1:point2] = random.sample(values, len(values))
values = solution.variables[point1:point2]
solution.variables[point1:point2] = random.sample(values, len(values))

return solution

Expand Down
4 changes: 3 additions & 1 deletion jmetal/problem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
from .multiobjective.unconstrained import Kursawe, Fonseca, Schaffer, Viennet2
from .multiobjective.zdt import ZDT1, ZDT2, ZDT3, ZDT4, ZDT6
from .singleobjective.unconstrained import OneMax, Sphere
from .singleobjective.tsp import TSP

__all__ = [
'Srinivas', 'Tanaka',
'Kursawe', 'Fonseca', 'Schaffer', 'Viennet2',
'DTLZ1', 'DTLZ2',
'ZDT1', 'ZDT2', 'ZDT3', 'ZDT4', 'ZDT6',
'LZ09_F2',
'OneMax', 'Sphere'
'OneMax', 'Sphere',
'TSP'
]

0 comments on commit 2837195

Please sign in to comment.