def main(argv): opt_values = [] if FLAGS.opt_method == 'nelder_mead': logging.info('Running Nelder-Mead simplex.') res = scipy.optimize.minimize(eval, np.zeros(50 + 1), method='Nelder-Mead') logging.info(f'Got:\n{res}\n') opt_values = res.x elif FLAGS.opt_method == 'dual_annealing': logging.info('Running dual annealing.') bounds = [(-10, 10) for _ in range(50 + 1)] res = scipy.optimize.dual_annealing(eval, bounds, maxiter=2000) logging.info(f'Got:\n{res}\n') opt_values = res.x elif FLAGS.opt_method == 'particle_swarm': logging.info('Running PSO.') res = psopy.minimize(eval, np.random.uniform(-10, 10, (100, 50 + 1)), options={'stable_iter': 100}) logging.info(f'Got:\n{res}\n') opt_values = res.x else: logging.error(f'Unknown optimization method: {FLAGS.opt_method}') sys.exit(-1) if FLAGS.plot_file: logging.info(f'Saving plot to: {FLAGS.plot_file}') eval_plot(opt_values, FLAGS.plot_file)
def PSO(self, function, Initial_Bounds): self.coeff = psopy.minimize( function, numpy.array([numpy.random.uniform(*b, 10) for b in Initial_Bounds]).T).x self.description['tech'] = 'PSO' return self.coeff
def test_unconstrained(self): """Test against the Rosenbrock function.""" x0 = np.random.uniform(0, 2, (1000, 5)) sol = np.array([1., 1., 1., 1., 1.]) res = minimize(rosen, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_rosendisc(self): """Test against the Rosenbrock function constrained to a disk.""" cons = ({'type': 'ineq', 'fun': lambda x: -x[0]**2 - x[1]**2 + 2}, ) x0 = init_feasible(cons, low=-1.5, high=1.5, shape=(1000, 2)) sol = np.array([1., 1.]) def rosen(x): return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2 res = minimize(rosen, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_ackley(self): """Test against the Ackley function.""" x0 = np.random.uniform(-5, 5, (1000, 2)) sol = np.array([0., 0.]) def ackley(x): return -20 * np.exp(-.2 * np.sqrt(0.5 * (x[0]**2 + x[1]**2))) - \ np.exp(.5 * (np.cos(2*np.pi*x[0]) + np.cos(2*np.pi*x[1]))) + \ np.e + 20 res = minimize(ackley, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_levi(self): """Test against the Levi function.""" x0 = np.random.uniform(-10, 10, (1000, 2)) sol = np.array([1., 1.]) def levi(x): sin3x = np.sin(3*np.pi*x[0]) ** 2 sin2y = np.sin(2*np.pi*x[1]) ** 2 return sin3x + (x[0] - 1)**2 * (1 + sin3x) + \ (x[1] - 1)**2 * (1 + sin2y) res = minimize(levi, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def adversarial_with_minimize(self, instance, target_value=1): """ Attempts to use general minization to find adversarial. 'DOES NOT WORK': With nelder-mead this returns an instance that is of the adversarial class, but magnetic_sampling is not able to draw samples around it, because it seems to find outlier adversarials rather than a robust area. """ d = distance_function(instance) f = func(target_value, self.clf, d, 0.000000001, scaler=self.scaler) delta = np.random.rand(len(instance)) t_inst = self.scaler.transform(instance.reshape(1, -1)) print('old ', f(t_inst)) res = minimize(f, t_inst, method="nelder-mead") print('new ', f(res.x)) print(self.scaler.inverse_transform(res.x)) return self.scaler.inverse_transform(res.x)
def select_nontumor_cube(ct_array, segmented_lungs, box_size, n_particles=50, lung_frac_tgt=1., avg_int_tgt=-700, int_lw=1e-5, frac_lw=1): # Use Particle Swarm Optimization (PSO) to locate a cube that contains mostly the lungs # but does not contain the cancerous cube. # A "swarm" of initial guesses for the starting corner of the cube x0 = np.hstack((np.random.uniform(0, (segmented_lungs.shape[0]-box_size[0]), (n_particles, 1)), np.random.uniform(0, (segmented_lungs.shape[1]-box_size[1]), (n_particles, 1)), np.random.uniform(0, (segmented_lungs.shape[2]-box_size[2]), (n_particles, 1)))) # The corner must be within the limits of the original scan constraints = ({'type': 'ineq', 'fun': lambda x: x[0]}, {'type': 'ineq', 'fun': lambda x: x[1]}, {'type': 'ineq', 'fun': lambda x: x[2]}, {'type': 'stin', 'fun': lambda x: (segmented_lungs.shape[0]-box_size[0]) - x[0]}, {'type': 'stin', 'fun': lambda x: (segmented_lungs.shape[1]-box_size[1]) - x[1]}, {'type': 'stin', 'fun': lambda x: (segmented_lungs.shape[2]-box_size[2]) - x[2]}) # Optimize the corner location res = minimize(segment_opt, x0, constraints=constraints, args=(segmented_lungs, ct_array, box_size, lung_frac_tgt, avg_int_tgt, int_lw, frac_lw)) box_start_good = np.round(res.x).astype(int) segmented_cube = segmented_lungs[box_start_good[0]:box_start_good[0]+box_size[0], box_start_good[1]:box_start_good[1]+box_size[1], box_start_good[2]:box_start_good[2]+box_size[2]] lung_frac = np.sum(segmented_cube) / np.prod(box_size) print('\t\t%0.f%% of the selected cube resides within the lungs.'%(lung_frac*100)) # Segment this cube good_segment = ct_array[box_start_good[0]:box_start_good[0]+box_size[0], box_start_good[1]:box_start_good[1]+box_size[1], box_start_good[2]:box_start_good[2]+box_size[2]] avg_int = np.mean(good_segment) print('\t\tThe average intensity of the cube is %0.2f HU.'%(avg_int)) # Mask this area to not use it again segmented_lungs[box_start_good[0]:box_start_good[0]+box_size[0], box_start_good[1]:box_start_good[1]+box_size[1], box_start_good[2]:box_start_good[2]+box_size[2]] = -1000 return good_segment, segmented_lungs, box_start_good, lung_frac, avg_int
def test_mishra(self): """Test against the Mishra Bird function.""" cons = ({'type': 'ineq', 'fun': lambda x: 25 - np.sum((x + 5)**2)}, ) x0 = init_feasible(cons, low=-10, high=0, shape=(1000, 2)) sol = np.array([-3.130, -1.582]) def mishra(x): cos = np.cos(x[0]) sin = np.sin(x[1]) return sin*np.e**((1 - cos)**2) + cos*np.e**((1 - sin)**2) + \ (x[0] - x[1])**2 res = minimize(mishra, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_constrained(self): """Test against the following function:: y = (x0 - 1)^2 + (x1 - 2.5)^2 under the constraints:: x0 - 2.x1 + 2 >= 0 -x0 - 2.x1 + 6 >= 0 -x0 + 2.x1 + 2 >= 0 x0, x1 >= 0 """ cons = ({ 'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2 }, { 'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6 }, { 'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2 }, { 'type': 'ineq', 'fun': lambda x: x[0] }, { 'type': 'ineq', 'fun': lambda x: x[1] }) x0 = init_feasible(cons, low=0, high=2, shape=(1000, 2)) options = { 'g_rate': 1., 'l_rate': 1., 'max_velocity': 4., 'stable_iter': 50 } sol = np.array([1.4, 1.7]) res = minimize(lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2, x0, constraints=cons, options=options) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
default 1e-7. verbose : bool, optional Set True to display convergence messages. savefile : string or None, optional File to save global best solution vector and its corresponding function value for each iteration as a csv file. If None, no data is saved. """ pso_options = { "friction": 0.75, "max_velocity": 5.0, "g_rate": 0, "l_rate": 1, "max_iter": 1000, "stable_iter": 15, "ptol": 0.001, "ctol": 1e-6, "verbose": True, "savefile": "100_particle_solution_12.csv" } sol = minimize(objfun, x0=initial_swarm, args=(sample_robots, sample_tasks), tol=1e-6, callback=None, options=pso_options) print(sol)
rates = np.empty((ncheck, ncheck)) values = np.arange(rmin, rmax + 0.1, step) print("\n Try different values of learning rates in dimension 50 :") g = 0 for grate in values: l = 0 for lrate in values: x_0s = [fs.define_x_rand(count=n_pop, dim=d) for d in D] i = (ncheck * l + g) % 25 f = s_fam[0][i] myResult = pso.minimize(f, x_0s[0], options={ 'max_iter': 200, 'verbose': False, 'friction': 0.5, 'g_rate': grate, 'l_rate': lrate }, constraints=const[0:50].extend(const[500:550])) rates[l, g] = myResult.fun print("local rate = ", lrate, " ; global rate = ", grate, " ; fitness = ", rates[l, g]) l += 1 g += 1 rates1 = rates plt.imshow(rates1, cmap=cm.gray) plt.title(label="Fitness of PSO according to learning rate \n \ (global_rate*2, local_rate*2) \ \n in dimension 50")
def run_pso_clustering(mus, sigmas, ids, num_clusters=2, num_objectives=3, verbose=False, optimization_iterations=10, num_particles=10): try: from psopy import minimize except ImportError: print("Psopy not installed: ") return 0, 0, 0 num_agents = len(mus) weight_calculator = make_weights_assignment_function( mus, sigmas, ids, num_objectives=num_objectives, verbose=verbose, num_iters=optimization_iterations) # The objective function. def obj_func(assignment): assignment = assignment.reshape((num_clusters, num_agents)) obj = 0 for i in range(assignment.shape[0]): w, loss = weight_calculator(assignment[i]) obj += loss return obj # The constraints. def simplex(p): p = p.reshape((num_clusters, num_agents)) if np.isclose(np.sum(p, axis=0) - 1., 0.).all(): return 0 else: return -1 cons = ({ 'type': 'ineq', 'fun': lambda p: np.min(1 - p) }, { 'type': 'ineq', 'fun': lambda p: np.min(p) }, { 'type': 'eq', 'fun': simplex }) x0 = np.zeros((num_particles, num_clusters * num_agents)) for i in range(num_particles): p = np.random.uniform(size=(num_clusters, num_agents)) p = p / np.sum(p, axis=0) p = p.flatten() x0[i] = p res = minimize(obj_func, x0, constraints=cons, options={ 'g_rate': 1., 'l_rate': 1., 'max_velocity': 4., 'stable_iter': 50 }) print(res.x) best_assignment = res.x loss_value = 0. best_weights = np.zeros(shape=(num_clusters, num_objectives)) best_assignment = best_assignment.reshape((num_clusters, num_agents)) for i in range(num_clusters): w, loss = weight_calculator(best_assignment[i]) best_weights[i] = w loss_value += loss return best_assignment, best_weights, loss_value
x0 = np.random.uniform(0.0, 0.1, (25, 1)) q.setparams(K=0.3061728395, L=0.38888888899999996) print(f(x0)) results = [] # This is Python's string formatting using the method `str.format`. record = 'Record: {:05d} fun: {:.6f} x: {:.4f} constr: {:.4f} {:.4f}' cost=[] position=[] # Iterate over all records. for i in range(len(df)): # Options must be specified this way because the function modifies the # dictionary passed to the parameter `options`. q.setparams(K=df['ICR'][i],L=df['NLIQ'][i]) result = minimize(fun=f, x0=x0,constraints=constraints, options = {'g_rate': 0.6, 'l_rate': 0.3, 'max_velocity': 0.9,'stable_iter': 100,'sttol': 1e-4}) # Print a nice pretty report line. print(record.format(i + 1, result['fun'], result['x'][0],result['cvec'][0, 0], result['cvec'][0, 1])) position.append(result['x'][0]) cost.append(result['fun']) results.append(result) df['rho'] = position df['Internationality'] = cost #df.to_csv('result/resultV1.csv') # I haven't written any code to save the results in a file because I'm not # sure about the formatting required. Each item in `results` has, # # cvec -- constraint vector for `x`,