-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First implementation of split * Add tests * Update test * Add objectives to init * Add evaluation function to objectives * Add objective class support
- Loading branch information
Showing
7 changed files
with
211 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import numpy as np | ||
|
||
class Objective(): | ||
def __init__(self, objective_functions, num_objectives = None) -> None: | ||
self.objective_functions = objective_functions | ||
if num_objectives is None: | ||
self.num_objectives = len(self.objective_functions) | ||
else: | ||
self.num_objectives = num_objectives | ||
pass | ||
|
||
def evaluate(self, items): | ||
return np.array([objective_function([item for item in items]) for objective_function in self.objective_functions]) | ||
|
||
def type(self): | ||
return self.__class__.__name__ | ||
|
||
class ElementWiseObjective(Objective): | ||
def __init__(self, objective_functions, num_objectives=None) -> None: | ||
super().__init__(objective_functions, num_objectives) | ||
|
||
def evaluate(self, items): | ||
return np.array([[obj_func(item) for item in items] for obj_func in self.objective_functions]) | ||
|
||
class BatchObjective(Objective): | ||
def __init__(self, objective_functions, num_objectives=None) -> None: | ||
super().__init__(objective_functions, num_objectives) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import optimizer | ||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
import matplotlib.animation as animation | ||
|
||
|
||
num_agents = 100 | ||
num_iterations = 200 | ||
num_params = 30 | ||
|
||
lb = [0] * num_params | ||
ub = [1] * num_params | ||
|
||
def zdt1_objective1(x): | ||
return x[0] | ||
|
||
def zdt1_objective2(x): | ||
f1 = x[0] | ||
g = 1 + 9.0 / (len(x)-1) * sum(x[1:]) | ||
h = 1.0 - np.sqrt(f1 / g) | ||
f2 = g * h | ||
return f2 | ||
|
||
optimizer.FileManager.working_dir="tmp/zdt1/" | ||
optimizer.FileManager.loading_enabled = False | ||
optimizer.FileManager.saving_enabled = False | ||
|
||
objective = optimizer.ElementWiseObjective([zdt1_objective1, zdt1_objective2]) | ||
|
||
pso = optimizer.MOPSO(objective=objective,lower_bounds=lb, upper_bounds=ub, | ||
num_particles=num_agents, num_iterations=num_iterations, | ||
inertia_weight=0.6, cognitive_coefficient=1, social_coefficient=2, | ||
max_iter_no_improv=None) | ||
|
||
# run the optimization algorithm | ||
pso.optimize() | ||
|
||
fig, ax = plt.subplots() | ||
|
||
pareto_front = pso.get_current_pareto_front() | ||
n_pareto_points = len(pareto_front) | ||
pareto_x = [particle.fitness[0] for particle in pareto_front] | ||
pareto_y = [particle.fitness[1] for particle in pareto_front] | ||
real_x = (np.linspace(0, 1, n_pareto_points)) | ||
real_y = 1-np.sqrt(real_x) | ||
plt.scatter(real_x, real_y, s=5, c='red') | ||
plt.scatter(pareto_x, pareto_y, s=5) | ||
|
||
plt.savefig('tmp/pf.png') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import optimizer | ||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
import matplotlib.animation as animation | ||
|
||
|
||
num_agents = 100 | ||
num_iterations = 300 | ||
num_params = 30 | ||
|
||
lb = [0] * num_params | ||
ub = [1] * num_params | ||
|
||
def zdt2_objective1(x): | ||
return x[0] | ||
|
||
def zdt2_objective2(x): | ||
f1 = x[0] | ||
g = 1.0 + 9.0 * sum(x[1:]) / (len(x) - 1) | ||
h = 1.0 - np.power((f1 *1.0 / g),2) | ||
f2 = g * h | ||
return f2 | ||
|
||
optimizer.FileManager.working_dir="tmp/zdt2/" | ||
optimizer.FileManager.loading_enabled = False | ||
optimizer.FileManager.saving_enabled = True | ||
|
||
objective = optimizer.ElementWiseObjective([zdt2_objective1, zdt2_objective2]) | ||
|
||
pso = optimizer.MOPSO(objective=objective,lower_bounds=lb, upper_bounds=ub, | ||
num_particles=num_agents, num_iterations=num_iterations, | ||
inertia_weight=0.4, cognitive_coefficient=0, social_coefficient=2, | ||
max_iter_no_improv=None) | ||
|
||
# run the optimization algorithm | ||
pso.optimize() | ||
|
||
fig, ax = plt.subplots() | ||
|
||
pareto_front = pso.get_current_pareto_front() | ||
n_pareto_points = len(pareto_front) | ||
pareto_x = [particle.fitness[0] for particle in pareto_front] | ||
pareto_y = [particle.fitness[1] for particle in pareto_front] | ||
|
||
real_x = (np.linspace(0, 1, n_pareto_points)) | ||
real_y = 1 - np.power(real_x, 2) | ||
plt.scatter(real_x, real_y, s=5, c='red') | ||
plt.scatter(pareto_x, pareto_y, s=5) | ||
|
||
plt.savefig('tmp/pf.png') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import optimizer | ||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
import matplotlib.animation as animation | ||
|
||
|
||
num_agents = 200 | ||
num_iterations = 300 | ||
num_params = 30 | ||
|
||
lb = [0] * num_params | ||
ub = [1] * num_params | ||
|
||
def zdt3_objective1(x): | ||
return x[0] | ||
|
||
def zdt3_objective2(x): | ||
f1 = x[0] | ||
g = 1.0 + 9.0 * sum(x[1:]) / (len(x) - 1) | ||
h = (1.0 - np.power(f1 * 1.0 / g, 0.5) - (f1 * 1.0 / g) * np.sin(10 * np.pi * f1)) | ||
f2 = g * h | ||
return f2 | ||
|
||
optimizer.FileManager.working_dir="tmp/zdt3/" | ||
optimizer.FileManager.loading_enabled = False | ||
optimizer.FileManager.saving_enabled = True | ||
|
||
objective = optimizer.ElementWiseObjective([zdt3_objective1, zdt3_objective2]) | ||
|
||
pso = optimizer.MOPSO(objective=objective,lower_bounds=lb, upper_bounds=ub, | ||
num_particles=num_agents, num_iterations=num_iterations, | ||
inertia_weight=0.4, cognitive_coefficient=1, social_coefficient=2, | ||
max_iter_no_improv=None) | ||
|
||
# run the optimization algorithm | ||
pso.optimize() | ||
|
||
fig, ax = plt.subplots() | ||
|
||
pareto_front = pso.get_current_pareto_front() | ||
n_pareto_points = len(pareto_front) | ||
pareto_x = [particle.fitness[0] for particle in pareto_front] | ||
pareto_y = [particle.fitness[1] for particle in pareto_front] | ||
|
||
regions = [[0, 0.0830015349], | ||
[0.182228780, 0.2577623634], | ||
[0.4093136748, 0.4538821041], | ||
[0.6183967944, 0.6525117038], | ||
[0.8233317983, 0.8518328654]] | ||
|
||
pf = [] | ||
|
||
for r in regions: | ||
x1 = np.linspace(r[0], r[1], int(n_pareto_points / len(regions))) | ||
x2 = 1 - np.sqrt(x1) - x1 * np.sin(10 * np.pi * x1) | ||
pf.append([x1, x2]) | ||
|
||
real_x = np.concatenate([x for x, _ in pf]) | ||
real_y = np.concatenate([y for _, y in pf]) | ||
|
||
plt.scatter(real_x, real_y, s=5, c='red') | ||
plt.scatter(pareto_x, pareto_y, s=5) | ||
|
||
plt.savefig('tmp/pf.png') | ||
|