Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_solution_comparison_maximization(self):
sol1 = Individual(SOLUTIONS[0][0], SOLUTIONS[0][1])
sol2 = Individual(SOLUTIONS[1][0], SOLUTIONS[1][1])
sol3 = Individual(SOLUTIONS[2][0], SOLUTIONS[2][1])
# test ordering
assert sol1.__cmp__(sol2) == -1
assert sol1.__cmp__(sol1) == 0
assert sol1.__cmp__(sol3) == 1
assert sol1 < sol2
assert sol1 == sol1
assert sol1 > sol3
# test gt and lt
assert sol1.__lt__(sol2)
assert sol1.__gt__(sol3)
assert not sol1.__lt__(sol1)
def test_solution_comparison_maximization(self):
sol1 = Individual(SOLUTIONS[0][0], SOLUTIONS[0][1])
sol2 = Individual(SOLUTIONS[1][0], SOLUTIONS[1][1])
sol3 = Individual(SOLUTIONS[2][0], SOLUTIONS[2][1])
# test ordering
assert sol1.__cmp__(sol2) == -1
assert sol1.__cmp__(sol1) == 0
assert sol1.__cmp__(sol3) == 1
assert sol1 < sol2
assert sol1 == sol1
assert sol1 > sol3
# test gt and lt
assert sol1.__lt__(sol2)
assert sol1.__gt__(sol3)
assert not sol1.__lt__(sol1)
assert not sol1.__gt__(sol1)
def test_solution_comparison_maximization(self):
sol1 = Individual(SOLUTIONS[0][0], SOLUTIONS[0][1])
sol2 = Individual(SOLUTIONS[1][0], SOLUTIONS[1][1])
sol3 = Individual(SOLUTIONS[2][0], SOLUTIONS[2][1])
# test ordering
assert sol1.__cmp__(sol2) == -1
assert sol1.__cmp__(sol1) == 0
assert sol1.__cmp__(sol3) == 1
assert sol1 < sol2
assert sol1 == sol1
assert sol1 > sol3
# test gt and lt
assert sol1.__lt__(sol2)
assert sol1.__gt__(sol3)
assert not sol1.__lt__(sol1)
assert not sol1.__gt__(sol1)
assert not sol2.__lt__(sol1)
def test_solution_comparison_minimization(self):
sol1 = Individual(SOLUTIONS[0][0], SOLUTIONS[0][1], maximize=False)
sol2 = Individual(SOLUTIONS[1][0], SOLUTIONS[1][1], maximize=False)
sol3 = Individual(SOLUTIONS[2][0], SOLUTIONS[2][1], maximize=False)
# test ordering
assert sol1.__cmp__(sol2) == -1
assert sol1.__cmp__(sol1) == 0
assert sol1.__cmp__(sol3) == -1
assert sol3.__cmp__(sol1) == 1
assert sol1 < sol2
assert sol1 == sol1
assert sol1 < sol3
# test gt and lt
assert sol1.__lt__(sol2)
assert sol1.__lt__(sol3)
assert not sol1.__gt__(sol1)
assert not sol1.__lt__(sol1)
def test_solution_comparison_minimization(self):
sol1 = Individual(SOLUTIONS[0][0], SOLUTIONS[0][1], maximize=False)
sol2 = Individual(SOLUTIONS[1][0], SOLUTIONS[1][1], maximize=False)
sol3 = Individual(SOLUTIONS[2][0], SOLUTIONS[2][1], maximize=False)
# test ordering
assert sol1.__cmp__(sol2) == -1
assert sol1.__cmp__(sol1) == 0
assert sol1.__cmp__(sol3) == -1
assert sol3.__cmp__(sol1) == 1
assert sol1 < sol2
assert sol1 == sol1
assert sol1 < sol3
# test gt and lt
assert sol1.__lt__(sol2)
assert sol1.__lt__(sol3)
assert not sol1.__gt__(sol1)
heuristic_method=None,
simulation_method=fba,
simulation_kwargs=None,
solutions=solutions,
objective_function=None,
target_type="reaction",
decoder=decoder,
seed=SEED,
simplify=False)
assert result.target_type == "reaction"
individuals = []
for row in result:
encoded = set(representation.index(v) for v in row[0])
individual = Individual(encoded, row[1])
assert individual not in individuals, "%s is repeated on result"
individuals.append(individual)
assert individual in solutions.archive
assert solutions.archive.count(individual) == 1, "%s is unique in archive" % individual
def __init__(self, candidate, fitness, maximize=True, birthdate=time.time()):
super(Individual, self).__init__(set(candidate), maximize)
self.fitness = fitness
self.birthdate = birthdate
def add(self, candidate, fitness, birthdate, maximize, max_archive_size):
if self.worst_fitness is None:
self.worst_fitness = fitness
if (maximize and fitness >= self.worst_fitness) or (not maximize and fitness <= self.worst_fitness):
candidate = Individual(candidate, fitness, maximize, birthdate)
add = True
for c in self.archive:
if c == candidate:
add = False
elif c.improves(candidate) and candidate.fitness == c.fitness:
add = False
elif candidate.improves(c) and candidate.fitness == c.fitness:
self.archive.remove(c)
if add:
insort(self.archive, candidate)
while self.length() > max_archive_size:
self.archive.pop()
self.worst_fitness = self.archive[len(self.archive) - 1].fitness
def improves(self, other):
assert isinstance(other, Individual)
if self.maximize:
return self.issubset(other) and len(self.symmetric_difference(other)) > 0 and self.fitness >= other.fitness
else:
return self.issubset(other) and len(self.symmetric_difference(other)) > 0 and self.fitness <= other.fitness
def simplify(self, individual):
new_individual = Individual(individual.candidate, individual.fitness, individual.maximize,
birthdate=individual.birthdate)
for target in individual.candidate:
new_individual.candidate.remove(target)
new_fitness = self._evaluator.evaluate_individual(tuple(new_individual))
if isinstance(new_fitness, inspyred.ec.emo.Pareto):
if new_fitness < individual.fitness:
new_individual.candidate.add(target)
else:
if new_fitness < individual.fitness or numpy.isnan(new_fitness):
new_individual.candidate.add(target)
return new_individual