Skip to content

Commit

Permalink
Tests fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicl-nno committed Jul 25, 2022
1 parent 06bbff8 commit 1acbae2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
56 changes: 28 additions & 28 deletions gefest/core/opt/operators/crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,36 @@ def crossover_worker(args):
# All polygons are shuffling between Structures in random way
s1, s2 = shuffle_structures(s1, s2)

# Choosing crossover point randomly
crossover_point = random.randint(1, min(len(s1.polygons), len(s2.polygons)))

# Crossover conversion
part_1 = s1.polygons[:crossover_point]
if not isinstance(part_1, list):
part_1 = [part_1]
part_2 = s2.polygons[crossover_point:]
if not isinstance(part_2, list):
part_2 = [part_2]

result = copy.deepcopy(part_1)
result.extend(copy.deepcopy(part_2))

new_structure.polygons = result

# Postprocessing for new structure
# Choosing crossover point randomly
crossover_point = random.randint(1, min(len(s1.polygons), len(s2.polygons)))

# Crossover conversion
part_1 = s1.polygons[:crossover_point]
if not isinstance(part_1, list):
part_1 = [part_1]
part_2 = s2.polygons[crossover_point:]
if not isinstance(part_2, list):
part_2 = [part_2]

result = copy.deepcopy(part_1)
result.extend(copy.deepcopy(part_2))

new_structure.polygons = result

# Postprocessing for new structure
new_structure = postprocess(new_structure, domain)
constraints = check_constraints(structure=new_structure, domain=domain)
max_attempts = 3 # Number of postprocessing attempts
while not constraints:
new_structure = postprocess(new_structure, domain)
constraints = check_constraints(structure=new_structure, domain=domain)
max_attempts = 3 # Number of postprocessing attempts
while not constraints:
new_structure = postprocess(new_structure, domain)
constraints = check_constraints(structure=new_structure, domain=domain)
max_attempts -= 1
if max_attempts == 0:
# If the number of attempts is over,
# the transformation is considered unsuccessful
# and one of the structures is returned
return s1
return new_structure
max_attempts -= 1
if max_attempts == 0:
# If the number of attempts is over,
# the transformation is considered unsuccessful
# and one of the structures is returned
return s1
return new_structure
except Exception as ex:
print(ex)
return s1
Expand Down
2 changes: 1 addition & 1 deletion gefest/core/opt/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from gefest.core.opt.setup import Setup


def optimize(task_setup: Setup, objective_function: Callable, max_gens, pop_size):
def optimize(task_setup: Setup, objective_function: Callable, max_gens, pop_size) -> Result:
operators = default_operators()

params = GA.Params(max_gens=max_gens, pop_size=pop_size,
Expand Down
10 changes: 8 additions & 2 deletions test/test_circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

from gefest.core.geometry.geometry_2d import Geometry2D
from gefest.core.opt.optimize import optimize
from gefest.core.opt.result import Result
from gefest.core.opt.setup import Setup
from gefest.core.structure.domain import Domain
from gefest.core.structure.structure import Structure


"""
Test for synthetic case with isoperimetric task
"""


def test_fast():
geometry = Geometry2D(is_closed=True)

def area_length_ratio(struct: Structure):
if len(struct.polygons) == 0:
return None

poly = struct.polygons[0]
area = geometry.get_square(poly)
length = geometry.get_length(poly)
Expand All @@ -40,9 +44,11 @@ def area_length_ratio(struct: Structure):

task_setup = Setup(domain=domain)

optimized_structure = optimize(task_setup=task_setup,
optimization_result = optimize(task_setup=task_setup,
objective_function=area_length_ratio,
pop_size=20,
max_gens=1)

optimized_structure = optimization_result.best_structure
assert type(optimized_structure) == Structure
assert type(optimization_result) == Result
5 changes: 2 additions & 3 deletions test/test_crossover.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import pytest
from gefest.core.opt.operators.crossover import crossover
from gefest.core.structure.domain import Domain
from gefest.core.structure.point import Point
from gefest.core.structure.polygon import Polygon
from gefest.core.structure.structure import Structure
from gefest.core.structure.domain import Domain


domain = Domain()
geometry = domain.geometry
Expand All @@ -26,6 +24,7 @@ def test_crossover_passed():
expected_square = geometry.get_square(structure_large.polygons[0])\
+ geometry.get_square(structure_small.polygons[0])

condition = False
for i in range(100):
new_structure = crossover(structure_large, structure_small, domain)
if len(new_structure.polygons) == 2:
Expand Down
7 changes: 4 additions & 3 deletions test/test_optimization_case.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
import numpy as np

from gefest.core.geometry.geometry_2d import Geometry2D
Expand Down Expand Up @@ -53,10 +52,12 @@ def multi_loss(struct: Structure):


def test_optimization():

optimized_structure = optimize(task_setup=task_setup,
optimization_result = optimize(task_setup=task_setup,
objective_function=multi_loss,
pop_size=30,
max_gens=3)
optimized_structure = optimization_result.best_structure

assert optimization_result.fitness > 0
assert all([isinstance(optimized_structure, Structure),
len(optimized_structure.polygons) > 0])

0 comments on commit 1acbae2

Please sign in to comment.