forked from phoopies/DesignOptimizationCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createGeometryDesignProblem.py
87 lines (65 loc) · 3.4 KB
/
createGeometryDesignProblem.py
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
from desdeo_mcdm.utilities.solvers import solve_pareto_front_representation
from desdeo_emo.EAs import NSGAIII
from modules.utils import save
from modules.GeometryDesign.problem import create_problem
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore") # ignore warnings :)
# Creating geometry design problem : tent like buildings
# Which objectives do you wish to optimize
# surface area, volume, min height and floor area
obj = np.array([
True, False, True, False, # Optimizing Surface area and min height and ignoring others,
])
# ideal and nadir in respective order
# ideal = 0, 1, 1, 1
# nadir = 5, 0, 0, 0
# Set constraint for objectives, [lower, upper]
# If no constraint then set it to None
# Each row represents a objective function in the same order as in obj_gd
# Notice that breaking constraints will result in a penalty and therefore we might get results that break the constraints
constraints = np.array([
[0.2, None], # Surface area > 0.2
[.5, .8], # .5 < volume < .8. Even though we're not optimizing volume, we can set a constraint on it
[.4, None], # min height > .4
[None, 0.6], # floor area < .6
])
# How many 3d points should the hull be formed of
# more points => More complex problem : longer execution times
# Less points => More likely to fail in constructing the hull
variable_count = 15 # Around 15 - 25 seems to be good enough
# To create the problem we can call the gd_create method with the parameters defined earlier
# the pfront argument should be set to True if using the solve_pareto_front_representation method as it doesn't
# take account minimizing/maximizing. For everything else we can set it to False
# The method returns a MOProblem and a scalarmethod instance which can be passed to different Desdeo objects
problem, method = create_problem(variable_count , obj, constraints, pfront = True)
# Two methods to solve the problem are shown below. Do not use them both at the same time!
# Use one, and comment out the other!
# Example on solving the pareto front : This might take some time so feel free to comment this out (lines 57 and 60).
# We will use the solve_pareto_front_representation method but one can change this to something else.
# The method takes the problem instance and a step size array
# The method will create reference points from nadir to ideal with these step sizes
# in this case : ref points = [[5,0,0,0], [4.5, 0, 0, 0], [4, 0, 0, 0] ... [5, 0.2, 0, 0] ... [0, 1, 1, 1]]
# large step sizes => less solutions but faster calculation
#step_sizes = np.array([.5, .2, .2, .2])[obj]
# The method returns the decision vectors and corresponding objective vectors
#var, obj = solve_pareto_front_representation(problem, step_sizes, solver_method= method)
# Example on solving the pareto front using NSGA-III
evolver = NSGAIII(
problem,
population_size=100,
n_iterations=10,
n_gen_per_iter=100)
i=0
while evolver.continue_evolution():
evolver.iterate()
print(f"Iteration number {i}")
i = i+1
var, obj = evolver.end()
# save the solution if you wish, make sure to change the name to not accidentally overwrite an existing solution.
# Saved solutions can be used later to visualize it
# The solution will be saved to modules/DataAndVisualization/'name'
save("gdExample", obj, var, problem.nadir, problem.ideal)
pd.DataFrame(obj).to_csv("objective_vectors_3.csv")
pd.DataFrame(var).to_csv("decision_vectors_3.csv")