def test_rosenbrock(self): pso = PSO(optimization_object=ROSENBROCK['optimization'], no_particles=100, no_iteration_steps=230, c_1=1.2215, c_2=1.5416) pso.solve() at_least_matched = False for r in ROSENBROCK['results']: matched = abs(r[0] - pso.best[0]) < 5e-5 print(r[0] - pso.best[0]) for i in range(len(r[1])): matched &= abs(r[1][i] - pso.best[1][i]) < 1e-2 at_least_matched |= matched assert at_least_matched
def test_himmelblau(self): pso = PSO(optimization_object=HIMMELBLAU['optimization'], no_particles=140, no_iteration_steps=225, c_1=1.0520, c_2=0.8791) pso.solve() at_least_matched = False for r in HIMMELBLAU['results']: matched = abs(r[0] - pso.best[0]) < 5e-5 print(r[0] - pso.best[0]) for i in range(len(r[1])): matched &= abs(r[1][i] - pso.best[1][i]) < 1e-2 at_least_matched |= matched assert at_least_matched
def _ms_gen(self, x): core_pso = PSO(optimization_object=self.ori_opt_object, no_particles=math.ceil(x[0]), no_iteration_steps=math.ceil(x[1]), c_1=x[2], c_2=x[3]) core_ms = \ MultipleSolving(algorithm_obj=core_pso, no_tries=self.no_tries) return core_ms
def test___init__(self, fix_optimization_object): no_particles = 10 no_iteration_steps = 20 pso_1 = PSO(optimization_object=fix_optimization_object, no_particles=no_particles, no_iteration_steps=no_iteration_steps, c_1=2.0, c_2=2.0) assert pso_1.optimization_object == fix_optimization_object assert pso_1.no_particles == no_particles assert pso_1.no_iteration_steps == no_iteration_steps assert pso_1.learning_factors == (2.0, 2.0) assert isinstance(pso_1.particles, list) assert isinstance(pso_1.best, tuple)
def test_solve(self, fix_optimization_object, capsys): pso_1 = PSO(optimization_object=fix_optimization_object, no_particles=100, no_iteration_steps=200, c_1=2.0, c_2=2.0) result = pso_1.solve() assert isinstance(result, tuple) assert result[0] <= -4.130395 pso_2 = PSO(optimization_object=fix_optimization_object, no_particles=10, no_iteration_steps=20, c_1=2.0, c_2=2.0, historical=True, verbose=True) pso_2.solve() assert pso_2.snapshots.__len__() == 20 out, err = capsys.readouterr() for i in range(20): assert out.find("Iteration step #%d" % i) > -1
def run(self, **kwargs): no_particles = kwargs.get('no_particles', 10) no_iterations = kwargs.get('no_iterations', 20) c_1 = kwargs.get('c_1', 2.0) c_2 = kwargs.get('c_2', 2.0) opt_obj = Optimization(optimizing_function=self._optimizing_function, boundaries=[(0, self.max_particles), (0, self.max_iteration), (0, self.max_learning_factor), (0, self.max_learning_factor)], no_dimensions=4, find_max=False) opt_obj.add_constraint(self._constraint_function) pso = PSO(optimization_object=opt_obj, no_particles=no_particles, no_iteration_steps=no_iterations, c_1=c_1, c_2=c_2, verbose=True) ms = MultipleSolving(algorithm_obj=pso, no_tries=kwargs.get('no_tries', 5)) max_thread = kwargs.get('max_thread', 0) ms.run(pool=Pool(max_thread if max_thread else None)) return ms
return x[0] * 40 + x[1] * 50 def constraint(x): return (x[0] + x[1] <= 50) & \ (x[0] * 3 + x[1] * 4 <= 180) & \ (x[1] <= 40) optimization = Optimization( optimizing_function=optimizing_function, boundaries=[(0, 100), (0, 100)], no_dimensions=2, find_max=True ) optimization.add_constraint(constraint) pso = PSO( optimization_object=optimization, no_particles=10, no_iteration_steps=20, c_1=2.0, c_2=2.0 ) ms = MultipleSolving(pso, 20) ms.run() print(ms)
for i in no_iter_steps_suite: test_cases.append({ 'no_particles': p, 'no_iteration_steps': i, 'c_1': general_c1, 'c_2': general_c2 }) writer = csv.writer(args.output_file) writer.writerow([ '#Particles', '#IterSteps', 'Mean', 'Median', 'StdDev', 'SuccessRate' ]) for test_case in test_cases: pso = PSO(optimization_object=ROSENBROCK['optimization'], **test_case) ms = MultipleSolving(pso, 200) ms.run(Pool()) writer.writerow([ # Particles test_case['no_particles'], # IterSteps test_case['no_iteration_steps'], # Mean statistics.mean(ms.results_value_only), # Median statistics.median(ms.results_value_only), # StdDev statistics.stdev(ms.results_value_only), # SuccessRate calculate_success_rate(ms.results_value_only, max_error=0.05)
optimizing_function=year_2.calculate_profit, boundaries=[ (5, 100.0), (5, 100.0), (5, 100.0), (5, 100.0), ], no_dimensions=4, find_max=True ) optimization_2_years.add_constraint(year_2.check_consumption) pso_1st = PSO( optimization_object=optimization_2_years, no_particles=40, no_iteration_steps=20, c_1=2.0, c_2=2.0 ) print("Result for 1st case:") ms_1st = MultipleSolving(pso_1st, 10) ms_1st.run() print(ms_1st) # 2nd case: 3 year production planning with linear profit model def profit_calculator_2nd(x, prices): """ :type x: list(float) :type prices: list(float) :rtype: float