def test_llparam(self): # TODO: tests for undefined, partially defined and fully defined params plan = self.move_no_obs horizon = plan.horizon move = plan.actions[0] pr2 = move.params[0] robot_init_pose = move.params[1] start = move.params[1] end = move.params[2] model = grb.Model() model.params.OutputFlag = 0 # silences Gurobi output # pr2 is an Object parameter pr2_ll = ll_solver.LLParam(model, pr2, horizon, (0,horizon-1)) pr2_ll.create_grb_vars() self.assertTrue(pr2_ll.pose.shape == (2, horizon)) with self.assertRaises(AttributeError): pr2_ll._type with self.assertRaises(AttributeError): pr2_ll.geom model.update() obj = grb.QuadExpr() obj += pr2_ll.pose[0,0]*pr2_ll.pose[0,0] + \ pr2_ll.pose[1,0]*pr2_ll.pose[1,0] model.setObjective(obj) model.optimize() self.assertTrue(np.allclose(pr2_ll.pose[0,0].X, 0.)) self.assertTrue(np.allclose(pr2_ll.pose[1,0].X, 0.)) pr2_ll.batch_add_cnts() model.optimize() self.assertTrue(np.allclose(pr2_ll.pose[0,0].X, robot_init_pose.value[0,0])) self.assertTrue(np.allclose(pr2_ll.pose[1,0].X, robot_init_pose.value[1,0])) # x1^2 + x2^2 - 2x obj = grb.QuadExpr() obj += pr2_ll.pose[0,1]*pr2_ll.pose[0,1] + \ pr2_ll.pose[1,1]*pr2_ll.pose[1,1]- 2*pr2_ll.pose[1,1] model.setObjective(obj) model.optimize() self.assertTrue(np.allclose(pr2_ll.pose[0,0].X, robot_init_pose.value[0,0])) self.assertTrue(np.allclose(pr2_ll.pose[1,0].X, robot_init_pose.value[1,0])) self.assertTrue(np.allclose(pr2_ll.pose[0,1].X, 0.)) self.assertTrue(np.allclose(pr2_ll.pose[1,1].X, 1.)) pr2_ll.update_param() self.assertTrue(np.allclose(pr2.pose[0,1], 0.)) self.assertTrue(np.allclose(pr2.pose[1,1], 1.)) # robot_init_pose is a Symbol parameter model = grb.Model() model.params.OutputFlag = 0 # silences Gurobi output robot_init_pose_ll = ll_solver.LLParam(model, robot_init_pose, horizon, (0, horizon-1)) robot_init_pose_ll.create_grb_vars() self.assertTrue(robot_init_pose_ll.value.shape == (2,1)) with self.assertRaises(AttributeError): pr2_ll._type with self.assertRaises(AttributeError): pr2_ll.geom
def __cost_function(self): """ Driving Cost Function """ distances = nx.floyd_warshall_numpy(self.G) driving_cost_function = [] for c in range(self.number_of_homes + 1): summation = [] for i in range(self.number_of_locations): for j in range(self.number_of_locations): summation.append( grb.QuadExpr( self.arrangement_matrix[i][c] * distances.item( (i, j)) * self.arrangement_matrix[j][c + 1])) self.model.update() driving_cost_function.append(grb.quicksum(summation)) """ Walking Cost Function """ walking_cost_function = [] for row in range(self.number_of_locations): for col in range(self.number_of_locations): walking_cost_function.append( grb.LinExpr(self.walking_matrix[row][col] * distances.item( (row, col)))) self.model.update() """ Set Objective Function """ cost_function = driving_cost_function + walking_cost_function self.model.setObjective(grb.quicksum(cost_function), grb.GRB.MINIMIZE)
def _create_obj_function(self): lin_expr = grb.LinExpr(0.0) quad_expr = grb.QuadExpr(0.0) # set coefficients for simple bids for bid_id, hourly_bid in self.dam_data.dam_bids.bid_id_2_step_hourly_bid.items( ): for step_id, simple_bid in hourly_bid.step_id_2_simple_bid.items(): lin_expr.add( self.bid_id_2_step_id_2_sbidvar[bid_id][step_id][0], simple_bid.p * simple_bid.q) # set coefficients for interpolated bids for bid_id, hourly_bid in self.dam_data.dam_bids.bid_id_2_piecewise_hourly_bid.items( ): for step_id, interpolated_bid in hourly_bid.step_id_2_interpolated_bid.items( ): pvar = self.bid_id_2_step_id_2_sbidvar[bid_id][step_id][0] lin_expr.add(pvar, interpolated_bid.p_start * interpolated_bid.q) quad_expr.add( pvar * pvar, 0.5 * (interpolated_bid.p_end - interpolated_bid.p_start) * interpolated_bid.q) # set coefficients for block bids for bid_id, block_bid in self.dam_data.dam_bids.bid_id_2_block_bid.items( ): lin_expr.add(self.bid_id_2_bbidvar[bid_id][0], block_bid.price * block_bid.total_quantity) obj_expr = lin_expr + quad_expr self.model.setObjective(obj_expr, grb.GRB.MAXIMIZE) self.surplus_expr = obj_expr
def quadratic_expression(H, x, tol=1.e-7): """ Generates a Gurobi quadratic expressions x' H x. Arguments ---------- H : numpy.ndarray Hessian of the quadratic expression. x : instance of gurobipy.Var Variable of the linear expression. tol : float Maximum absolute value for the elements of H to be considered nonzero. Returns ---------- expr : gurobipy.LinExpr Quadratic expressions. """ # initialize expression expr = grb.QuadExpr() for i in range(H.shape[0]): for j in range(H.shape[1]): # add only sufficiently big numbers if np.abs(H[i, j]) > tol: expr.add(x[i] * H[i, j] * x[j]) return expr
def test_save_and_restore(self): ## test to ensure saves sets self._saved_value correctly and modifying ## self._value doesn't change self._saved_value ## test to ensure that restore sets self._value to self._saved_value model = grb.Model() grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') model.update() grb_vars = np.array([grb_var]) var = Variable(grb_vars) obj = grb.QuadExpr() obj += grb_var*grb_var -4*grb_var + 4 model.setObjective(obj) model.optimize() var.update() var.save() self.assertTrue(np.allclose(var._value, np.array([2.0]))) obj = grb_var*grb_var -2*grb_var + 1 model.setObjective(obj) model.optimize() var.update() self.assertTrue(np.allclose(var._value, np.array([1.0]))) self.assertTrue(np.allclose(var._saved_value, np.array([2.0]))) var.restore() self.assertTrue(np.allclose(var._value, np.array([2.0])))
def forward_gurobi_prebuilt(Q, p, model, x, inequality_constraints, equality_constraints, G, h, quadobj): import gurobipy as gp import numpy as np obj = gp.QuadExpr() obj += quadobj for i in range(len(p)): obj += p[i] * x[i] model.setObjective(obj, gp.GRB.MINIMIZE) model.optimize() x_opt = np.array([x[i].x for i in range(len(x))]) if G is not None: print("G shape", G.shape) print("x_opt shape", x_opt.shape) print("h shape", h.shape) slacks = -(G @ x_opt - h) else: slacks = np.array([]) lam = np.array([ inequality_constraints[i].pi for i in range(len(inequality_constraints)) ]) nu = np.array( [equality_constraints[i].pi for i in range(len(equality_constraints))]) return model.ObjVal, x_opt, nu, lam, slacks
def add_model_soc_constr(self, model, variables, rows, mat, vec): """Adds SOC constraint to the model using the data from mat and vec. Parameters ---------- model : GUROBI model The problem model. variables : list The problem variables. rows : range The rows to be constrained. mat : SciPy COO matrix The matrix representing the constraints. vec : NDArray The constant part of the constraints. Returns ------- tuple A tuple of (QConstr, list of Constr, and list of variables). """ import gurobipy as gp # Make a variable and equality constraint for each term. soc_vars = [ model.addVar( obj=0, name="soc_t_%d" % rows[0], vtype=gp.GRB.CONTINUOUS, lb=0, ub=gp.GRB.INFINITY) ] for i in rows[1:]: soc_vars += [ model.addVar( obj=0, name="soc_x_%d" % i, vtype=gp.GRB.CONTINUOUS, lb=-gp.GRB.INFINITY, ub=gp.GRB.INFINITY) ] model.update() new_lin_constrs = [] for i, row in enumerate(rows): start = mat.indptr[row] end = mat.indptr[row + 1] x = [variables[j] for j in mat.indices[start:end]] coeff = -mat.data[start:end] expr = gp.LinExpr(coeff, x) expr.addConstant(vec[row]) new_lin_constrs.append(model.addLConstr(soc_vars[i], gp.GRB.EQUAL, expr)) t_term = soc_vars[0]*soc_vars[0] x_term = gp.QuadExpr() x_term.addTerms(np.ones(len(rows) - 1), soc_vars[1:], soc_vars[1:]) return (model.addQConstr(x_term <= t_term), new_lin_constrs, soc_vars)
def _quadexp_pic2grb(self, picosExpression): import gurobipy as gurobi if not isinstance(picosExpression, QuadExp): raise ValueError("Expression must be a quadratic expression.") # Import affine part of expression. gurobiExpression = gurobi.QuadExpr( self._scalar_affinexp_pic2grb(picosExpression.aff)) # Import quadratic form. gurobiI, gurobiJ, gurobiV = [], [], [] for (picosVar1, picosVar2), picosCoefficients \ in picosExpression.quad.items(): for sparseIndex in range(len(picosCoefficients)): localVar1Index = picosCoefficients.I[sparseIndex] localVar2Index = picosCoefficients.J[sparseIndex] localCoefficient = picosCoefficients.V[sparseIndex] gurobiI.append(self._gurobiVar[picosVar1.startIndex + localVar1Index]) gurobiJ.append(self._gurobiVar[picosVar2.startIndex + localVar2Index]) gurobiV.append(localCoefficient) gurobiExpression.addTerms(gurobiV, gurobiI, gurobiJ) return gurobiExpression
def opt_match(cost_matrix): # this will have to be modified to handle multiple similar items (integer not binary) model = gp.Model() model.setParam('OutputFlag', 0) x = {} l_n = cost_matrix.shape[0] r_n = cost_matrix.shape[1] for i in range(l_n): for j in range(r_n): x[i, j] = model.addVar(vtype=gp.GRB.BINARY, name=f'x_{i}_{j}') model.update() match_once_constraints = [] for i in range(l_n): match_once_constraints.append( model.addConstr(gp.quicksum(x[i, j] for j in range(r_n)) <= 1)) for j in range(r_n): match_once_constraints.append( model.addConstr(gp.quicksum(x[i, j] for i in range(l_n)) <= 1)) obj = gp.QuadExpr() for i in range(l_n): for j in range(r_n): obj += x[i, j] * cost_matrix[i, j].item() model.setObjective(obj, gp.GRB.MAXIMIZE) model.optimize() model.update() result = torch.zeros_like(cost_matrix) for i in range(l_n): for j in range(r_n): result[i, j] = x[i, j].x return result
def PotentialNEGurobi(m, n_I, n_C, n_constr, c, Q, A, b): m_Pot = grb.Model("PotentialNE") m_Pot.setParam('OutputFlag', False) m_Pot.setParam("Threads", 2) # set objective function direction m_Pot.ModelSense = -1 # maximize m_Pot.update() x = [ np.array([ m_Pot.addVar(vtype="B", name="x_" + str(p) + '_' + str(i)) for i in range(n_I[p]) ] + [ m_Pot.addVar(lb=0, vtype="C", name="x_" + str(p) + '_' + str(i)) for i in range(n_I[p], n_I[p] + n_C[p]) ]) for p in range(m) ] m_Pot.update() QuadPart = grb.QuadExpr(0) for p in range(m): for k in range(n_constr[p]): m_Pot.addConstr(np.dot(x[p], A[p][k]), grb.GRB.LESS_EQUAL, b[p][k]) m_Pot.update() if n_I[p] + n_C[p] == 1: QuadPart = QuadPart + grb.QuadExpr(x[p][0] * c[p][0] - 0.5 * x[p][0] * Q[p][p] * x[p][0]) else: QuadPart = QuadPart + grb.QuadExpr( grb.quicksum( 0.5 * np.dot(x[j], np.dot((Q[p][j] + Q[j][p].T), x[p].T)) for j in range(p)) + np.dot(x[p], c[p]) - 0.5 * (np.dot(x[p], np.dot(Q[p][p], x[p].T)))) m_Pot.setObjective(QuadPart) m_Pot.update() #m_Pot.write("apagar.lp") m_Pot.optimize() try: S = [[[x[p][k].x for k in range(n_I[p] + n_C[p])]] for p in range(m)] U_p = [[ float( np.dot(c[p], S[p][0]) - 0.5 * np.dot(S[p][0], np.dot(Q[p][p], S[p][0]))) ] for p in range(m)] return S, U_p except: print("No feasible profile of strategies", m_Pot.status) return None
def addNormConstr(model, vec1, vec2, vec_offset, th): #Add a constraint of the form norm(vec1+vec2+vec_offset))<=threshold #Input variables are Vector3D type x = vec1.x + vec2.x + vec_offset.x y = vec1.y + vec2.y + vec_offset.y z = vec1.z + vec2.z + vec_offset.z expr = go.QuadExpr(x*x+y*y+z*z) #Still needs to test it to see if it works as expected model.addQConstr(expr <= (th*th))
def forward_single_np_gurobi(Q, p, G, h, A, b): import gurobipy as gp import numpy as np ''' Convert to Gurobi model. Copied from https://github.com/stephane-caron/qpsolvers/blob/master/qpsolvers/gurobi_.py ''' n = Q.shape[1] model = gp.Model() model.params.OutputFlag = 0 x = [ model.addVar(vtype=gp.GRB.CONTINUOUS, name='x_%d' % i, lb=0, ub=1) for i in range(n) ] model.update() # integrate new variables # minimize # x.T * Q * x + p * x obj = gp.QuadExpr() rows, cols = Q.nonzero() for i, j in zip(rows, cols): obj += x[i] * Q[i, j] * x[j] for i in range(n): obj += p[i] * x[i] model.setObjective(obj, gp.GRB.MINIMIZE) # subject to # G * x <= h inequality_constraints = [] if G is not None: for i in range(G.shape[0]): row = np.where(G[i] != 0)[0] inequality_constraints.append( model.addConstr( gp.quicksum(G[i, j] * x[j] for j in row) <= h[i])) # subject to # A * x == b equality_constraints = [] if A is not None: for i in range(A.shape[0]): row = np.where(A[i] != 0)[0] equality_constraints.append( model.addConstr( gp.quicksum(A[i, j] * x[j] for j in row) == b[i])) model.optimize() x_opt = np.array([x[i].x for i in range(len(x))]) slacks = -(G @ x_opt - h) lam = np.array([ inequality_constraints[i].pi for i in range(len(inequality_constraints)) ]) nu = np.array( [equality_constraints[i].pi for i in range(len(equality_constraints))]) return model.ObjVal, x_opt, nu, lam, slacks
def QPmindist(cobramodel, fluxvalues, reactionmap, optreq, useoptreq=True, debug=False): #Create a dictionary to hold the experimental flux values: Ydict = {} #For every entry in the reaction map: for linkdict in reactionmap: if ((len(linkdict['modrxns']) > 1) or (len(linkdict['exprxns']) > 1)): raise Exception( 'Model to experimental reaction mapping must be one to one.') modrxnid = linkdict['modrxns'][0]['rxid'] exprxnid = linkdict['exprxns'][0]['rxid'] modcoef = linkdict['modrxns'][0]['coef'] #print(linkdict) if (exprxnid in fluxvalues): Ydict[modrxnid] = fluxvalues[exprxnid] * modcoef gurobimodel = gurobiFBA(cobramodel) FBAsolution = [v.x for v in gurobimodel.getVars()] FBAobjval = gurobimodel.Objval FBAobjective = getFBAobjective(gurobimodel) if debug: print("FBA objective value:", FBAobjval) #Add optimality requirement constraint to original model: if useoptreq: gurobimodel.addConstr(FBAobjective, gurobi.GRB.GREATER_EQUAL, FBAobjval * optreq) gurobimodel.update() #Create a new QuadExpr object to store the objective function. QPobjective = gurobi.QuadExpr() reactlist = [] terms = [] for key in Ydict: x = gurobimodel.getVarByName(key) fluxval = float(Ydict[key]) newterm = x * x - 2 * fluxval * x newterm.addConstant((fluxval**2)) QPobjective.add(newterm) gurobimodel.setObjective(QPobjective, gurobi.GRB.MINIMIZE) gurobimodel.update() terms.append(newterm) #Perform QP optimization: gurobimodel.optimize() gurobimodel.update() return gurobimodel
def dense_optimize(): # Put model data into dense matrices c = [1, 1, 0] Q = [[1, 1, 0], [0, 1, 1], [0, 0, 1]] A = [[1, 2, 3], [1, 1, 0]] sense = [GRB.GREATER_EQUAL, GRB.GREATER_EQUAL] rhs = [4, 1] lb = [0, 0, 0] ub = [GRB.INFINITY, GRB.INFINITY, GRB.INFINITY] vtype = [GRB.CONTINUOUS, GRB.CONTINUOUS, GRB.CONTINUOUS] solution = [0] * 3 rows = 2 cols = 3 # Optimize model = gp.Model() # Add variables to model vars = [] for j in range(cols): vars.append(model.addVar(lb=lb[j], ub=ub[j], vtype=vtype[j])) # Populate A matrix for i in range(rows): expr = gp.LinExpr() for j in range(cols): if A[i][j] != 0: expr += A[i][j] * vars[j] model.addConstr(expr, sense[i], rhs[i]) # Populate objective obj = gp.QuadExpr() for i in range(cols): for j in range(cols): if Q[i][j] != 0: obj += Q[i][j] * vars[i] * vars[j] for j in range(cols): if c[j] != 0: obj += c[j] * vars[j] model.setObjective(obj) # Solve model.optimize() # Write model to a file # model.write('dense.lp') if model.status == GRB.OPTIMAL: x = model.getAttr('x', vars) for i in range(cols): solution[i] = x[i] return True, solution else: return False, solution
def injection_equalize_optimization(Pg0, Pd0, gen_params, load_params, sysLoad=None): pd_ls_pg = np.where(((Pg0 - Pd0) > 0) & (Pd0 > 0) & (Pg0 > 0))[0] pd_gr_pg = np.where(((Pg0 - Pd0) < 0) & (Pd0 > 0) & (Pg0 > 0))[0] Pg_map = np.where(Pg0 > 0)[0] Pd_map = np.where(Pd0 > 0)[0] Pgmax = max(Pg0) Pdmax = max(Pd0) import gurobipy as gb m = gb.Model() alpha_g = m.addVars(Pg_map, lb=0) alpha_d = m.addVars(Pd_map, lb=0) m.addConstr( sum(alpha_g[i] * Pg0[i] for i in Pg_map) - sum(alpha_d[i] * Pd0[i] for i in Pd_map) == 0) m.addConstrs(alpha_g[i] * Pg0[i] >= gen_params['vmin'] for i in Pg_map) m.addConstrs(alpha_g[i] * Pg0[i] <= gen_params['vmax'] for i in Pg_map) m.addConstrs(alpha_d[i] * Pd0[i] >= load_params['vmin'] for i in Pd_map) m.addConstrs(alpha_d[i] * Pd0[i] <= load_params['vmax'] for i in Pd_map) m.addConstrs(alpha_d[i] * Pd0[i] <= alpha_g[i] * Pg0[i] for i in pd_ls_pg) m.addConstrs(alpha_d[i] * Pd0[i] >= alpha_g[i] * Pg0[i] for i in pd_gr_pg) if sysLoad is not None: m.addConstr(sum(alpha_g[i] * Pg0[i] for i in Pg_map) == sysLoad) obj = gb.QuadExpr() for i in alpha_g: obj += (Pg0[i] / Pgmax) * (alpha_g[i] - 1) * (alpha_g[i] - 1) for i in alpha_d: obj += (Pd0[i] / Pdmax) * (alpha_d[i] - 1) * (alpha_d[i] - 1) m.setObjective(obj, gb.GRB.MINIMIZE) m.optimize() Pgnew = np.zeros(Pg0.shape) for i in range(Pgnew.shape[0]): try: Pgnew[i] = Pg0[i] * alpha_g[i].X except KeyError: Pgnew[i] = Pg0[i] Pdnew = np.zeros(Pd0.shape) for i in range(Pdnew.shape[0]): try: Pdnew[i] = Pd0[i] * alpha_d[i].X except KeyError: Pdnew[i] = Pd0[i] return Pgnew, Pdnew
def _import_rscone_constraint(self, picosConstraint): import gurobipy as gurobi assert isinstance(picosConstraint, RSOCConstraint) picosLHS = picosConstraint.ne picosRHS1 = picosConstraint.ub1 picosRHS2 = picosConstraint.ub2 picosLHSLen = len(picosLHS) # Add auxiliary variables: One for every dimension of the left hand side # of the PICOS constraint and one for its right hand side. gurobiLHSVarsIndexed = self.int.addVars(picosLHSLen, lb=-gurobi.GRB.INFINITY, ub=gurobi.GRB.INFINITY) gurobiLHSVars = gurobiLHSVarsIndexed.values() gurobiRHSVars = self.int.addVars(2, lb=0.0, ub=gurobi.GRB.INFINITY).values() # Add constraints that identify the left hand side Gurobi auxiliary # variables with their slice of the PICOS left hand side expression. gurobiLHSSlices = dict() for dimension, slice in enumerate(self._affinexp_pic2grb(picosLHS)): gurobiLHSSlices[dimension] = slice gurobiLHSCons = self.int.addConstrs( (gurobiLHSSlices[dimension] - gurobiLHSVarsIndexed[dimension] == 0 \ for dimension in range(picosLHSLen))).values() # Add two constraints that identify the right hand side Gurobi auxiliary # variables with the PICOS right hand side scalar expressions. gurobiRHSExps = \ self._scalar_affinexp_pic2grb(picosRHS1), \ self._scalar_affinexp_pic2grb(picosRHS2) gurobiRHSCons = self.int.addConstrs( (gurobiRHSVars[i] - gurobiRHSExps[i] == 0 for i in (0, 1))).values() # Add a quadratic constraint over the auxiliary variables that # represents the PICOS second order cone constraint itself. quadExpr = gurobi.QuadExpr() quadExpr.addTerms([1.0] * picosLHSLen, gurobiLHSVars, gurobiLHSVars) gurobiName = picosConstraint.name if picosConstraint.name else "" gurobiQuadCon = self.int.addQConstr( quadExpr, gurobi.GRB.LESS_EQUAL, gurobiRHSVars[0] * gurobiRHSVars[1], gurobiName) gurobiMetaCon = self.GurobiRSOCC(LHSVars=gurobiLHSVars, RHSVars=gurobiRHSVars, LHSCons=gurobiLHSCons, RHSCons=gurobiRHSCons, quadCon=gurobiQuadCon) return gurobiMetaCon
def updateCost(u, A1_lb, A1_ub, A2_lb, A2_ub, b_lb, b_ub, Qc, Sc, Rc, qc, rc, w1, w2, w3): # global Qc, Rc, Sc, qc, rc # if Qc is None: # Qc = np.zeros((A1_lb.shape[0], A1_lb.shape[0])) # if Rc is None: # Rc = np.zeros((A1_lb.shape[1], A1_lb.shape[1])) # if Sc is None: # Sc = np.zeros((A1_lb.shape[0], A1_lb.shape[1])) # if qc is None: # qc = np.zeros(A1_lb.shape[0]) # if rc is None: # rc = np.zeros(A1_lb.shape[1]) # Weighted center matrices A and weight vector B coeffSup = w3 * w1 + (1 - w3) * w2 coeffInf = w3 * (1 - w1) + (1 - w3) * (1 - w2) midB = coeffSup * b_ub + coeffInf * b_lb midA = w3 * w1 * A1_ub + w3 * (1 - w1) * A1_lb + (1 - w3) * w2 * A2_ub + ( 1 - w3) * (1 - w2) * A2_lb # Compute Qt matrix temp1 = np.matmul(Qc, midA) + Sc Qt = np.matmul(midA.T, temp1 + Sc) + Rc qt = 2 * np.dot(temp1.T, midB) + np.dot(midA.T, qc) + rc # pt = np.dot(midB, np.dot(Qc, midB)) + np.dot(qc, midB) # print(Qt) # print(qt) # Build the quadratic expression res = gp.QuadExpr() listCoeff, listVar1, listVar2 = list(), list(), list() # COmpute the term u^T R u for i, ui in enumerate(u): for j, uj in enumerate(u): listCoeff.append(Qt[i, j]) listVar1.append(ui) listVar2.append(uj) # res += u[i] * R[i,j] * u[j] res.addTerms(listCoeff, listVar1, listVar2) # Coeff linear term linearCoeff = list() linearVar = list() # COmpute the term r^T u for i, ui in enumerate(u): linearCoeff.append(qt[i]) linearVar.append(ui) # res += r[i] * u[i] res.addTerms(linearCoeff, linearVar) # res.addConstant(pt) return res
def getQuadraticCost(x_var, u, Q, S, R, q, r): """ COmpute the quaadratic cost function given by [x_var u]^T [Q S; S^T R] [x_var u] + q^T x + r^T u """ res = gp.QuadExpr() listCoeff, listVar1, listVar2 = list(), list(), list() if Q is not None: # Compute th term x^T Q x for i, xi in enumerate(x_var): for j, xj in enumerate(x_var): listCoeff.append(Q[i,j]) listVar1.append(xi) listVar2.append(xj) # res += x_var[i] * Q[i,j] * x_var[j] if S is not None: # Compute the term 2 x^T S u for i, xi in enumerate(x_var): for j, uj in enumerate(u): listCoeff.append(S[i,j]) listVar1.append(xi) listVar2.append(uj) # res += 2 * x_var[i] * S[i,j] * u[j] if R is not None: # COmpute the term u^T R u for i, ui in enumerate(u): for j, uj in enumerate(u): listCoeff.append(R[i,j]) listVar1.append(ui) listVar2.append(uj) # res += u[i] * R[i,j] * u[j] res.addTerms(listCoeff, listVar1, listVar2) # Coeff linear term linearCoeff = list() linearVar = list() if q is not None: # Compute the term q^T x for i, xi in enumerate(x_var): linearCoeff.append(q[i]) linearVar.append(xi) # res += q[i] * x_var[i] if r is not None: # COmpute the term r^T u for i, ui in enumerate(u): linearCoeff.append(r[i]) linearVar.append(ui) # res += r[i] * u[i] res.addTerms(linearCoeff, linearVar) return res
def _gurobi_quadratic_program_ineq(c, Q, A, b): m, n = A.shape model = gpy.Model() model.setParam('OutputFlag', 0) # Add variables to model vars = [] for j in range(n): vars.append( model.addVar(lb=-gpy.GRB.INFINITY, ub=gpy.GRB.INFINITY, vtype=gpy.GRB.CONTINUOUS)) model.update() # Populate linear constraints for i in range(m): expr = gpy.LinExpr() for j in range(n): expr += A[i, j] * vars[j] model.addConstr(expr, gpy.GRB.GREATER_EQUAL, b[i, 0]) # Populate objective obj = gpy.QuadExpr() for i in range(n): for j in range(n): obj += Q[i, j] * vars[i] * vars[j] for j in range(n): obj += c[j, 0] * vars[j] model.setObjective(obj) model.update() # Solve model.optimize() if model.status == gpy.GRB.OPTIMAL: return np.array(model.getAttr('x', vars)).reshape((n, 1)) else: np.savez('bad_gurobi_qp_ineq_{:010d}'.format( np.random.randint(int(1e9))), c=c, Q=Q, A=A, b=b, model=model) raise Exception('Gurobi did not solve the QP. Blame Gurobi.') return None
def initIdealisticProblemGrb(U_lb, U_ub): global dictConstr, mOpt, uVar, objPb # , Qc, Rc, Sc, qc, rc dictConstr = dict() # Create the Gurobi model mOpt = gp.Model('Midpoint Model') # Variable u for the midpoint problem uVar = [mOpt.addVar(lb=U_lb[i], ub=U_ub[i]) for i in range(U_lb.shape[0])] # Set the objective to zero for now objPb = gp.QuadExpr() objPb.addConstant(0) mOpt.setObjective(objPb) # Add learning constraints for j, uj in enumerate(uVar): dictConstr[-j-1] = \ mOpt.addLConstr(gp.LinExpr([0], [uj]), gp.GRB.EQUAL, 0)
def _quad_expr_to_grb_expr(self, quad_expr, var): x = var.get_grb_vars() grb_expr = grb.QuadExpr() Q = quad_expr.Q rows, cols = x.shape assert cols == 1 inds = np.nonzero(Q) coeffs = 0.5*Q[inds] v1 = x[inds[0], 0] v2 = x[inds[1], 0] grb_expr.addTerms(coeffs.tolist(), v1.tolist(), v2.tolist()) inds = np.nonzero(quad_expr.A) coeffs = quad_expr.A[inds] v1 = x[inds[1], 0] grb_expr.addTerms(coeffs.tolist(), v1.tolist()) grb_expr = grb_expr + quad_expr.b return np.array([[grb_expr]]), []
def make_gurobi_model(G, h, A, b, Q, test=False): import gurobipy as gp import numpy as np ''' Convert to Gurobi model. Copied from https://github.com/stephane-caron/qpsolvers/blob/master/qpsolvers/gurobi_.py ''' vtype = gp.GRB.CONTINUOUS # gp.GRB.BINARY if test else gp.GRB.CONTINUOUS n = A.shape[1] if A is not None else G.shape[1] model = gp.Model() model.params.OutputFlag = 0 x = [ model.addVar(vtype=vtype, name='x_%d' % i, lb=-gp.GRB.INFINITY, ub=+gp.GRB.INFINITY) for i in range(n) ] model.update() # integrate new variables # subject to # G * x <= h inequality_constraints = [] if G is not None: for i in range(G.shape[0]): row = np.where(G[i] != 0)[0] inequality_constraints.append( model.addConstr( gp.quicksum(G[i, j] * x[j] for j in row) <= h[i])) # subject to # A * x == b equality_constraints = [] if A is not None: for i in range(A.shape[0]): row = np.where(A[i] != 0)[0] equality_constraints.append( model.addConstr( gp.quicksum(A[i, j] * x[j] for j in row) == b[i])) obj = gp.QuadExpr() if Q is not None: rows, cols = Q.nonzero() for i, j in zip(rows, cols): obj += x[i] * Q[i, j] * x[j] return model, x, inequality_constraints, equality_constraints, obj
def find_closest_feasible_point(self): """ Finds the closest point (l2 norm) to the initialization that satisfies the linear constraints. """ self._del_old_grb_cnts() self._model.update() obj = grb.QuadExpr() for var in self._vars: g_var = var.get_grb_vars() val = var.get_value() if val is not None: assert g_var.shape == val.shape for i in np.ndindex(g_var.shape): if not np.isnan(val[i]): obj += g_var[i]*g_var[i] - 2*val[i]*g_var[i] + val[i]*val[i] # grb_exprs = [] # for bound_expr in self._quad_obj_exprs: # grb_expr, grb_cnts = self._expr_to_grb_expr(bound_expr) # self._grb_penalty_cnts.extend(grb_cnts) # grb_exprs.extend(grb_expr.flatten().tolist()) # obj += grb.quicksum(grb_exprs) self._model.setObjective(obj) self._model.optimize() if self._model.status != 2: return False # if self._model.status == 3: # self._model.optimize() # elif self._model.status != 2: # self._model.write('infeasible.lp') # raise Exception('Failed to satisfy linear equalities. Infeasible Constraint set written to infeasible.lp') try: self._update_vars() except Exception as e: print(e) print('Model status:', self._model.status) self._callback() return True
def dense_optimize_v2(): solution = [0] * 3 # Optimize model = gp.Model() xyz = model.addMVar(shape=3, lb=0.0, ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, name="xyz") x = xyz.vararr[0] y = xyz.vararr[1] z = xyz.vararr[2] # Build (sparse) constraint matrix data = np.array([1.0, 2.0, 3.0, 1.0, 1.0, 1.0, 1.0, 1.0]) row = np.array([0, 0, 0, 1, 1, 2, 3, 4]) col = np.array([0, 1, 2, 0, 1, 0, 1, 2]) A = sp.csr_matrix((data, (row, col)), shape=(5, 3)) # Build rhs vector rhs = np.array([4.0, 1.0, 0.0, 0.0, 0.0]) # Add constraints model.addConstr(A @ xyz >= rhs, name="c") # Populate objective obj = gp.QuadExpr() obj += x + y + x * x + y * y + y * z + z * z model.setObjective(obj) # Solve model.optimize() # Write model to a file # model.write('dense.lp') if model.status == GRB.OPTIMAL: x = model.getAttr('x', vars) for i in range(3): solution[i] = x[i] return True, solution else: return False, solution
def _regularize(self): if self.regularization_param == 0 or self.iteration == 0: return MSP = self.MSP for t in range(MSP.T): m = MSP.models[t] regularization = m.addVar( lb=0, obj=MSP.sense*self.regularization_param*0.99**self.iteration, name='regularization_{}'.format(self.iteration) ) if self.regularization_type == 'L1': m.addConstrs( (regularization >= gurobipy.quicksum( m.find_states[i][j] *m.dual_probability[j] for j in range(m.dual_n_samples)) - self.forward_solution[t-1][i] for i in range(m.find_n_states)), name = 'regularization_{}'.format(self.iteration) ) elif self.regularization_type == 'L2': m.addQConstr( regularization - gurobipy.QuadExpr( gurobipy.quicksum([ gurobipy.quicksum( m.find_states[i][j] *m.dual_probability[j] for j in range(m.dual_n_samples)) * gurobipy.quicksum( m.find_states[i][j] *m.dual_probability[j] for j in range(m.dual_n_samples)) - gurobipy.quicksum( m.find_states[i][j] *m.dual_probability[j] for j in range(m.dual_n_samples)) * 2 * self.forward_solution[t-1][i] for i in range(m.find_n_states) ]) ) >=0, name = 'regularization_{}'.format(self.iteration) ) else: raise NotImplementedError m.update()
def _build_model(H=None, f=None, A=None, b=None, C=None, d=None): """ Builds the Gurobi model the LP or the QP. Arguments ---------- H, f, A, b, C, d : numpy.ndarray Matrices of the mathematical program. Returns ---------- model : instance of gurobipy.Model Model of the mathematical program. """ # initialize model model = grb.Model() n_x = f.size x = model.addVars(n_x, lb=[-grb.GRB.INFINITY]*n_x) # linear inequalities for i, expr in enumerate(linear_expression(A, -b, x)): model.addConstr(expr <= 0., name='ineq_'+str(i)) # linear equalities if C is not None and d is not None: for i, expr in enumerate(linear_expression(C, -d, x)): model.addConstr(expr == 0., name='eq_'+str(i)) # cost function if H is not None: cost = grb.QuadExpr() expr = quadratic_expression(H, x) cost.add(.5 * expr) else: cost = grb.LinExpr() f = f.reshape(1, f.size) expr = linear_expression(f, np.zeros(1), x) cost.add(expr[0]) model.setObjective(cost) return model
def dense_optimize(rows, cols, c, Q, A, sense, rhs, lb, ub, vtype, solution): model = gp.Model() # Add variables to model vars = [] for j in range(cols): vars.append(model.addVar(lb=lb[j], ub=ub[j], vtype=vtype[j])) # Populate A matrix for i in range(rows): expr = gp.LinExpr() for j in range(cols): if A[i][j] != 0: expr += A[i][j]*vars[j] model.addConstr(expr, sense[i], rhs[i]) # Populate objective obj = gp.QuadExpr() for i in range(cols): for j in range(cols): if Q[i][j] != 0: obj += Q[i][j]*vars[i]*vars[j] for j in range(cols): if c[j] != 0: obj += c[j]*vars[j] model.setObjective(obj) # Solve model.optimize() # Write model to a file model.write('dense.lp') if model.status == GRB.OPTIMAL: x = model.getAttr('x', vars) for i in range(cols): solution[i] = x[i] return True else: return False
def test_update(self): ## test that update updates self._value to values in Variable's Gurobi ## variables and that a GurobiError is raised if Variable's Gurobi ## variables do not have valid values model = grb.Model() grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') model.update() grb_vars = np.array([grb_var]) var = Variable(grb_vars) with self.assertRaises(grb.GurobiError) as cm: var.update() obj = grb.QuadExpr() obj += grb_var*grb_var -4*grb_var + 4 model.setObjective(obj) model.optimize() var.update() self.assertTrue(np.allclose(var._value, np.array([2.0])))
def maxval_rescale(P, Q, Pmin, pavg, qavg, pmax, pmin, qmax, qmin, G): import gurobipy as gb m = gb.Model() m.setParam('LogToConsole', 0) Gmap = np.where(P > 0)[0] PgQ = np.where((P > 0) & (P > Q))[0] PlQ = np.where((P > 0) & (P < Q))[0] Pmax = max(P) Qmax = max(Q) wp = m.addVars(Gmap, lb=0) #wq = m.addVars(Gmap,lb=0) m.addConstr(sum(wp[i] * P[i] for i in Gmap) == pavg * G) #m.addConstr(sum(wq[i]*Q[i] for i in Gmap) == qavg*G) m.addConstrs(wp[i] * P[i] <= pmax for i in Gmap) m.addConstrs(wp[i] * P[i] >= pmin for i in Gmap) #m.addConstrs(wq[i]*Q[i] <= qmax for i in Gmap) #m.addConstrs(wq[i]*Q[i] >= qmin for i in Gmap) #m.addConstrs(wp[i]*P[i] >= wq[i]*Q[i] for i in PgQ) #m.addConstrs(wp[i]*P[i] <= wq[i]*Q[i] for i in PlQ) obj = gb.QuadExpr() for i in wp: obj += (P[i] / Pmax)**2 * (wp[i] - 1) * (wp[i] - 1) #for i in wq: # obj += (Q[i]/Qmax)*(wq[i]- 1)*(wq[i] - 1) m.setObjective(obj, gb.GRB.MINIMIZE) m.optimize() flag = m.status == 2 if flag: for i in Gmap: P[i] = wp[i].X * P[i] Q[i] = wp[i].X * Q[i] Pmin[i] = wp[i].X * Pmin[i] return flag, P, Q, Pmin
def solve(self): objective = gp.QuadExpr() for i in range(len(self.diff)): objective += self.diff[i] * self.diff[i] # Set objective: maximize x self.m.setObjective(objective, GRB.MINIMIZE) try: # self.m.write("m.lp") # m.feasRelaxS(0, True, False, True) # m.write("feasopt1.lp") self.m.optimize() except gp.GurobiError: print("Optimize failed due to non-convexity") f = open("optmized_angles.txt", "w") for i in range(len(self.a)): print("a_", i, self.a[i].x) f.write(str(self.a[i].x) + "\n") f.close()