def test_pso(): opp = op.OptimizationProblem('min', lambda x: x[:, 0]**2 - 4 * x[:, 0] + 4, [0], [6]) # check pso with given 1d minimization problem we should be close to true solution global_optimizer = go.GlobalOptimizer() x_star, f_star = global_optimizer.pso(opp) x_star = np.round(x_star, 2) assert abs(2 - x_star[0]) <= 1e-1 # check pso with given 1d maximization problem we should be close to true solution opp.mode = 'max' global_optimizer = go.GlobalOptimizer() x_star, f_star = global_optimizer.pso(opp) x_star_true = 6 assert abs(x_star_true - x_star[0]) <= 1e-1 # check pso with 2d rosenbrock minimization problem opp = op.OptimizationProblem( 'min', lambda x: (1 - x[:, 0])**2 + 100 * (x[:, 1] - x[:, 0]**2)**2, [-5, -5], [10, 10]) global_optimizer = go.GlobalOptimizer() x_star, f_star = global_optimizer.pso(opp) x_star = np.round(x_star, 2) x_star_true = np.array([1, 1]) err = np.round(max(abs(x_star_true - x_star)), 2) assert err <= 1e-1
def test_ga(): opp = op.OptimizationProblem('min', lambda x: x[:, 0]**2 - 4 * x[:, 0] + 4, [0], [6]) # check pso with given 1d minimization problem we should be close to true solution global_optimizer = go.GlobalOptimizer() x_star, f_star = global_optimizer.ga(opp) x_star_true = 2 err = np.round(max(abs(x_star_true - x_star)), 2) assert err <= 1e-1 # check pso with given 1d maximization problem we should be close to true solution opp.mode = 'max' global_optimizer = go.GlobalOptimizer() x_star, f_star = global_optimizer.ga(opp) x_star = np.round(x_star, 2) x_star_true = 6 err = np.round(max(abs(x_star_true - x_star)), 2) assert err <= 1e-1 # check pso with 2d rosenbrock minimization problem opp = op.OptimizationProblem( 'min', lambda x: np.sin(x[:, 0] + x[:, 1]) + (x[:, 0] - x[:, 1])**2 - 1.5 * x[:, 0] + 2.5 * x[:, 1] + 1, [-1.5, -3], [4, 4]) global_optimizer = go.GlobalOptimizer() x_star, f_star = global_optimizer.ga(opp) x_star = np.round(x_star, 2) x_star_true = np.array([-0.54, -1.54]) err = np.round(max(abs(x_star_true - x_star)), 2) assert err <= 1e-1
def test_optimization_problem(): optimization_problem = op.OptimizationProblem( 'min', lambda x: x[:, 0]**2 - 4 * x[:, 0] + 4, [0], [6]) # # just check things and functions assert optimization_problem.mode == 'min' assert optimization_problem.lower_constraints == np.array([0]) assert optimization_problem.upper_constraints == np.array([6]) random_input = np.random.randn(2, 2) assert optimization_problem.f(random_input)[0] == ( lambda x: x[:, 0]**2 - 4 * x[:, 0] + 4)(random_input)[0]
def fit(self, X, y, optimizer={}): self.X_train = X.copy() self.y_train = y.copy() self.d = len(X[0]) lower_constraints = np.random.uniform(-1, 0, self.d) upper_constraints = np.random.uniform(0, 1, self.d) global_optimizer = go.GlobalOptimizer() p = op.OptimizationProblem('min', self.cost_function, lower_constraints, upper_constraints, feasibility=False) # default params all algorithms w = 0.6 w_init = 0.9 N = 30 max_iter = 200 tol = 1e-4 trajectory = True mutation = True F = 2 CR = 0.7 self.lambda_ = 0 algo = 'pso' if 'algo' in optimizer: algo = optimizer['algo'] if 'w' in optimizer: w = optimizer['w'] if 'w_init' in optimizer: w_init = optimizer['w_init'] if 'N' in optimizer: N = optimizer['N'] if 'max_iter' in optimizer: max_iter = optimizer['max_iter'] if 'tol' in optimizer: tol = optimizer['tol'] if 'trajectory' in optimizer: trajectory = optimizer['trajectory'] if 'mutation' in optimizer: mutation = optimizer['mutation'] if 'F' in optimizer: F = optimizer['F'] if 'CR' in optimizer: CR = optimizer['CR'] if 'lambda_' in optimizer: self.lambda_ = optimizer['lambda_'] if algo == 'pso': x_star, J_star, x_path, y_path = global_optimizer.pso( p, trajectory=True, max_iter=max_iter, N=N, tol=tol, w=w) elif algo == 'apso': x_star, J_star, x_path, y_path = global_optimizer.adaptive_pso( p, trajectory=True, max_iter=max_iter, N=N, tol=tol, w_init=w_init) elif algo == 'de': x_star, J_star, x_path, y_path = global_optimizer.de( p, trajectory=True, max_iter=max_iter, N=N, tol=tol, F=F, CR=CR) elif algo == 'ga': x_star, J_star, x_path, y_path = global_optimizer.ga( p, trajectory=True, max_iter=max_iter, N=N, tol=tol, mutation=mutation) self.weights = x_star self.cost_function_values = y_path