def __init__(self, model=None, env=None): Solver.__init__(self) if env: self.problem = GurobiModel(env=env) else: self.problem = GurobiModel() self.set_logging(False) self.set_parameters(default_parameters) if model: self.build_problem(model)
def __init__(self, model=None): Solver.__init__(self) self.problem = GurobiModel() self.set_logging() self.set_parameters(default_parameters) if model: self.build_problem(model)
def additive_optimization(model): """Optimize the model exactly, but just based on marginal utilities of individual migrant-locality pairs and assuming additivity. Args: model (models.Model): The submodular model to use Returns: pair (locality_per_agent,best_value) of type (list of int/None, float). The first component is the matching, the second its queried value in the model. """ gm = GurobiModel() gm.setParam("OutputFlag", False) variables = [] matching = [None for _ in range(model.num_agents)] objective = 0 for i in range(model.num_agents): agent_vars = [] for l in range(len(model.locality_caps)): matching[i] = l utility = model.utility_for_matching(matching) matching[i] = None v = gm.addVar(vtype=GRB.INTEGER, name=f"m_{i}_{l}") gm.addConstr(v >= 0) gm.addConstr(v <= 1) agent_vars.append(v) objective += utility * v variables.append(agent_vars) gm.addConstr(quicksum(agent_vars) <= 1) for l in range(len(model.locality_caps)): gm.addConstr(quicksum(variables[i][l] for i in range(model.num_agents)) <= model.locality_caps[l]) gm.setObjective(objective, GRB.MAXIMIZE) gm.optimize() assert gm.status == GRB.OPTIMAL for i in range(model.num_agents): for l in range(len(model.locality_caps)): if variables[i][l].X > 0.5: matching[i] = l break return matching, model.utility_for_matching(matching, False)
def build_gurobi_model(case): G, B = case.G, case.B P = real(case.demands) Q = imag(case.demands) branches = case.branch_list n = len(case.demands) vhat = case.vhat s2 = 2**.5 gens = {bus: gen.v for bus, gen in case.gens.items()} del gens[0] m = GurobiModel("jabr") u = [m.addVar(name='u_%d' % i) for i in range(n)] R = {(i, j): m.addVar(name='R_%d_%d' % (i, j)) for i, j in branches} I = {(i, j): m.addVar(lb=-GRB.INFINITY, name='I_%d_%d' % (i, j)) for i, j in branches} for i, j in branches: R[j, i] = R[i, j] I[j, i] = I[i, j] m.update() m.addConstr(u[0] == vhat * vhat / s2, 'u0') for gen, v in gens.iteritems(): m.addConstr(u[gen] == v * v / s2, 'u%d' % gen) for i, j in branches: m.addQConstr(2 * u[i] * u[j] >= R[i, j] * R[i, j] + I[i, j] * I[i, j], 'cone_%d_%d' % (i, j)) k = lambda i: (j for j in B[i, :].nonzero()[1]) s = lambda i, j: 1 if i < j else -1 for i in range(1, n): m.addConstr( -s2 * u[i] * G[i, :].sum() + quicksum(G[i, j] * R[i, j] + B[i, j] * s(i, j) * I[i, j] for j in k(i)) == P[i], 'real_flow_%d_%d' % (i, j)) if i in gens: continue m.addConstr( s2 * u[i] * B[i, :].sum() + quicksum(-B[i, j] * R[i, j] + G[i, j] * s(i, j) * I[i, j] for j in k(i)) == Q[i], 'reac_flow_%d_%d' % (i, j)) m.setObjective(quicksum(R[i, j] for i, j in branches), sense=GRB.MAXIMIZE) m.params.outputFlag = 0 #m.params.barQCPConvTol = 5e-10 m.optimize() if m.status != 2: raise ValueError("gurobi failed to converge: %s (check log)" % m.status) u_opt = [x.getAttr('x') for x in u] R_opt = {(i, j): x.getAttr('x') for (i, j), x in R.items()} I_opt = {(i, j): x.getAttr('x') for (i, j), x in I.items()} return u_opt, R_opt, I_opt