def check_feasibility(dist, k, **kwargs): """ Checks feasibility by solving the minimum residual problem: minimize: max(abs(A x - b)) If the value of the objective is close to zero, then we know that we can match the constraints, and so, the problem is feasible. """ from cvxopt import matrix from cvxopt.modeling import variable A, b = marginal_constraints(dist, k) A = matrix(A) b = matrix(b) n = len(dist.pmf) x = variable(n) t = variable() c1 = (-t <= A * x - b) c2 = ( A * x - b <= t) c3 = ( x >= 0 ) objective = t constraints = [c1, c2, c3] opt = op_runner(objective, constraints, **kwargs) if opt.status != 'optimal': raise Exception('Not feasible') return opt
def check_feasibility(dist, k, **kwargs): """ Checks feasibility by solving the minimum residual problem: minimize: :math:`max(abs(A x - b))` If the value of the objective is close to zero, then we know that we can match the constraints, and so, the problem is feasible. """ from cvxopt import matrix from cvxopt.modeling import variable A, b = marginal_constraints(dist, k) A = matrix(A) b = matrix(b) n = len(dist.pmf) x = variable(n) t = variable() c1 = (-t <= A * x - b) c2 = (A * x - b <= t) c3 = (x >= 0) objective = t constraints = [c1, c2, c3] opt = op_runner(objective, constraints, **kwargs) if opt.status != 'optimal': raise Exception('Not feasible') return opt
def _create_op(self, state, Q): var_pi = [variable() for _ in range(self.action_count)] constraints = [(var_pi[i] >= 0) for i in range(self.action_count)] constraints.append((sum(var_pi) == 1)) v = variable() for j in range(self.action_count): c = 0 for i in range(self.action_count): c += float(Q[state, i, j]) * var_pi[i] constraints.append((c >= v)) return op(-v, constraints), v, var_pi
def feasible_point(G, h, progress=False): """ Finds a single point within constraints specified by Gx <= h. Parameters ---------- G: pandas.DataFrame Array that specifies Gx <= h. h: pandas.Series The limits specifying Gx <= h. progress: bool True if detailed progress text from optimization should be shown. Returns ------- x: pd.Series Point with index names equal to the column names of G. """ # Aligning input h = h.ix[G.index] if h.isnull().values.any() or G.isnull().values.any(): msg = 'Row indeces of G and h must match and contain no NaN entries.' omfa.logger.error(msg) raise ValueError(msg) if progress: solvers.options['show_progress'] = True else: solvers.options['show_progress'] = False # Setting up LP problem m, n = G.shape s = variable() x = variable(n) G_opt = matrix(np.array(G, dtype=np.float64)) h_opt = matrix(np.array(h, dtype=np.float64)) inequality_constraints = [G_opt[k,:]*x + s <= h_opt[k] for k in range(m)] # Run LP to find feasible point model = op(-s, inequality_constraints) model.solve() if s.value[0] <= 0: msg = 'Could not find feasible starting point to calculate centroid.' omfa.logger.error(msg) raise omfa.ModelError(msg) out = pd.Series(x.value, index=G.columns) return(out)
def test_case1(self): x = variable() y = variable() c1 = (2 * x + y <= 3) c2 = (x + 2 * y <= 3) c3 = (x >= 0) c4 = (y >= 0) lp1 = op(-4 * x - 5 * y, [c1, c2, c3, c4]) print(lp1) print(str(lp1)) lp1.solve() self.assertTrue(lp1.status == 'optimal')
def setUp(self): """ Use cvxopt to get ground truth values """ from cvxopt import lapack, solvers, matrix, spdiag, log, div, normal, setseed from cvxopt.modeling import variable, op, max, sum solvers.options['show_progress'] = 0 setseed() m, n = 100, 30 A = normal(m, n) b = normal(m, 1) b /= (1.1 * max(abs(b))) self.m, self.n, self.A, self.b = m, n, A, b # l1 approximation # minimize || A*x + b ||_1 x = variable(n) op(sum(abs(A * x + b))).solve() self.x1 = x.value # l2 approximation # minimize || A*x + b ||_2 bprime = -matrix(b) Aprime = matrix(A) lapack.gels(Aprime, bprime) self.x2 = bprime[:n] # Deadzone approximation # minimize sum(max(abs(A*x+b)-0.5, 0.0)) x = variable(n) dzop = op(sum(max(abs(A * x + b) - 0.5, 0.0))) dzop.solve() self.obj_dz = sum( np.max([np.abs(A * x.value + b) - 0.5, np.zeros((m, 1))], axis=0)) # Log barrier # minimize -sum (log ( 1.0 - (A*x+b)**2)) def F(x=None, z=None): if x is None: return 0, matrix(0.0, (n, 1)) y = A * x + b if max(abs(y)) >= 1.0: return None f = -sum(log(1.0 - y**2)) gradf = 2.0 * A.T * div(y, 1 - y**2) if z is None: return f, gradf.T H = A.T * spdiag(2.0 * z[0] * div(1.0 + y**2, (1.0 - y**2)**2)) * A return f, gradf.T, H self.cxlb = solvers.cp(F)['x']
def setUp(self): """ Use cvxopt to get ground truth values """ from cvxopt import lapack,solvers,matrix,spdiag,log,div,normal,setseed from cvxopt.modeling import variable,op,max,sum solvers.options['show_progress'] = 0 setseed() m,n = 100,30 A = normal(m,n) b = normal(m,1) b /= (1.1*max(abs(b))) self.m,self.n,self.A,self.b = m,n,A,b # l1 approximation # minimize || A*x + b ||_1 x = variable(n) op(sum(abs(A*x+b))).solve() self.x1 = x.value # l2 approximation # minimize || A*x + b ||_2 bprime = -matrix(b) Aprime = matrix(A) lapack.gels(Aprime,bprime) self.x2 = bprime[:n] # Deadzone approximation # minimize sum(max(abs(A*x+b)-0.5, 0.0)) x = variable(n) dzop = op(sum(max(abs(A*x+b)-0.5, 0.0))) dzop.solve() self.obj_dz = sum(np.max([np.abs(A*x.value+b)-0.5,np.zeros((m,1))],axis=0)) # Log barrier # minimize -sum (log ( 1.0 - (A*x+b)**2)) def F(x=None, z=None): if x is None: return 0, matrix(0.0,(n,1)) y = A*x+b if max(abs(y)) >= 1.0: return None f = -sum(log(1.0 - y**2)) gradf = 2.0 * A.T * div(y, 1-y**2) if z is None: return f, gradf.T H = A.T * spdiag(2.0*z[0]*div(1.0+y**2,(1.0-y**2)**2))*A return f,gradf.T,H self.cxlb = solvers.cp(F)['x']
def test_case1(self): x = variable() y = variable() c1 = ( 2*x+y <= 3 ) c2 = ( x+2*y <= 3 ) c3 = ( x >= 0 ) c4 = ( y >= 0 ) lp1 = op(-4*x-5*y, [c1,c2,c3,c4]) print(repr(x)) print(str(x)) print(repr(lp1)) print(str(lp1)) lp1.solve() print(repr(x)) print(str(x)) self.assertTrue(lp1.status == 'optimal')
def balance(self, energy_demand): active_engines = filter(lambda engine: engine.is_enabled, self.engines) if len(active_engines) <= 0: return [] rpms = variable(len(active_engines), 'rpms') fixed_engine_costs = matrix([float(engine.engine_type.fixed_engine_cost) for engine in active_engines]) linear_engine_costs = matrix([float(engine.engine_type.linear_engine_cost) for engine in active_engines]) fixed_energy_outputs = matrix([float(engine.engine_type.fixed_energy_output) for engine in active_engines]) linear_energy_outputs = matrix([float(engine.engine_type.linear_energy_output) for engine in active_engines]) minimum_rpms = matrix([float(engine.engine_type.minimum_rpm) for engine in active_engines]) maximum_rpms = matrix([float(engine.engine_type.maximum_rpm) for engine in active_engines]) energy_demand_constraint = ((float(energy_demand) - dot(rpms, linear_energy_outputs) - sum(fixed_energy_outputs)) <= 0) maximum_rpm_constraint = ((rpms - maximum_rpms) <= 0) minimum_rpm_constraint = ((rpms - minimum_rpms) >= 0) constraints = [energy_demand_constraint, maximum_rpm_constraint, minimum_rpm_constraint] objective_function = op((dot(linear_engine_costs, rpms) - sum(fixed_engine_costs)), constraints) objective_function.solve() for i in range(len(active_engines)): engine = active_engines[i] engine.rpm = rpms.value[i] engine.energy_output = float(engine.engine_type.fixed_energy_output) * engine.rpm + float(engine.engine_type.fixed_energy_output) return active_engines
def _get_convex_opt_assignment(C): m, n = C.shape c = matrix(C.reshape(m * n)) x = variable(m * n) constraints = [ x >= 0, x <= 1, ] # row constraints for i in range(m): #print('setting constraint', i*n, i*n+n) constraints.append(cvx_sum(x[i * n:i * n + n]) == 1) # col constraints # add constraints so max number of assignments to each port in ports2 # is 1 as well for j in range(n): #print(list(range(j, m*n, n))) constraints.append(cvx_sum([x[jj] for jj in range(j, m * n, n)]) <= 1) # NOTE must use external solver (such as glpk), the default one is # _very_ slow op( dot(c, x), constraints, ).solve(solver='glpk') X = np.array(x.value).reshape(m, n) > 0.01 return X
def solve_lad(X, Y): Y_cvx = matrix(Y) X_cvx = matrix(X) w_hat = variable(X.shape[1]) solvers.options['show_progress'] = False op(sum(abs(Y_cvx - X_cvx * w_hat))).solve() return w_hat.value
def _findChebyshevCenter(G, h, full_output=False): # return the binding constraints bindingIndex, dualHull, G, h, x0 = bindingConstraint(G, h, None, True) # Chebyshev center R = variable() xc = variable(2) m = len(h) op(-R, [G[k, :] * xc + R * blas.nrm2(G[k, :]) <= h[k] for k in range(m)] + [R >= 0]).solve() R = R.value xc = xc.value if full_output: return numpy.array(xc).flatten(), G, h else: return numpy.array(xc).flatten()
def Pseudoinverse(A): n = size(A, 0) I = zeros((n,n)) for i in range(n): I[i,i] = 1 X = variable(n**2) mu = variable(n**2) nu = variable() C = [] for i in range(n): for j in range(n): C.append( (I[i,j]-sum(A[i,k]*X[n*k+j] for k in range(n)) <= mu[n*i+j]) ) C.append( (-I[i,j]+sum(A[i,k]*X[n*k+j] for k in range(n)) <= mu[n*i+j]) ) for i in range(n): C.append( (sum(mu[n*i+j] for j in range(n)) <= nu) ) op(nu, [C[0]]).solve() return X.value
def test_problem_penalty(self): """ Compare cvxpy solutions to cvxopt ground truth """ from cvxpy import (matrix, variable, program, minimize, sum, abs, norm2, log, square, zeros, max, hstack, vstack) m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # set tolerance to 5 significant digits tol_exp = 5 # l1 approximation x = variable(n) p = program(minimize(sum(abs(A * x + b)))) p.solve(True) np.testing.assert_array_almost_equal(x.value, self.x1, tol_exp) # l2 approximation x = variable(n) p = program(minimize(norm2(A * x + b))) p.solve(True) np.testing.assert_array_almost_equal(x.value, self.x2, tol_exp) # Deadzone approximation - implementation is currently ugly (need max along axis) x = variable(n) Axbm = abs(A * x + b) - 0.5 Axbm_deadzone = vstack( [max(hstack((Axbm[i, 0], 0.0))) for i in range(m)]) p = program(minimize(sum(Axbm_deadzone))) p.solve(True) obj_dz_cvxpy = np.sum( np.max([np.abs(A * x.value + b) - 0.5, np.zeros((m, 1))], axis=0)) np.testing.assert_array_almost_equal(obj_dz_cvxpy, self.obj_dz, tol_exp) # Log barrier x = variable(n) p = program(minimize(-sum(log(1.0 - square(A * x + b))))) p.solve(True) np.testing.assert_array_almost_equal(x.value, self.cxlb, tol_exp)
def minimum_effort_control(): ''' Minimum effort control problem minimize max{||D_jerk * x||} subject to A_eq * x == b_eq ''' #create matrix data type for cvxopt D_sparse = sparse([matrix(1/float_(power(N, 3))*D_jerk)]) # we multiply # D_jerk with 1/float_(power(N, 3)) to ensure numerical stability A_eq = sparse([matrix(Aeq)]) b_eq = matrix(beq) t = variable() #auxiliary variable x = variable(N) #x position of particle op(t, [-t <= D_sparse*x, D_sparse*x <= t, A_eq*x == b_eq]).solve() #linear program return x
def test_case2(self): x = variable(2) A = matrix([[2., 1., -1., 0.], [1., 2., 0., -1.]]) b = matrix([3., 3., 0., 0.]) c = matrix([-4., -5.]) ineq = (A * x <= b) lp2 = op(dot(c, x), ineq) lp2.solve() self.assertAlmostEqual(lp2.objective.value()[0], -9.0, places=4)
def test_case2(self): x = variable(2) A = matrix([[2.,1.,-1.,0.], [1.,2.,0.,-1.]]) b = matrix([3.,3.,0.,0.]) c = matrix([-4.,-5.]) ineq = ( A*x <= b ) lp2 = op(dot(c,x), ineq) lp2.solve() self.assertAlmostEqual(lp2.objective.value()[0], -9.0, places=4)
def cvxoptAttempt(train,labels): ''' defunct ''' A = cx.matrix(train) B = cx.matrix(labels) x = variable() holdsol = op(objective_fn(A,B,x,0,2)) sol = holdsol.solve()
def _create_op(self, state, Q1, Q2): var_pi = [[variable() for _ in range(self.action_count)] for __ in range(self.action_count)] sum_var_pi = 0 constraints = [] for i in range(self.action_count): for j in range(self.action_count): constraints.append(var_pi[i][j] >= 0) sum_var_pi += var_pi[i][j] constraints.append((sum_var_pi == 1)) v = variable() for i in range(self.action_count): rc1 = 0 for j in range(self.action_count): rc1 += var_pi[i][j] * float(Q1[state, i, j]) for k in range(self.action_count): if i != k: rc2 = 0 for j in range(self.action_count): rc2 += var_pi[i][j] * float(Q1[state, k, j]) constraints.append((rc1 >= rc2)) for i in range(self.action_count): rc1 = 0 for j in range(self.action_count): rc1 += var_pi[j][i] * float(Q2[state, j, i]) for k in range(self.action_count): if i != k: rc2 = 0 for j in range(self.action_count): rc2 += var_pi[j][i] * float(Q2[state, j, k]) constraints.append((rc1 >= rc2)) sum_total = 0 for i in range(self.action_count): for j in range(self.action_count): sum_total += var_pi[i][j] * float(Q1[state, i, j]) sum_total += var_pi[i][j] * float(Q2[state, i, j]) constraints.append((v == sum_total)) return op(-v, constraints), v, var_pi
def solver(A, b): A = matrix(A) b = matrix(b) x = variable(n) start = time.time() op(sum(abs(A*x-b))).solve() end = time.time() sol_x = np.array(x.value).flatten() return ((end - start), sol_x)
def test_problem_penalty(self): """ Compare cvxpy solutions to cvxopt ground truth """ from cvxpy import (matrix,variable,program,minimize, sum,abs,norm2,log,square,zeros,max, hstack,vstack) m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # set tolerance to 5 significant digits tol_exp = 5 # l1 approximation x = variable(n) p = program(minimize(sum(abs(A*x + b)))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x1,tol_exp) # l2 approximation x = variable(n) p = program(minimize(norm2(A*x + b))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x2,tol_exp) # Deadzone approximation - implementation is currently ugly (need max along axis) x = variable(n) Axbm = abs(A*x+b)-0.5 Axbm_deadzone = vstack([max(hstack((Axbm[i,0],0.0))) for i in range(m)]) p = program(minimize(sum(Axbm_deadzone))) p.solve(True) obj_dz_cvxpy = np.sum(np.max([np.abs(A*x.value+b)-0.5,np.zeros((m,1))],axis=0)) np.testing.assert_array_almost_equal(obj_dz_cvxpy,self.obj_dz,tol_exp) # Log barrier x = variable(n) p = program(minimize(-sum(log(1.0-square(A*x + b))))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.cxlb,tol_exp)
def minimax_op(Qs): from cvxopt.modeling import dot, op, variable solvers.options['show_progress'] = False v = variable() p = variable(5) c1 = p >= 0 c2 = sum(p) == 1 c3 = dot(matrix(Qs), p) >= v lp = op(-v, [c1, c2, c3]) lp.solve() success = lp.status == 'optimal' Vs = v.value[0] return Vs, success
def test_case3(self): m, n = 500, 100 setseed(100) A = normal(m,n) b = normal(m) x1 = variable(n) lp1 = op(max(abs(A*x1-b))) lp1.solve() self.assertTrue(lp1.status == 'optimal') x2 = variable(n) lp2 = op(sum(abs(A*x2-b))) lp2.solve() self.assertTrue(lp2.status == 'optimal') x3 = variable(n) lp3 = op(sum(max(0, abs(A*x3-b)-0.75, 2*abs(A*x3-b)-2.25))) lp3.solve() self.assertTrue(lp3.status == 'optimal')
def minimum_effort_control_3D(D_jerk, Aeq, beq, N): ''' Minimum effort control problem minimize max{||D_jerk * x||} subject to A_eq * x == b_eq ''' # create matrix data type for cvxopt # we multiply D_jerk with 1/float_(power(2 * N, 3)) to ensure numerical # stability D_sparse = sparse([matrix(1 / np.float_(np.power(N, 3)) * D_jerk)]) A_eq = sparse([matrix(Aeq)]) b_eq = matrix(beq) t = variable() # auxiliary variable x = variable(3 * N) # x, y position of particles solvers.options['feastol'] = 1e-6 solvers.options['show_progress'] = False # linear program op(t, [-t <= D_sparse * x, D_sparse * x <= t, A_eq * x == b_eq]).solve() return x
def _foe_objective_fn(Q_s): # pi_N, pi_E, pi_W, pi_S, pi_X na = Q_s.shape[0] # Variable vector [pi_N, pi_E, pi_W, pi_S, pi_X, V] x = variable(na) # Maximize the probability distribution pi obj_matrix = np.zeros(len(Q_s)) obj_matrix[-1] = -1 c = matrix(obj_matrix) return dot(c, x)
def slack_m(self): m, N = self.stock.shape lis = [] liss = [] for j in range(2**self.T - 1): for i in range(N): lis.append(variable(1, 'x')) liss.append(lis) lis = [] return liss
def test_case3(self): m, n = 500, 100 setseed(100) A = normal(m, n) b = normal(m) x1 = variable(n) lp1 = op(max(abs(A * x1 - b))) lp1.solve() self.assertTrue(lp1.status == 'optimal') x2 = variable(n) lp2 = op(sum(abs(A * x2 - b))) lp2.solve() self.assertTrue(lp2.status == 'optimal') x3 = variable(n) lp3 = op( sum(max(0, abs(A * x3 - b) - 0.75, 2 * abs(A * x3 - b) - 2.25))) lp3.solve() self.assertTrue(lp3.status == 'optimal')
def solve(problem): # Variables R = variable(1, 'R') P = variable(1, 'P') S = variable(1, 'S') V = variable(1, 'V') # Variable vector [R, P, S, V] x = variable(4) # minimize cTx == -V c = matrix([0., 0., 0., -1.]) objective = dot(c, x) # Matrices Ainit = np.zeros((0, len(x))) # R = 0, P = 1, S = 2, V = V for row in problem: Ainit = np.vstack([Ainit, [row[0], row[1], row[2], 1]]) # Other known constraints Ainit = np.vstack([Ainit, [1., 1., 1., 0.]]) # R + P + S <= 1 # Ainit = np.vstack([Ainit, [-1., 0., 0., 0.]]) # -R <= 0 # Ainit = np.vstack([Ainit, [0., -1., 0., 0.]]) # -P <= 0 # Ainit = np.vstack([Ainit, [0., 0., -1., 0.]]) # -S <= 0 A = matrix(Ainit) # b = matrix([0., 0., 0., 1., 0., 0., 0.]) b = matrix([0., 0., 0., 1.]) # Linear constraints embedded in matrices ineq = (A * x <= b) lp = op(objective, ineq) lp.solve() # [R, P, S, V] ^ T return ineq.multiplier.value
def lin_regression(): raw_data = pd.read_csv("../Datasets/winequality-red.csv", sep=";", header=0) raw_training = raw_data[:1500] x = cm.matrix(raw_training.iloc[:, :-1].to_numpy(dtype=float)) y = cm.matrix(raw_training.iloc[:, -1:].to_numpy(dtype=float)) raw_test = raw_data[1500:].reset_index(drop=True) x_test = cm.matrix(raw_test.iloc[:, :-1].to_numpy(dtype=float)) y_test = cm.matrix(raw_test.iloc[:, -1:].to_numpy(dtype=float)) a = cm.variable(x.size[1]) b = cm.variable() z = cm.variable(x.size[0]) constraint_1 = (z >= (y - x * a - b)) constraint_2 = (z >= (x * a + b - y)) z_min = cm.op(cm.min(cm.sum(z) / x.size[0]), [constraint_1, constraint_2]) z_min.solve() calc_a = a.value calc_b = b.value z_train = y - x * calc_a - calc_b z_test = y_test - x_test * calc_a - calc_b train_results = x * calc_a + calc_b average_training_error = mean_square_error(y, train_results) test_results = x_test * calc_a + calc_b average_test_error = mean_square_error(y_test, test_results) print(f"average training error = {average_training_error}") print(f"average testing error = {average_test_error}")
def _update_learners_weights(self, t, samples_dist): """ Solve equation (3) in the paper, returns the set of lagrange multipliers that matches w """ n = len(samples_dist) batch_size = min(self.max_batch_size, int(n * self.batch_size_ratio)) batch_indices = np.random.choice(a=list(range(n)), replace=False, p=samples_dist, size=batch_size) # Weak learners weights - what we need to find w = modeling.variable(t, 'w') # Slack variables # zetas = {int(i): modeling.variable(1, 'zeta_%d' % int(i)) for i in batch_indices} zetas = modeling.variable(batch_size, 'zetas') # Margin rho = modeling.variable(1, 'rho') # Constraints c1 = (w >= 0) c2 = (sum(w) == 1) c_slacks = (zetas >= 0) c_soft_margins = [ (modeling.dot(matrix(self.u[sample_idx].astype(float).T), w) >= (rho - zetas[int(idx)])) for idx, sample_idx in enumerate(batch_indices) ] # Solve optimisation problems lp = modeling.op(-(rho - self.kappa * modeling.sum(zetas)), [c1, c2, c_slacks] + c_soft_margins) solvers.options['show_progress'] = False lp.solve() return w.value
def balance(request, engines, demand): engines = json.loads(engines) enabled_engines = filter(lambda engine: engine["_isEnabled"], engines) print enabled_engines if not enabled_engines: return HttpResponse(json.dumps(enabled_engines)) vector_size = len(enabled_engines) rpms = variable(vector_size, "rpms") fixed_engine_costs = matrix([float(EngineType.objects.get(pk=engine["_engineTypeId"]).fixed_engine_cost) for engine in enabled_engines]) linear_engine_costs = matrix([float(EngineType.objects.get(pk=engine["_engineTypeId"]).linear_engine_cost) for engine in enabled_engines]) fixed_energy_outputs = matrix([float(EngineType.objects.get(pk=int(engine["_engineTypeId"])).fixed_energy_output) for engine in enabled_engines]) linear_energy_outputs = matrix([float(EngineType.objects.get(pk=int(engine["_engineTypeId"])).linear_energy_output) for engine in enabled_engines]) minimum_rpms = matrix([float(EngineType.objects.get(pk=engine["_engineTypeId"]).minimum_rpm) for engine in enabled_engines]) maximum_rpms = matrix([float(EngineType.objects.get(pk=engine["_engineTypeId"]).maximum_rpm) for engine in enabled_engines]) demand_constraint = ((float(demand) - dot(rpms, linear_energy_outputs) - sum(fixed_energy_outputs)) <= 0) maximum_rpm_constraint = ((rpms - maximum_rpms) <= 0) minimum_rpm_constraint = ((rpms - minimum_rpms) >= 0) constraints = [demand_constraint, maximum_rpm_constraint, minimum_rpm_constraint] objective_function = op((dot(linear_engine_costs, rpms) + sum(fixed_engine_costs)), constraints) objective_function.solve() print rpms.value rpmsIndex = 0 for engine in engines: engine["_rpm"] = 0 engine["_energyOutput"] = 0 if engine["_isEnabled"]: print rpms if rpms.value: engine["_rpm"] = rpms.value[rpmsIndex] rpmsIndex += 1 else: engine["_rpm"] = float(EngineType.objects.get(pk=engine["_engineTypeId"]).maximum_rpm) energy_output_per_rpm = float(EngineType.objects.get(pk=engine["_engineTypeId"]).linear_energy_output) base_energy_output = float(EngineType.objects.get(pk=engine["_engineTypeId"]).fixed_energy_output) engine["_energyOutput"] = energy_output_per_rpm * float(engine["_rpm"]) + base_energy_output return HttpResponse(json.dumps(engines));
def work(): x = variable(range(len(table)), 'x') z = (30 * x[0] + 1 * x[1]) # Функция цели mass1 = (90 * x[0] + 5 * x[1] <= 10000) # "1" mass2 = (3 * x[0] - x[1] == 0) # "2" x_non_negative = (x >= 0) # "3" problem = op(z, [mass1, mass2, x_non_negative]) problem.solve(solver='glpk') problem.status print("Прибыль:") print(abs(problem.objective.value()[0])) print("Результат:") print(x.value) stop = time.time() print("Время :") print(stop - start)
def linear_programming(): workbook = xlrd.open_workbook('Zadachka.xlsx') worksheet = workbook.sheet_by_index(0) alpha = get_values(worksheet, 4, 1) beta = get_values(worksheet, 4, 2) v = get_values(worksheet, 4, 3) V = get_values(worksheet, 4, 4) teta = get_values(worksheet, 4, 5, string=True) F = worksheet.cell(0, 1).value T = worksheet.cell(1, 1).value k = [a / b for a, b in zip(V, v)] n = len(alpha) x = Symbol('x') teta_solved = [ float(integrate(eval(teta[i]), (x, 0, 1))) for i in range(0, n) ] x = variable(n, 'x') sum_var1 = 0 sum_var2 = 0 for i in range(0, n): sum_var1 += x[i] * v[i] * beta[i] for i in range(0, n): sum_var2 += x[i] * v[i] * alpha[i] z = -(sum_var1 - sum_var2) # Функция цели "11.1" restrict1 = (sum_var2 <= F) # "11.2" restrict2 = ([0 <= x[i] <= k[i] for i in range(0, n)]) # "11.3" restrict3 = ([ x[i] * v[i] <= teta_solved[i] <= v[i] * (x[i] + 1) for i in range(0, n) ]) # "11.4" x_non_negative = (x >= 0) problem = op(z, [restrict1, *restrict2, *restrict3, x_non_negative]) problem.solve('glpk') print(problem.status) print("Максимум целевой функции:") print(abs(problem.objective.value()[0])) print("X:") print(x.value)
def solve_task(c1, c2, b1, b2, b3, b4, a1, a2): # x1 = pulp.LpVariable("x1", lowBound=0) # x2 = pulp.LpVariable("x2", lowBound=0) # problem = pulp.LpProblem('0', pulp.LpMaximize) # problem += c1 * x1 + c2 * x2, "Функция цели" # problem += a1.pop() * x1 + a2.pop() * x2 <= b1, "1" # problem += a1.pop() * x1 + a2.pop() * x2 <= b2, "2" # problem += a1.pop() * x1 + a2.pop() * x2 <= b3, "3" # problem += a1.pop() * x1 + a2.pop() * x2 <= b4, "4" # problem.solve() x = variable(2, 'x') z = -(c1 * x[0] + c2 * x[1]) mass1 = (a1.pop() * x[0] + a2.pop() * x[1] <= b1) mass2 = (a1.pop() * x[0] + a2.pop() * x[1] <= b2) mass3 = (a1.pop() * x[0] + a2.pop() * x[1] <= b3) mass4 = (a1.pop() * x[0] + a2.pop() * x[1] <= b4) x_non_negative = (x >= 0) problem = op(z, [mass1, mass2, mass3, mass4, x_non_negative]) problem.solve(solver='glpk') return problem, x
def __init__(self): self.x = variable(25, 'x') # Стоимость доставки self.c = [ 7, 1, 1, 4, 2, 3, 5, 6, 6, 8, 8, 2, 4, 3, 7, 3, 4, 2, 5, 8, 8, 1, 4, 7, 6 ] self.z = (self.c[0] * self.x[0] + self.c[1] * self.x[1] + self.c[2] * self.x[2] + self.c[3] * self.x[3] + self.c[4] * self.x[4] + self.c[5] * self.x[5] + self.c[6] * self.x[6] + self.c[7] * self.x[7] + self.c[8] * self.x[8] + self.c[9] * self.x[9] + self.c[10] * self.x[10] + self.c[11] * self.x[11] + self.c[12] * self.x[12] + self.c[13] * self.x[13] + self.c[14] * self.x[14] + self.c[15] * self.x[15] + self.c[16] * self.x[16] + self.c[17] * self.x[17] + self.c[18] * self.x[18] + self.c[19] * self.x[19] + self.c[20] * self.x[20] + self.c[21] * self.x[21] + self.c[22] * self.x[22] + self.c[23] * self.x[23] + self.c[24] * self.x[24])
def test_linear(fe): fi = open(fe, "rU") r = [] while True: try: h1, s1 = analyze(fi.next()) h2, s2 = analyze(fi.next()) except: break r.append(h2 - h1) if s1 < s2: r[-1] *= -1 A = matrix(r) C = matrix([1.0] * A.size[0]) x = variable(A.size[1]) f = matrix([0.0] * A.size[1]).T prob = op(f * x, [A * x + C <= 0]) prob.solve(solver = "glpk") if prob.status == "optimal": return True, x.value else: return False, None
def calculate_distance_eqopp_2d(data_tuple, theta, tau, eps = 0): data = data_tuple[0] # print(data) data_sensitive = data_tuple[1] data_label = data_tuple[2] N = np.shape(data_sensitive)[0] emp_marginals = np.reshape(get_marginals(sensitives=data_sensitive, target=data_label), [2, 2]) w = -math.log(1./tau - 1) d = distance_func(theta,data,w) C = C_classifier(data, theta, tau) phi1 = phi_function(data_sensitive, data_label, emp_marginals) phi0 = phi_function_0(data_sensitive, data_label, emp_marginals) C_phi1 = np.array(phi1)*(1-2*np.array(C)) C_phi0 = np.array(phi0)*(1-2*np.array(C)) b_phi1 = - np.sum(np.array(phi1)*np.array(C)) b_phi0 = - np.sum(np.array(phi0)*np.array(C)) p = variable(N) opt_A1 = matrix(C_phi1) opt_A0 = matrix(C_phi0) equality1 = (dot(opt_A1,p) ==matrix(b_phi1) ) equality0 = (dot(opt_A0,p) ==matrix(b_phi0) ) opt_c = matrix(d) lp = op(dot(opt_c,p), [equality1,equality0,p>=0,p<=1]) lp.solve() return(lp.objective.value()[0])
def solve_lp(a, b, c): """ >>> a = matrix([[-5., 3., 1., 8., 1.], ... [ 5., 5., 4., 6., 1.], ... [-4., 6., 0., 5., 1.], ... [-1.,-1.,-1.,-1., 0.], ... [ 1., 1., 1., 1., 0.], ... [-1., 0., 0., 0., 0.], ... [ 0.,-1., 0., 0., 0.], ... [ 0., 0.,-1., 0., 0.], ... [ 0., 0., 0.,-1., 0.]]) >>> b = matrix([0.,0.,0.,0.,1.]) >>> c = matrix([0.,0.,0., 1.,-1.,0.,0.,0.,0.]) >>> solve_lp(a, b, c) """ variables = c.size[0] x = variable(variables, 'x') eq = (a * x == b) ineq = (x >= 0) lp = op(dot(c, x), [eq, ineq]) lp.solve(solver='glpk') return (lp.objective.value(), x.value)
def optimize_min(products): if not products: return [] n = len(products) x = variable(n, 'x') # initialize parameters z = products[0].default_price * x[0] proteins = products[0].proteins * x[0] hydrocarbons = products[0].hydrocarbons * x[0] fat = products[0].fat * x[0] minerals = products[0].minerals * x[0] # construct parameters for i in range(1, n): z = z + products[i].default_price * x[i] proteins = proteins + products[i].proteins * x[i] hydrocarbons = hydrocarbons + products[i].hydrocarbons * x[i] fat = fat + products[i].fat * x[i] minerals = minerals + products[i].minerals * x[i] mass1 = (-proteins <= -118) mass2 = (-hydrocarbons <= -500) mass3 = (-fat <= -56) mass4 = (-minerals <= -28) x_non_negative = (x >= 0) problem = op(z, [mass1, mass2, mass3, mass4, x_non_negative]) problem.solve(solver='glpk') problem.status result = [] for i in range(0, n): result.append(round(1000 * x.value[i], 1)) return result
#solvers.options['show_progress'] = 0 try: import numpy, pylab except ImportError: pylab_installed = False else: pylab_installed = True m, n = 100, 30 A = normal(m,n) b = normal(m,1) b /= (1.1 * max(abs(b))) # Make x = 0 feasible for log barrier. # l1 approximation # # minimize || A*x + b ||_1 x = variable(n) op(sum(abs(A*x+b))).solve() x1 = x.value if pylab_installed: pylab.figure(1, facecolor='w', figsize=(10,10)) pylab.subplot(411) nbins = 100 bins = [-1.5 + 3.0/(nbins-1)*k for k in range(nbins)] pylab.hist( A*x1+b , numpy.array(bins)) nopts = 200 xs = -1.5 + 3.0/(nopts-1) * matrix(list(range(nopts))) pylab.plot(xs, (35.0/1.5) * abs(xs), 'g-') pylab.axis([-1.5, 1.5, 0, 40]) pylab.ylabel('l1') pylab.title('Penalty function approximation (fig. 6.2)')
def test_exceptions(self): with self.assertRaises(TypeError): x = variable(0)
# The small LP of section 10.4 (Optimization problems). from cvxopt import matrix from cvxopt.modeling import variable, op, dot x = variable() y = variable() c1 = ( 2*x+y <= 3 ) c2 = ( x+2*y <= 3 ) c3 = ( x >= 0 ) c4 = ( y >= 0 ) lp1 = op(-4*x-5*y, [c1,c2,c3,c4]) lp1.solve() print("\nstatus: %s" %lp1.status) print("optimal value: %f" %lp1.objective.value()[0]) print("optimal x: %f" %x.value[0]) print("optimal y: %f" %y.value[0]) print("optimal multiplier for 1st constraint: %f" %c1.multiplier.value[0]) print("optimal multiplier for 2nd constraint: %f" %c2.multiplier.value[0]) print("optimal multiplier for 3rd constraint: %f" %c3.multiplier.value[0]) print("optimal multiplier for 4th constraint: %f\n" %c4.multiplier.value[0]) x = variable(2) A = matrix([[2.,1.,-1.,0.], [1.,2.,0.,-1.]]) b = matrix([3.,3.,0.,0.]) c = matrix([-4.,-5.]) ineq = ( A*x <= b ) lp2 = op(dot(c,x), ineq) lp2.solve() print("\nstatus: %s" %lp2.status)
def frank_wolfe(objective, gradient, A, b, initial_x, maxiters=2000, tol=1e-4, clean=True, verbose=None): """ Uses the Frank--Wolfe algorithm to minimize the convex objective. Minimization is subject to the linear equality constraint: A x = b. Assumes x should be nonnegative. Parameters ---------- objective : callable The objective function. It would receive a ``cvxopt`` matrix for the input `x` and return the value of the objective function. gradient : callable The gradient function. It should receive a ``cvxopt`` matrix for the input `x` and return the value of the gradient evaluated at `x`. A : matrix A ``cvxopt`` matrix specifying the LHS linear equality constraints. b : matrix A ``cvxopt`` matrix specifying the RHS linear equality constraints. initial_x : matrix A ``cvxopt`` matrix specifying the initial `x` to use. maxiters : int The maximum number of iterations to perform. If convergence was not reached after the last iteration, a warning is issued and the current value of `x` is returned. tol : float The tolerance used to determine when we have converged to the optimum. clean : bool Occasionally, the iteration process will take nonnegative values to be ever so slightly negative. If ``True``, then we forcibly make such values equal to zero and renormalize the vector. This is an application specific decision and is probably not more generally useful. verbose : int An integer representing the logging level ala the ``logging`` module. If `None`, then (effectively) the log level is set to `WARNING`. For a bit more information, set this to `logging.INFO`. For a bit less, set this to `logging.ERROR`, or perhaps 100. """ # Function level import to avoid circular import. from dit.algorithms.optutil import op_runner # Function level import to keep cvxopt dependency optional. # All variables should be cvxopt variables, not NumPy arrays from cvxopt import matrix from cvxopt.modeling import variable # Set up a custom logger. logger = basic_logger('dit.frankwolfe', verbose) # Set cvx info level based on logging.DEBUG level. if logger.isEnabledFor(logging.DEBUG): show_progress = True else: show_progress = False assert (A.size[1] == initial_x.size[0]) n = initial_x.size[0] x = initial_x xdiff = 0 TOL = 1e-7 verbosechunk = maxiters / 10 for i in range(maxiters): obj = objective(x) grad = gradient(x) xbar = variable(n) new_objective = grad.T * xbar constraints = [] constraints.append((xbar >= 0)) constraints.append((-TOL <= A * xbar - b)) constraints.append((A * xbar - b <= TOL)) logger.debug('FW Iteration: {}'.format(i)) opt = op_runner(new_objective, constraints, show_progress=show_progress) if opt.status != 'optimal': msg = '\tFrank-Wolfe: Did not find optimal direction on ' msg += 'iteration {}: {}' msg = msg.format(i, opt.status) logger.info(msg) # Calculate optimality gap xbar_opt = opt.variables()[0].value opt_bd = grad.T * (xbar_opt - x) msg = "i={:6} obj={:10.7f} opt_bd={:10.7f} xdiff={:12.10f}" if logger.isEnabledFor(logging.DEBUG): logger.debug(msg.format(i, obj, opt_bd[0, 0], xdiff)) logger.debug("") elif i % verbosechunk == 0: logger.info(msg.format(i, obj, opt_bd[0, 0], xdiff)) xnew = (i * x + 2 * xbar_opt) / (i + 2) xdiff = np.linalg.norm(xnew - x) x = xnew if xdiff < tol: obj = objective(x) break else: msg = "Only converged to xdiff={:12.10f} after {} iterations. " msg += "Desired: {}" logger.warn(msg.format(xdiff, maxiters, tol)) xopt = np.array(x) if clean: xopt[np.abs(xopt) < tol] = 0 xopt /= xopt.sum() return xopt, obj
# Inequality description G*x <= h with h = 1 G, h = matrix(0.0, (m,2)), matrix(0.0, (m,1)) G = (X[:m,:] - X[1:,:]) * matrix([0., -1., 1., 0.], (2,2)) h = (G * X.T)[::m+1] G = mul(h[:,[0,0]]**-1, G) h = matrix(1.0, (m,1)) # Chebyshev center # # maximizse R # subject to gk'*xc + R*||gk||_2 <= hk, k=1,...,m # R >= 0 R = variable() xc = variable(2) op(-R, [ G[k,:]*xc + R*blas.nrm2(G[k,:]) <= h[k] for k in range(m) ] + [ R >= 0] ).solve() R = R.value xc = xc.value if pylab_installed: pylab.figure(1, facecolor='w') # polyhedron for k in range(m): edge = X[[k,k+1],:] + 0.1 * matrix([1., 0., 0., -1.], (2,2)) * \ (X[2*[k],:] - X[2*[k+1],:]) pylab.plot(edge[:,0], edge[:,1], 'k')
def variable_range(G, h, A=None, b=None, progress=False): """ Determines the net upper and lower constraints on x given that Gx <= h and Ax == b. Parameters ---------- G: pandas.DataFrame Array that specifies Gx <= h. h: pandas.Series The limits specifying Gx <= h. A: pandas.DataFrame Array that specifies Ax == b. b: pandas.Series The limits specifying Ax == b. progress: bool True if detailed progress text from optimization should be shown. Returns ------- ranges: pd.DataFrame A dataframe with columns indicating the lowest and highest values each basis variable can take. """ # Aligning input h = h.ix[G.index] if h.isnull().values.any() or G.isnull().values.any(): msg = 'Row indeces of G and h must match and contain no NaN entries.' omfa.logger.error(msg) raise ValueError(msg) if sum([A is None, b is None]) == 1: msg = 'If one of A or b is specified, the other one must be too' omfa.logger.error(msg) raise ValueError(msg) if A is not None: b = b.ix[A.index] if set(A.columns) != set(G.columns): msg = 'A and G must have the same column names' omfa.logger.error(msg) raise ValueError(msg) if progress: solvers.options['show_progress'] = True else: solvers.options['show_progress'] = False # Setting up LP problem m_G, n_G = G.shape x = variable(n_G) G_opt = matrix(np.array(G, dtype=np.float64)) h_opt = matrix(np.array(h, dtype=np.float64)) constraints = [G_opt[k,:]*x <= h_opt[k] for k in range(m_G)] if A is not None: m_A, n_A = A.shape A_opt = matrix(np.array(A, dtype=np.float64)) b_opt = matrix(np.array(b, dtype=np.float64)) constraints = constraints + [A_opt[k,:]*x == b_opt[k] for k in range(m_A)] # Initializing output ranges = pd.DataFrame(None, index=G.columns, columns=['low', 'high']) # Looping through the individual objectives for basis in range(n_G): # Minimum model = op(x[basis], constraints) model.solve() status = model.status if 'optimal' in model.status: ranges.ix[basis, 'low'] = x.value[basis] elif 'dual infeasible' in model.status: ranges.ix[basis, 'low'] = -float('inf') elif 'primal infeasible' in model.status: msg = 'Specified constraints are infeasible.' omfa.logger.error(msg) raise ValueError(msg) elif 'unknown' in model.status: msg = 'Minimum not found due to unknown optimization error.' omfa.logger.warn(msg) ranges.ix[basis, 'low'] = None # Maximum model = op(-x[basis], constraints) model.solve() status = model.status if 'optimal' in model.status: ranges.ix[basis, 'high'] = x.value[basis] elif 'dual infeasible' in model.status: ranges.ix[basis, 'high'] = -float('inf') elif 'primal infeasible' in model.status: msg = 'Specified constraints are infeasible.' omfa.logger.error(msg) raise ValueError(msg) elif 'unknown' in model.status: msg = 'Minimum not found due to unknown optimization error.' omfa.logger.warn(msg) ranges.ix[basis, 'low'] = None return(ranges)
def check_feasibility(G, h, progress=False, silent=False): """ Determines if specified constraints of the form Gx <= h are feasible. Parameters ---------- G: pandas.DataFrame Array that specifies Gx <= h. h: pandas.Series The limits specifying Gx <= h. progress: bool True if detailed progress text from optimization should be shown. silent: bool True if diagnostic messages should bre printed (through logger.warn). Returns ------- check_passed: bool True if constraints are feasible, False if they aren't. """ # Aligning input h = h.ix[G.index] if h.isnull().values.any() or G.isnull().values.any(): msg = 'Row indeces of G and h must match and contain no NaN entries.' omfa.logger.error(msg) raise ValueError(msg) if progress: solvers.options['show_progress'] = True else: solvers.options['show_progress'] = False # Setting up LP problem m, n = G.shape x = variable(n) G_opt = matrix(np.array(G, dtype=np.float64)) h_opt = matrix(np.array(h, dtype=np.float64)) inequality_constraints = [G_opt[k,:]*x <= h_opt[k] for k in range(m)] # Arbitrary, simple minimization problem model = op(xsum(abs(x)), inequality_constraints) model.solve() if 'primal infeasible' in model.status: msg = 'The problem is infeasible.' omfa.logger.warn(msg) check_passed = False elif 'dual infeasible' in model.status: msg = 'The problem is unbounded.' omfa.logger.warn(msg) check_passed = False elif 'unknown' in model.status: msg = 'An unknown optimization error occured.' sol = ('\nSome constraints may be near parallel or the problem ' 'needs scaling.') omfa.logger.warn(msg + sol) check_passed = False else: check_passed = True return(check_passed)
try: import pylab except ImportError: pylab_installed = False else: pylab_installed = True data = pickle.load(open("linsep.bin", 'rb')) X, Y = data['X'], data['Y'] n, N, M = X.size[0], X.size[1], Y.size[1] # Via linear programming. # # minimize sum(u) + sum(v) # subject to a'*X - b >= 1 - u # a'*Y - b <= -1 + v # u >= 0, v >= 0 a, b = variable(2), variable() u, v = variable(N), variable(M) op( sum(u)+sum(v), [X.T*a-b >= 1-u, Y.T*a-b <= -1+v, u>=0, v>=0] ).solve() a = a.value b = b.value if pylab_installed: pylab.figure(1, facecolor='w', figsize=(5,5)) pts = matrix([-10.0, 10.0], (2,1)) pylab.plot(X[0,:], X[1,:], 'ow', mec = 'k'), pylab.plot(Y[0,:], Y[1,:], 'ok', pts, (b - a[0]*pts)/a[1], '-r', pts, (b+1.0 - a[0]*pts)/a[1], '--r', pts, (b-1.0 - a[0]*pts)/a[1], '--r' ) pylab.title('Separation via linear programming (fig. 8.10)')
def chebyshev_center(G, h, progress=False): """ Calculates the center point of the largest sphere that can fit within constraints specified by Gx <= h. Parameters ---------- G: pandas.DataFrame Array that specifies Gx <= h. h: pandas.Series The limits specifying Gx <= h. progress: bool True if detailed progress text from optimization should be shown. Returns ------- x: pd.Series Centroid with index names equal to the column names of G. """ # Aligning input h = h.ix[G.index] if h.isnull().values.any() or G.isnull().values.any(): msg = 'Row indeces of G and h must match and contain no NaN entries.' omfa.logger.error(msg) raise ValueError(msg) if progress: solvers.options['show_progress'] = True else: solvers.options['show_progress'] = False # Setting up LP problem m, n = G.shape R = variable() x = variable(n) G_opt = matrix(np.array(G, dtype=np.float64)) h_opt = matrix(np.array(h, dtype=np.float64)) inequality_constraints = [G_opt[k,:]*x + R*blas.nrm2(G_opt[k,:]) <= h_opt[k] for k in range(m)] model = op(-R, inequality_constraints + [ R >= 0] ) model.solve() x = pd.Series(x.value, index=G.columns) # Checking output if model.status != 'optimal': if all(G.dot(x) <= h): msg = ('Centroid was not found, ' 'but the last calculated point is feasible.') omfa.logger.warn(msg) else: msg = 'Optimization calculatoin failed on a non-feasible point.' sol = '\nSet progress=True for more details.' omfa.logger.error(msg + sol) raise RuntimeError(msg + sol) return(x)
pylab.contour(pylab.array(X), pylab.array(Y), pylab.array(utility(X,Y)), [.1*(k+1) for k in range(9)], colors='k') pylab.xlabel('x1') pylab.ylabel('x2') pylab.title('Goods baskets and utility function (fig. 6.25)') #print("Close figure to start analysis.") #pylab.show() # P are basket indices in order of increasing preference l = list(zip(utility(B[0,:], B[1,:]), range(m))) l.sort() P = [ e[1] for e in l ] # baskets with known preference relations u = variable(m) gx = variable(m) gy = variable(m) # comparison basket at (.5, .5) has utility 0 gxc = variable(1) gyc = variable(1) monotonicity = [ gx >= 0, gy >= 0, gxc >= 0, gyc >= 0 ] preferences = [ u[P[j+1]] >= u[P[j]] + 1.0 for j in range(m-1) ] concavity = [ u[j] <= u[i] + gx[i] * ( B[0,j] - B[0,i] ) + gy[i] * ( B[1,j] - B[1,i] ) for i in range(m) for j in range(m) ] concavity += [ 0 <= u[i] + gx[i] * ( 0.5 - B[0,i] ) + gy[i] * ( 0.5 - B[1,i] ) for i in range(m) ] concavity += [ u[j] <= gxc * ( B[0,j] - 0.5 ) + gyc * ( B[1,j] - 0.5 ) for j in range(m) ]
from cvxopt.modeling import variable, op x,y = variable(), variable() c1 = (2*x+y <= 3) c2 = (x+2*y <= 3) c3 = (x >= 0) c4 = (y >= 0) lp1 = op(-4*x-5*y, [c1,c2,c3,c4]) lp1.solve()
# The robust LP example of section 10.5 (Examples). from cvxopt import normal, uniform from cvxopt.modeling import variable, dot, op, sum from cvxopt.blas import nrm2 m, n = 500, 100 A = normal(m,n) b = uniform(m) c = normal(n) x = variable(n) op(dot(c,x), A*x+sum(abs(x)) <= b).solve() x2 = variable(n) y = variable(n) op(dot(c,x2), [A*x2+sum(y) <= b, -y <= x2, x2 <= y]).solve() print("\nDifference between two solutions %e" %nrm2(x.value - x2.value))
# The 1-norm support vector classifier of section 10.5 (Examples). from cvxopt import normal, setseed from cvxopt.modeling import variable, op, max, sum from cvxopt.blas import nrm2 m, n = 500, 100 A = normal(m,n) x = variable(A.size[1],'x') u = variable(A.size[0],'u') op(sum(abs(x)) + sum(u), [A*x >= 1-u, u >= 0]).solve() x2 = variable(A.size[1],'x') op(sum(abs(x2)) + sum(max(0, 1 - A*x2))).solve() print("\nDifference between two solutions: %e" %nrm2(x.value - x2.value))
# LS fit of 5th order polynomial # # minimize ||A*x - y ||_2 n = 6 A = matrix( [[t**k] for k in range(n)] ) xls = +y lapack.gels(+A,xls) xls = xls[:n] # Chebyshev fit of 5th order polynomial # # minimize ||A*x - y ||_inf xinf = variable(n) op( max(abs(A*xinf - y)) ).solve() xinf = xinf.value if pylab_installed: pylab.figure(1, facecolor='w') pylab.plot(t, y, 'bo', mfc='w', mec='b') nopts = 1000 ts = -1.1 + (1.1 - (-1.1))/nopts * matrix(list(range(nopts)), tc='d') yls = sum( xls[k] * ts**k for k in range(n) ) yinf = sum( xinf[k] * ts**k for k in range(n) ) pylab.plot(ts,yls,'g-', ts, yinf, '--r') pylab.axis([-1.1, 1.1, -0.1, 0.25]) pylab.xlabel('u') pylab.ylabel('p(u)') pylab.title('Polynomial fitting (fig. 6.19)')
# The norm and penalty approximation problems of section 10.5 (Examples). from cvxopt import normal, setseed from cvxopt.modeling import variable, op, max, sum setseed(0) m, n = 500, 100 A = normal(m, n) b = normal(m) x1 = variable(n) prob1 = op(max(abs(A * x1 + b))) prob1.solve() x2 = variable(n) prob2 = op(sum(abs(A * x2 + b))) prob2.solve() x3 = variable(n) prob3 = op(sum(max(0, abs(A * x3 + b) - 0.75, 2 * abs(A * x3 + b) - 2.25))) prob3.solve() try: import pylab except ImportError: pass else: pylab.subplot(311) pylab.hist(A * x1.value + b, m // 5) pylab.subplot(312) pylab.hist(A * x2.value + b, m // 5)
def initial_point(dist, k, A=None, b=None, isolated=None, **kwargs): """ Find an initial point in the interior of the feasible set. """ from cvxopt import matrix from cvxopt.modeling import variable if isolated is None: variables = isolate_zeros(dist, k) else: variables = isolated if A is None or b is None: A, b = marginal_constraints(dist, k) # Reduce the size of A so that only nonzero elements are searched. # Also make it full rank. Asmall = A[:, variables.nonzero] Asmall, b, rank = as_full_rank(Asmall, b) Asmall = matrix(Asmall) b = matrix(b) else: # Assume they are already CVXOPT matrices if A.size[1] == len(variables.nonzero): Asmall = A else: msg = 'A must be the reduced equality constraint matrix.' raise Exception(msg) n = len(variables.nonzero) x = variable(n) t = variable() tols = [1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3] for tol in tols: constraints = [] constraints.append( (-tol <= Asmall * x - b) ) constraints.append( ( Asmall * x - b <= tol) ) constraints.append( (x >= t) ) # Objective to minimize objective = -t opt = op_runner(objective, constraints, **kwargs) if opt.status == 'optimal': #print("Found initial point with tol={}".format(tol)) break else: msg = 'Could not find valid initial point: {}' raise Exception(msg.format(opt.status)) # Grab the optimized x optvariables = opt.variables() if len(optvariables[0]) == n: xopt = optvariables[0].value else: xopt = optvariables[1].value # Turn values close to zero to be exactly equal to zero. xopt = np.array(xopt)[:,0] xopt[np.abs(xopt) < tol] = 0 xopt /= xopt.sum() # Do not build the full vector since this is input to the reduced # optimization problem. #xx = np.zeros(len(dist.pmf)) #xx[variables.nonzero] = xopt return xopt, opt
def initial_dist(self): """ Find an initial point in the interior of the feasible set. """ from cvxopt import matrix from cvxopt.modeling import variable A = self.A b = self.b # Assume they are already CVXOPT matrices if self.vartypes and A.size[1] != len(self.vartypes.free): msg = 'A must be the reduced equality constraint matrix.' raise Exception(msg) # Set cvx info level based on logging.INFO level. if self.logger.isEnabledFor(logging.INFO): show_progress = True else: show_progress = False n = len(self.vartypes.free) x = variable(n) t = variable() tols = [1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3] for tol in tols: constraints = [] constraints.append((-tol <= A * x - b)) constraints.append((A * x - b <= tol)) constraints.append((x >= t)) # Objective to minimize objective = -t opt = op_runner(objective, constraints, show_progress=show_progress) if opt.status == 'optimal': #print("Found initial point with tol={}".format(tol)) break else: msg = 'Could not find valid initial point: {}' raise Exception(msg.format(opt.status)) # Grab the optimized x. Perhaps there is a more reliable way to get # x rather than t. For now,w e check the length. optvariables = opt.variables() if len(optvariables[0]) == n: xopt = optvariables[0].value else: xopt = optvariables[1].value # Turn values close to zero to be exactly equal to zero. xopt = np.array(xopt)[:, 0] xopt[np.abs(xopt) < tol] = 0 # Normalize properly accounting for fixed nonzero values. xopt /= xopt.sum() xopt *= self.normalization # Do not build the full vector since this is input to the reduced # optimization problem. #xx = np.zeros(len(dist.pmf)) #xx[variables.nonzero] = xopt return xopt, opt