def criar_funcao_objetivo(model, lista_variaveis, variables, lista_coef): # inicializando a expressão com a primeira variável linear_expression = LinExpr() for i in range(1, len(lista_variaveis)): linear_expression.add(variables.values()[i], lista_coef[i]) model.setObjective(linear_expression, GRB.MINIMIZE) return model
def _set_objective(self, objective): expr = LinExpr() for elem in objective.expr: var = self.problem.getVarByName(elem.name) expr.add(var, elem.coefficient) self.problem.setObjective(expr, objective.sense)
def _add_constraint(self, const): lhs = LinExpr() for elem in const.lhs: var = self.problem.getVarByName(elem.name) lhs.add(var, elem.coefficient) self.problem.addConstr(lhs, const.sense, const.rhs, const.name)
def solve_restricted_primal(self, demands, demands_next, p): self.obj = 0. m = Model("multi-unit-auction") # self.m.params.LogToConsole = 0 self.allocation_vars = dict() for agent in self.agents: for i in range(1, self.supply + 1): self.allocation_vars[agent.id, i] = m.addVar(lb=0., ub=1., vtype=GRB.CONTINUOUS, name='x_%s_%s' % (agent.id, i)) m.update() for agent in self.agents: if len(demands[agent.id]) > 0 and len(demands_next[agent.id]) > 0: m.addConstr(quicksum(self.allocation_vars[agent.id, i] for i in range(1, self.supply + 1)), GRB.EQUAL, 1, name="u_%s_strict" % agent.id) else: m.addConstr(quicksum(self.allocation_vars[agent.id, i] for i in range(1, self.supply + 1)), GRB.LESS_EQUAL, 1, name="u_%s" % agent.id) for j in range(1, self.supply + 1): if j not in [demand.quantity for demand in demands[agent.id]]: m.addConstr(self.allocation_vars[agent.id, j], GRB.EQUAL, 0, name='x_%s_%s_undemanded' % (agent.id, j)) if p > 0: m.addConstr( quicksum(self.allocation_vars[agent.id, i] * i for i in range(1, self.supply + 1) for agent in self.agents), GRB.EQUAL, self.supply, name="price_strict") else: m.addConstr( quicksum(self.allocation_vars[agent.id, i] * i for i in range(1, self.supply + 1) for agent in self.agents), GRB.LESS_EQUAL, self.supply, name="price") obj_expr = LinExpr() for agent in self.agents: for valuation in agent.valuations: obj_expr.addTerms(valuation.quantity, self.allocation_vars[agent.id, valuation.quantity]) m.setObjective(obj_expr, GRB.MAXIMIZE) m.update() m.optimize() m.write('optimal-lp.lp') if m.status == GRB.OPTIMAL: m.write('optimal-lp.sol') for v in [v for v in m.getVars() if v.x != 0.]: print('%s %g' % (v.varName, v.x)) print '' print 'CONSTRAINTS:' for l in m.getConstrs(): if l.Pi > 0: print('%s %g' % (l.constrName, l.Pi)) print m.getObjective().getValue() return m
def objective_function(self, beta=1): # Set objective function obj = LinExpr() for e in self.network.edges.values(): obj.addTerms(e.value, e.var) for n in self.network.nodes.values(): if n.type == Type.NORMAL: obj.addTerms(-n.value * beta, n.var) self.lp.setObjective(obj, GRB.MINIMIZE)
def solve(model, lista_caminhos, variables, lista_cores, path): # Criar array de constraints para limitar as colunas # uma por vertice num_cores = obter_numero_cores(path) num_vertices = obter_numero_vertices(path) constraints = [] nomes_vertices = [] pi = [] # gerar lista de nomes for i in range(0, num_vertices): for j in range(0, num_cores): nomes_vertices.append("vertice" + str(i + 1) + "_cor" + str(j + 1)) for i in range(0, num_vertices * num_cores): expr = LinExpr() # Criando a expressão e já atribuindo o nome para poder recuperar # na geração de coluna constraints.append(model.addRange(expr, 1, 1, nomes_vertices[i])) # Aplicando a heuristica desenvolvida para encontrar solução inicial caminhos_possiveis = encontrar_caminhos_validos(lista_caminhos, lista_cores) caminhos_possiveis = unificar_caminho(model, caminhos_possiveis) # pra cada caminho possivel, achar uma coluna for i in range(0, len(caminhos_possiveis)): coluna = gerar_coluna(caminhos_possiveis[i], constraints) # adicionar ao modelo model.addVar(0, 1, cst_red, GRB.CONTINUOUS, coluna) model.update() solve_lp(pi, path, model)
def add_benders_cut(self, allocation, name): """ Adds another cut z <= wb - (c + wA) * X. :param allocation: Allocation as list of Assignment. :param name: Name for new constraint. """ # wb part of cut expr = LinExpr(self.b, self.utility_vars.values() + [self.price_var]) for assignment in allocation.assignments: # c expr.addConstant(-assignment.valuation) # if w=(u, p) then this is the uA part (for columns where X is 1) expr.addTerms(-1, self.utility_vars[assignment.agent_id]) # if w=(u, p) then this is the pA part (for columns where X is 1) expr.addTerms(-assignment.quantity, self.price_var) # we get v_i(j) + u_i + j * price summed over all i,j where x_ij = 1 self.m.addConstr(self.z, GRB.LESS_EQUAL, expr, name=name)
def initialise(self): # Initialise variables for n in self.network.nodes.values(): n.var = self.lp.addVar(vtype=GRB.BINARY, name=n.id) for e in self.network.edges.values(): e.var = self.lp.addVar(vtype=GRB.BINARY, name=e.id) # Update variables self.lp.update() # Root restriction lhs = LinExpr() for root_out in self.network.out_edges(self.network.root.id): lhs.addTerms(1, root_out.var) self.lp.addConstr(lhs, GRB.EQUAL, 1) # Income constraints for n in self.network.nodes.values(): lhs = LinExpr() for e in self.network.in_edges(n.id): lhs.addTerms(1, e.var) self.lp.addConstr(lhs, GRB.EQUAL, LinExpr(n.var)) # Reversibility constraint for e in self.network.edges.values(): if e.inverse is not None: lhs = LinExpr([1, 1], [e.var, self.network.get_edge(e.inverse).var]) self.lp.addConstr(lhs, GRB.LESS_EQUAL, self.network.get_node(e.source).var) self.lp.addConstr(lhs, GRB.LESS_EQUAL, self.network.get_node(e.target).var) for n in self.network.nodes.values(): if n.type == Type.LEAF or n.type == Type.LEAF_TERMINAL: for in_edge in self.network.in_edges(n.id): source = self.network.get_node(in_edge.source) if source.type != Type.ROOT: print n.id, in_edge.source self.lp.addConstr(source.var, GRB.LESS_EQUAL, n.var)
def equationBuilder(index_inequacao, model_vars): index_inequacao.reverse() sinal = '+' expr = LinExpr() for i in range(0, len(index_inequacao)): if sinal == '+': var = model_vars[index_inequacao[i]] # adicionar variavel a expressao expr.add(var, 1) # alterar sinal sinal = '-' elif sinal == '-': var = model_vars[index_inequacao[i]] # adicionar variavel a expressao expr.add(var, -1) # alterar sinal sinal = '+' return expr
for out_edge in out_edges: if out_edge.startswith(site): lp.addConstr(lp.getVarByName(out_edge), GRB.EQUAL, lp.getVarByName(site)) for kinase in kinases: for in_edge in in_edges: if in_edge.startswith(kinase): lp.addConstr(lp.getVarByName(kinase), GRB.GREATER_EQUAL, lp.getVarByName(in_edge)) for substrate in substrates: for out_edge in out_edges: if out_edge.endswith(substrate): lp.addConstr(lp.getVarByName(substrate), GRB.GREATER_EQUAL, lp.getVarByName(out_edge)) # Initialise objective function obj = LinExpr() for edge in in_edges: obj.addTerms(in_edges_costs[edge] if edge in in_edges_costs else 1, lp.getVarByName(edge)) for edge in out_edges: obj.addTerms(out_edges_costs[edge] if edge in out_edges_costs else 1, lp.getVarByName(edge)) for node in sites: obj.addTerms(-sites_fc[node] * beta if node in sites_fc else 0, lp.getVarByName(node)) for node in kinases: obj.addTerms(1e-6, lp.getVarByName(node)) for node in substrates: obj.addTerms(1e-6, lp.getVarByName(node))
def defineObjectives(data: DataInstance, model: Model, boolVars, N, posVars): LAG, RAG, TAG, BAG = boolVars L, R, T, B, H, W = posVars maxX = model.addVar(vtype=GRB.INTEGER, name="maxX") maxY = model.addVar(vtype=GRB.INTEGER, name="maxY") for element in range(data.element_count): model.addConstr(maxX >= R[element]) model.addConstr(maxY >= B[element]) OBJECTIVE_GRIDCOUNT = LinExpr(0.0) for element in range(data.element_count): OBJECTIVE_GRIDCOUNT.addTerms([1.0, 1.0], [LAG[element], TAG[element]]) OBJECTIVE_GRIDCOUNT.addTerms([1.0, 1.0], [BAG[element], RAG[element]]) OBJECTIVE_LT = LinExpr(0) for element in range(data.element_count): OBJECTIVE_LT.addTerms([1, 1, 2, 2, -1, -1], [T[element], L[element], B[element], R[element], W[element], H[element]]) Objective = LinExpr(0) Objective.add(OBJECTIVE_GRIDCOUNT, 1) Objective.add(OBJECTIVE_LT, 0.001) # Objective.add(maxX, 10) # Objective.add(maxY, 10) model.addConstr(OBJECTIVE_GRIDCOUNT >= (calculateLowerBound(N))) model.setObjective(Objective, GRB.MINIMIZE) return OBJECTIVE_GRIDCOUNT, OBJECTIVE_LT
def setConstraints(data: DataInstance, model: Model, relVars, boolVars, vVars, elemVars, posVars, N): L, R, T, B, H, W = posVars ABOVE, LEFT = relVars LAG, RAG, TAG, BAG = boolVars vLAG, vRAG, vTAG, vBAG = vVars elemAtLAG, elemAtRAG, elemAtTAG, elemAtBAG = elemVars # Known Position constraints X Y HORIZONTAL_TOLERANCE = data.canvasWidth * FLEXIBILITY_VALUE VERTICAL_TOLERANCE = data.canvasWidth * FLEXIBILITY_VALUE for element in range(data.element_count): print("At element ", element, "with lock = ", data.elements[element].isLocked) if data.elements[element].isLocked: if data.elements[element].X is not None and data.elements[element].X >= 0: model.addConstr(L[element] == data.elements[element].X, "PrespecifiedXOfElement(", element, ")") if data.elements[element].Y is not None and data.elements[element].Y >= 0: model.addConstr(T[element] == data.elements[element].Y, "PrespecifiedYOfElement(", element, ")") else: if data.elements[element].X is not None and data.elements[element].X >= 0: model.addConstr(L[element] >= data.elements[element].X - HORIZONTAL_TOLERANCE, "PrespecifiedXminOfElement(", element, ")") model.addConstr(L[element] <= data.elements[element].X + HORIZONTAL_TOLERANCE, "PrespecifiedXmaxOfElement(", element, ")") if data.elements[element].Y is not None and data.elements[element].Y >= 0: model.addConstr(T[element] >= data.elements[element].Y - VERTICAL_TOLERANCE, "PrespecifiedYminOfElement(", element, ")") model.addConstr(T[element] <= data.elements[element].Y + VERTICAL_TOLERANCE, "PrespecifiedYmaxOfElement(", element, ")") if data.elements[element].aspectRatio is not None and data.elements[element].aspectRatio > 0.001: model.addConstr(W[element] == data.elements[element].aspectRatio * H[element], "PrespecifiedAspectRatioOfElement(", element, ")") # Known Position constraints TOP BOTTOM LEFT RIGHT coeffsForAbsolutePositionExpression = [] varsForAbsolutePositionExpression = [] for element in range(data.element_count): for other in range(data.element_count): if element != other: if data.elements[element].verticalPreference is not None: if data.elements[element].verticalPreference.lower() == "top": varsForAbsolutePositionExpression.append(ABOVE[other, element]) coeffsForAbsolutePositionExpression.append(1.0) if data.elements[element].verticalPreference.lower() == "bottom": varsForAbsolutePositionExpression.append(ABOVE[element, other]) coeffsForAbsolutePositionExpression.append(1.0) if data.elements[element].horizontalPreference is not None: if data.elements[element].horizontalPreference.lower() == "left": varsForAbsolutePositionExpression.append(LEFT[other, element]) coeffsForAbsolutePositionExpression.append(1.0) if data.elements[element].horizontalPreference.lower() == "right": varsForAbsolutePositionExpression.append(LEFT[element, other]) coeffsForAbsolutePositionExpression.append(1.0) expression = LinExpr(coeffsForAbsolutePositionExpression, varsForAbsolutePositionExpression) model.addConstr(expression == 0, "Disable non-permitted based on prespecified") # Height/Width/L/R/T/B Summation Sanity for element in range(N): model.addConstr(W[element] + L[element] == R[element], "R-L=W(" + str(element) + ")") model.addConstr(H[element] + T[element] == B[element], "B-T=H(" + str(element) + ")") # MinMax limits of Left-Above interactions for element in range(N): for otherElement in range(N): if element > otherElement: model.addConstr( ABOVE[element, otherElement] + ABOVE[otherElement, element] + LEFT[element, otherElement] + LEFT[ otherElement, element] >= 1, "NoOverlap(" + str(element) + str(otherElement) + ")") model.addConstr( ABOVE[element, otherElement] + ABOVE[otherElement, element] + LEFT[element, otherElement] + LEFT[ otherElement, element] <= 2, "UpperLimOfQuadrants(" + str(element) + str(otherElement) + ")") model.addConstr(ABOVE[element, otherElement] + ABOVE[otherElement, element] <= 1, "Anti-symmetryABOVE(" + str(element) + str(otherElement) + ")") model.addConstr(LEFT[element, otherElement] + LEFT[otherElement, element] <= 1, "Anti-symmetryLEFT(" + str(element) + str(otherElement) + ")") # Interconnect L-R-LEFT and T-B-ABOVE for element in range(N): for otherElement in range(N): if element != otherElement: model.addConstr( R[element] + data.elementXPadding <= L[otherElement] + (1 - LEFT[element, otherElement]) * ( data.canvasWidth + data.elementXPadding), (str(element) + "(ToLeftOf)" + str(otherElement))) model.addConstr( B[element] + data.elementYPadding <= T[otherElement] + (1 - ABOVE[element, otherElement]) * ( data.canvasHeight + data.elementYPadding), (str(element) + "(Above)" + str(otherElement))) model.addConstr( (L[otherElement] - R[element] - data.elementXPadding) <= data.canvasWidth * LEFT[ element, otherElement] , (str(element) + "(ConverseOfToLeftOf)" + str(otherElement))) model.addConstr( (T[otherElement] - B[element] - data.elementYPadding) <= data.canvasHeight * ABOVE[ element, otherElement] , (str(element) + "(ConverseOfAboveOf)" + str(otherElement))) # One Alignment-group for every edge of every element for element in range(N): coeffsForLAG = [] coeffsForRAG = [] coeffsForTAG = [] coeffsForBAG = [] varsForLAG = [] varsForRAG = [] varsForTAG = [] varsForBAG = [] for alignmentGroupIndex in range(data.element_count): varsForLAG.append(elemAtLAG[element, alignmentGroupIndex]) coeffsForLAG.append(1) varsForRAG.append(elemAtRAG[element, alignmentGroupIndex]) coeffsForRAG.append(1) varsForTAG.append(elemAtTAG[element, alignmentGroupIndex]) coeffsForTAG.append(1) varsForBAG.append(elemAtBAG[element, alignmentGroupIndex]) coeffsForBAG.append(1) model.addConstr(LinExpr(coeffsForLAG, varsForLAG) == 1, "OneLAGForElement[" + str(element) + "]") model.addConstr(LinExpr(coeffsForTAG, varsForTAG) == 1, "OneTAGForElement[" + str(element) + "]") model.addConstr(LinExpr(coeffsForBAG, varsForBAG) == 1, "OneBAGForElement[" + str(element) + "]") model.addConstr(LinExpr(coeffsForRAG, varsForRAG) == 1, "OneRAGForElement[" + str(element) + "]") # Symmetry breaking and sequencing of alignment groups # for alignmentGroupIndex in range(data.N): # if(alignmentGroupIndex >= 1): # print() # gurobi.addConstr(LAG[alignmentGroupIndex] <= LAG[alignmentGroupIndex-1], "SymmBreakLAG["+str(alignmentGroupIndex)+ "]") # gurobi.addConstr(TAG[alignmentGroupIndex] <= TAG[alignmentGroupIndex-1], "SymmBreakTAG["+str(alignmentGroupIndex)+ "]") # gurobi.addConstr(RAG[alignmentGroupIndex] <= RAG[alignmentGroupIndex-1], "SymmBreakRAG["+str(alignmentGroupIndex)+ "]") # gurobi.addConstr(BAG[alignmentGroupIndex] <= BAG[alignmentGroupIndex-1], "SymmBreakBAG["+str(alignmentGroupIndex)+ "]") # gurobi.addConstr(vLAG[alignmentGroupIndex] >= vLAG[alignmentGroupIndex-1]+1, "ProgressiveIndexLAG["+str(alignmentGroupIndex)+"]") # gurobi.addConstr(vTAG[alignmentGroupIndex] >= vTAG[alignmentGroupIndex-1]+1, "ProgressiveIndexTAG["+str(alignmentGroupIndex)+"]") # gurobi.addConstr(vRAG[alignmentGroupIndex] >= vRAG[alignmentGroupIndex-1]+1, "ProgressiveIndexRAG["+str(alignmentGroupIndex)+"]") # gurobi.addConstr(vBAG[alignmentGroupIndex] >= vBAG[alignmentGroupIndex-1]+1, "ProgressiveIndexBAG["+str(alignmentGroupIndex)+"]") # Assign alignment groups to elements only if groups are enabled for alignmentGroupIndex in range(data.element_count): for element in range(N): model.addConstr(elemAtLAG[element, alignmentGroupIndex] <= LAG[alignmentGroupIndex]) model.addConstr(elemAtRAG[element, alignmentGroupIndex] <= RAG[alignmentGroupIndex]) model.addConstr(elemAtTAG[element, alignmentGroupIndex] <= TAG[alignmentGroupIndex]) model.addConstr(elemAtBAG[element, alignmentGroupIndex] <= BAG[alignmentGroupIndex]) # Correlate alignment groups value with element edge if assigned for alignmentGroupIndex in range(data.element_count): for element in range(N): model.addConstr(L[element] <= vLAG[alignmentGroupIndex] + data.canvasWidth * ( 1 - elemAtLAG[element, alignmentGroupIndex]), "MinsideConnectL[" + str(element) + "]ToLAG[" + str(alignmentGroupIndex) + "]") model.addConstr(R[element] <= vRAG[alignmentGroupIndex] + data.canvasWidth * ( 1 - elemAtRAG[element, alignmentGroupIndex]), "MinsideConnectR[" + str(element) + "]ToRAG[" + str(alignmentGroupIndex) + "]") model.addConstr(T[element] <= vTAG[alignmentGroupIndex] + data.canvasHeight * ( 1 - elemAtTAG[element, alignmentGroupIndex]), "MinsideConnectT[" + str(element) + "]ToTAG[" + str(alignmentGroupIndex) + "]") model.addConstr(B[element] <= vBAG[alignmentGroupIndex] + data.canvasHeight * ( 1 - elemAtBAG[element, alignmentGroupIndex]), "MinsideConnectB[" + str(element) + "]ToBAG[" + str(alignmentGroupIndex) + "]") model.addConstr(L[element] >= vLAG[alignmentGroupIndex] - data.canvasWidth * ( 1 - elemAtLAG[element, alignmentGroupIndex]), "MaxsideConnectL[" + str(element) + "]ToLAG[" + str(alignmentGroupIndex) + "]") model.addConstr(R[element] >= vRAG[alignmentGroupIndex] - data.canvasWidth * ( 1 - elemAtRAG[element, alignmentGroupIndex]), "MaxsideConnectR[" + str(element) + "]ToRAG[" + str(alignmentGroupIndex) + "]") model.addConstr(T[element] >= vTAG[alignmentGroupIndex] - data.canvasHeight * ( 1 - elemAtTAG[element, alignmentGroupIndex]), "MaxsideConnectT[" + str(element) + "]ToTAG[" + str(alignmentGroupIndex) + "]") model.addConstr(B[element] >= vBAG[alignmentGroupIndex] - data.canvasHeight * ( 1 - elemAtBAG[element, alignmentGroupIndex]), "MaxsideConnectB[" + str(element) + "]ToBAG[" + str(alignmentGroupIndex) + "]")
# obtendo dados dos arquivos df = obter_variaveis(path) # obtendo a lista de variaveis e respectivos coeficientes para função # obj. lista_variaveis = df['nome_variavel'] lista_coeficientes = df['coeff'] # CRIANDO AS VARIÁVEIS DO MODELO ------------------------------------------ # variables = adicionar_variaveis_modelo(m, lista_variaveis, # "modelo_var") variables = m.addVars(lista_variaveis, vtype=GRB.BINARY, name="variables") # ADICIONANDO EXPRESSÃO LINEAR DA FUNÇÃO OBJETIVO ------------------------- # inicializando a expressão com a primeira variável linear_expression = LinExpr() for i in range(1, len(lista_variaveis)): linear_expression.add(variables.values()[i], lista_coeficientes[i]) m.setObjective(linear_expression, GRB.MINIMIZE) # ADICIONANDO EXPRESSÃO LINEAR DA PRIMEIRA RESTRIÇÃO ------------------ # Garantindo que todos os vértices sejam pintados. for numero_vertice in map(int, obter_lista_vertices(path)): # Abaixo, obtendo indices das variáveis do vertice N, que devem ser # somados em uma restrição linear para garantir que assumam # apenas uma # cor. Nesse caso, itero por cada vertice X de cor 'qualquer', para # posteriomente somar todos quando construo a restrição. lista_temp = [ r[0] for r in [i.lower().split('_') for i in lista_variaveis] ]
def __init__(self, supply, agents, gap, restriced=False): print '' print 'Optimal Solver:' self.m = Model("multi-unit-auction") self.m.params.LogToConsole = 0 self.allocation_vars = dict() for agent in agents: for i in range(1, supply + 1): self.allocation_vars[agent.id, i] = self.m.addVar(lb=0., ub=1., vtype=GRB.CONTINUOUS, name='x_%s_%s' % (agent.id, i)) self.m.update() for agent in agents: self.m.addConstr(quicksum(self.allocation_vars[agent.id, i] for i in range(1, supply + 1)), GRB.LESS_EQUAL, 1, name="u_%s" % agent.id) if restriced: for valuation in agent.valuations: if valuation.valuation > 0: self.m.addConstr(self.allocation_vars[agent.id, valuation.quantity] >= epsilon, name="not_zero_%s_%s" % (agent.id, valuation.quantity)) self.m.addConstr(quicksum(self.allocation_vars[agent.id, i]*i for i in range(1, supply + 1) for agent in agents), GRB.LESS_EQUAL, supply, name="price") obj_expr = LinExpr() for agent in agents: for valuation in agent.valuations: obj_expr.addTerms(valuation.valuation, self.allocation_vars[agent.id, valuation.quantity]) self.m.setObjective(obj_expr, GRB.MAXIMIZE) self.m.update() self.m.optimize() # # for agent in agents: # for i in range(1, supply + 1): # self.allocation_vars[agent.id, i] = self.m.addVar(vtype=GRB.CONTINUOUS, lb=0, # name='x_%s_%s' % (agent.id, i)) # # self.m.update() # # self.m.addConstr(quicksum(self.allocation_vars[agent.id, i] for i in range(1, supply + 1) for agent in agents), # GRB.LESS_EQUAL, supply / gap, name="price") # for agent in agents: # for i in range(1, supply): # self.m.addConstr(self.allocation_vars[agent.id, i + 1] - self.allocation_vars[agent.id, i], # GRB.LESS_EQUAL, 0, name="chain_%s_%s" % (agent.id, i)) # self.m.addConstr(self.allocation_vars[agent.id, i], GRB.LESS_EQUAL, 1. / gap, # name="p_%s_%s" % (agent.id, i)) # self.m.addConstr(self.allocation_vars[agent.id, supply], GRB.GREATER_EQUAL, 0, name="greater_%s" % agent.id) # self.m.addConstr(self.allocation_vars[agent.id, 1], GRB.LESS_EQUAL, 1. / gap, name="u_%s" % agent.id) # # # m.addConstr(x11 + 2*x12 + 3*x13 + 4*x14 + x21 + 2*x22 + 3*x23 + 4*x24, GRB.LESS_EQUAL, 4, name="p_an") # # obj_expr = LinExpr() # for agent in agents: # prev_val = None # for valuation in agent.valuations: # try: # prev_val = next(val for val in agent.valuations if val.quantity == valuation.quantity - 1) # except StopIteration: # pass # marginal_value = valuation.valuation - (prev_val.valuation if prev_val else 0) # obj_expr.addTerms(marginal_value, self.allocation_vars[agent.id, valuation.quantity]) # self.m.setObjective(obj_expr, GRB.MAXIMIZE) # # self.m.optimize() for v in [v for v in self.m.getVars() if v.x != 0.]: print('%s %g' % (v.varName, v.x)) print '' print 'CONSTRAINTS:' for l in self.m.getConstrs(): if l.Pi > 0: print('%s %g' % (l.constrName, l.Pi)) print self.m.getObjective().getValue() # print 'Optimal solution:' # for v in self.m.getVars(): # print('%s %g' % (v.varName, v.x)) # for l in self.m.getConstrs(): # if l.Pi > 0: # print('%s %g' % (l.constrName, l.Pi)) print 'OPT social welfare %s | %s/%s=%s' % ( self.m.getObjective().getValue(), self.m.getObjective().getValue(), gap, self.m.getObjective().getValue() / gap) self.m.write('optimal-lp.lp') self.m.write('optimal-lp.sol')
import gurobipy as gp from gurobipy import GRB from gurobipy.gurobipy import LinExpr # Create a new model m = gp.Model("mip1") # Create the set of variables lista_variaveis = ['x', 'y', 'z'] vertices = m.addVars(lista_variaveis, vtype=GRB.BINARY, name="lista_variaveis") # FO: Maximize the linear expression # x + y + 2 z expr = LinExpr([1], [vertices['x']]) expr.add(vertices['y'], 1) expr.add(vertices['z'], 2) m.setObjective(expr, GRB.MAXIMIZE) # (outra maneira de passar o mesmo código acima, seria: # expr = LinExpr([1, 1, 2], [vertices['x'], # vertices['y'], # vertices['z']]) # Adding contraints # Add constraint: x + 2 y + 3 z <= 4 expr = LinExpr([1], [vertices['x']]) expr.add(vertices['y'], 2) expr.add(vertices['z'], 3) m.addConstr(expr, "<=", 4) # (outra maneira de passar o mesmo código acima, seria: #expr = LinExpr([1, 2, 3], [vertices['x'], # vertices['y'],