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 _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 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 resolve_pddd(self) -> Resultado: """ Resolve um problema de planejamento energético através da PDDD. """ # Erros e condição de parada it = 0 self.tol = 1e-3 self.z_sup = [] self.z_inf = [] logger.info("# RESOLVENDO PROBLEMA DE PDDD #") logger.info("X----X-------------------X-------------------X") logger.info(" IT Z_SUP Z_INF ") while True: for j in range(self.cfg.n_periodos): nos_periodo = self.arvore.nos_por_periodo[j] for k in range(nos_periodo): # Monta e resolve o PL do nó (exceto a partir da # segunda iteração, no período 1 - pois a backward é igual) if it == 0 or j > 0: self.__monta_pl(self.arvore, j, k) self.pl = op(self.func_objetivo, self.cons) self.pl.solve("dense", "glpk") # Armazena as saídas obtidas no PL no objeto nó self.__armazena_saidas(self.arvore, j, k) # Condição de saída por convergência it += 1 if self.__verifica_convergencia(it): break # Condição de saída por iterações if it >= self.cfg.max_iter: logger.warning(" LIMITE DE ITERAÇÕES ATINGIDO!") break # Executa a backward para cada nó for j in range(self.cfg.n_periodos - 1, -1, -1): for k in range(self.arvore.nos_por_periodo[j] - 1, -1, -1): # Monta e resolve o PL do nó (não resolve o último período) if j != self.cfg.n_periodos - 1: self.__monta_pl(self.arvore, j, k) self.pl = op(self.func_objetivo, self.cons) self.pl.solve("dense", "glpk") # Armazena as saídas obtidas no PL no objeto nó self.__armazena_saidas(self.arvore, j, k) # Gera um novo corte para o nó self.__cria_corte(j, k) # Terminando o loop do método, organiza e retorna os resultados logger.info("X----X-------------------X-------------------X") self.__simulacao_final() logger.info("# FIM DA SOLUÇÃO #") logger.info("----------------------------------------") return Resultado(self.cfg, self.uhes, self.utes, self.sim_final.organiza_cenarios(), self.z_sup, self.z_inf, [], self.__organiza_cortes())
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 _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 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 op_runner(objective, constraints, **kwargs): """ Minimize the objective specified by the constraints. This safely let's you pass options to the solver and restores their values once the optimization process has completed. The objective must be linear in the variables. This uses cvxopt.modeling. """ from cvxopt.solvers import options from cvxopt.modeling import variable, op old_options = options.copy() opt = op(objective, constraints) try: options.clear() options.update(kwargs) # Ignore 0 log 0 warnings. with np.errstate(divide='ignore', invalid='ignore'): opt.solve() except: raise finally: options.clear() options.update(old_options) return opt
def __simulacao_final(self): """ """ logger.info(" # SIMULAÇÂO FINAL #") logger.info("X-------------------X-------------------X") logger.info(" Z_SUP Z_INF ") self.sim_final.monta_simulacao_final(self.arvore) for j in range(self.cfg.n_periodos): nos_periodo = self.arvore.nos_por_periodo[j] for k in range(nos_periodo): self.__monta_pl(self.sim_final, j, k) self.pl = op(self.func_objetivo, self.cons) self.pl.solve("dense", "glpk") # Armazena as saídas obtidas no PL no objeto nó self.__armazena_saidas(self.sim_final, j, k) z_sup = 0.0 z_inf = 0.0 for j in range(self.cfg.n_periodos): nos_periodo = self.arvore.nos_por_periodo[j] for k in range(nos_periodo): no = self.arvore.arvore[j][k] z_sup += (1. / nos_periodo) * no.custo_imediato if j == 0: z_inf = no.custo_total self.z_sup.append(z_sup) self.z_inf.append(z_inf) logger.info(" {:19.6f} {:19.6f}".format(z_sup, z_inf)) logger.info("X-------------------X-------------------X")
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 maximize_fapx_cvxopt( node, problem, scoop=False): ''' Finds the value of y solving maximize sum(fapx(x)) subject to: constr l <= x <= u fapx is calculated by approximating the functions given in fs as the concave envelope of the function that is tight, for each coordinate i, at the points in tight[i] fs should be a list of tuples containing the function and its derivative corresponding to each coordinate of the vector x. Returns a dict containing the optimal variable y as a list, the optimal value of the approximate objective, and the value of sum(f(x)) at x. scoop = True optionally dumps all problem parameters into a file which can be parsed and solved using the scoop second order cone modeling language ''' n = len(node.l); l = matrix(node.l); u = matrix(node.u) x = problem.variable constr = problem.constr # add box constraints box = [x[i]<=u[i] for i in xrange(n)] + [x[i]>=l[i] for i in xrange(n)] # find approximation to concave envelope of each function (fapx,slopes,offsets,fapxs) = utilities.get_fapx(node.tight,problem.fs,l,u,y=x) if problem.check_z: utilities.check_z(problem,node,fapx,x) if scoop: utilities.scoop(p,node,slopes,offsets) obj = sum( fapx ) o = op(obj,constr + box) try: o.solve(solver = 'glpk') except: o.solve() if not o.status == 'optimal': if o.status == 'unknown': raise ImportError('Unable to solve subproblem. Please try again after installing cvxopt with glpk binding.') else: # This node is dead, since the problem is infeasible return False else: # find the difference between the fapx and f for each coordinate i fi = numpy.array([problem.fs[i][0](x.value[i]) for i in range(n)]) fapxi = numpy.array([list(-fun.value())[0] for fun in fapx]) #if verbose: print 'fi',fi,'fapxi',fapxi maxdiff_index = numpy.argmax( fapxi - fi ) results = {'x': list(x.value), 'fapx': -list(obj.value())[0], 'f': float(sum(fi)), 'maxdiff_index': maxdiff_index} return results
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 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 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 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 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 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 _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 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 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 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 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)
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') # 1000 points on the unit circle nopts = 1000
def test_loadfile(self): lp = op() lp.fromfile(os.path.join(os.path.dirname(__file__),"boeing2.mps")) lp.solve() self.assertTrue(lp.status == 'optimal')
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)
# 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) ] preferred, rejected, neutral = [], [], [] for k in range(m): p = op(-u[k], monotonicity + preferences + concavity) p.solve() if p.status == 'optimal' and p.objective.value()[0] > 0: rejected += [k] print("Basket (%1.2f, %1.2f) rejected." %(B[0,k],B[1,k])) else: p = op(u[k], monotonicity + preferences + concavity) p.solve() if p.status == 'optimal' and p.objective.value()[0] > 0: print("Basket (%1.2f, %1.2f) preferred." %(B[0,k],B[1,k])) preferred += [k] else: print("No conclusion about basket (%1.2f, %1.2f)." \ %(B[0,k],B[1,k])) neutral += [k]
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)
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()
# 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 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)
# 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)
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)') pylab.axis([-10, 10, -10, 10]) pylab.axis('off') pylab.xticks([])
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)')
# 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))
# change this in from line # 922 of coneprog.py in /usr/lib/python2.7/dist-packages/cvxopt # if iters == 0: # print("%2s%16s%16s%10s%10s%10s%16s" %("it","pcost", "dcost", "gap", "pres", "dres", "k/t")) # print("%2d%16.8e%16.8e%10.3e%10.3e%10.3e%16.8e" %(iters, pcost, dcost, gap, pres, dres, kappa/tau)) from cvxopt.modeling import op lp = op() # lp.fromfile('afiro.mps') # lp.fromfile('adlittle.mps') lp.fromfile("share1b.mps") lp.solve() res = {} for i, v in enumerate(lp.variables()): # print v.value[0] res[v.name] = v.value[0]