def model(self, x: mm.State, costs: mm.NodesData): """ :param x: State upon which the matchings are done. :param costs: NodesData giving the cost at each node. """ nb_edges = x.matching_graph.nb_edges nb_nodes = x.matching_graph.n edges_to_nodes = x.matching_graph.edges_to_nodes # The variables are the number of matching in each edge self.u = u = mip.VarVector([nb_edges], "u", mip.INT, lb=0, ub=mip.VAR_INF) # The goal is to maximize u\\cdot \\nabla h(state) where h(state)=\\sum_{i\\in \\mathcal{D}\\cup\\mathcal{S}} c_i x_i^2. # mip.maximize(mip.sum_(costs.data[i] * state.data[i] * mip.sum_(edges_to_nodes[i, j] * u[j] for j in range(nb_edges)) # for i in range(nb_nodes))) mip.maximize( mip.sum_( np.sum(np.multiply(costs[edge], x[edge])) * u[i] for i, edge in enumerate(x.matching_graph.edges))) # The inequalities constraints # The number of matchings can not be higher than the number of items in the system for i in range(nb_nodes): mip.sum_(edges_to_nodes[i, j] * u[j] for j in range(nb_edges)) <= x.data[i]
def model(self, x, grad_h, w, tau_star, Idle_index, NUmax): nb_edges = len(x.matchingGraph.edges) # The variables are the number of matching in each edge # u[i] correspond to the number of matching in state.matching_graph.edges[i] self.u = u = mip.VarVector([nb_edges], "u", mip.INT, lb=0, ub=NUmax) # The goal is to maximize grad_h*u mip.maximize(mip.sum_(grad_h[i] * u[i] for i in range(nb_edges))) ### The inequalities constraints ### # The number of matchings can not be higher than NUmax mip.sum_(u[i] for i in range(nb_edges)) <= NUmax # The number of matchings can not be higher than the number of items in the system for i in x.matchingGraph.demand_class_set: linked_edges = [ x.matchingGraph.edgeIndex((i, s)) for s in x.matchingGraph.demandToSupply[(i, )] ] mip.sum_(u[k] for k in linked_edges) <= x.demand(i) for j in x.matchingGraph.supply_class_set: linked_edges = [ x.matchingGraph.edgeIndex((d, j)) for d in x.matchingGraph.supplyToDemand[(j, )] ] mip.sum_(u[k] for k in linked_edges) <= x.supply(j) # The workload process can not be higher than the threshold mip.sum_(u[i] for i in Idle_index) <= np.maximum(-tau_star - w, 0.)
def set_objective(self, weightA: int = 10, weightB: int = 1): # \sum_{i \in K} 10 x_i + o mp.minimize(weightA * mp.sum_(self.weight[self.__all_data[uid][ToukenInfoKey.RARELITY.value]] * self.__x[uid] for uid in self.__x) + weightB * self.__over)
def solve_by_mip(p, time_limit=600, silent=True, bind=None): """Solve by MIPCL""" mpprob = mp.Problem("setcover") x = [mp.Var("x(%d)" % i, type=mp.BIN) for i in range(p.m)] mp.minimize(mp.sum_([int(p.cost[i]) * x[i] for i in range(p.m)])) for i in range(p.n): mp.sum_([x[j] for j in range(p.m) if i in p.sets[j]]) >= 1 if bind is not None: for i in bind: x[i] == bind[i] mp.optimize(silent=silent, timeLimit=time_limit) sol = np.zeros(p.m, dtype=np.int) for i in range(p.m): if x[i].val > 0.5: sol[i] = 1 assert p.is_feasible(sol) return sol
def __set_over_constr2(self): self.__over == mp.sum_(mp.sum_(self.__all_data[uid][ToukenInfoKey.UP.value][status] * self.__x[uid] for uid in self.__x) for status in self.target_status) + sum( self.__katana.max_status[status] for status in self.target_status) - self.__attack - self.__defense - self.__mobile - self.__back
def __set_status_constraint2(self): for status in self.target_status: mp.sum_(self.__all_data[uid][ToukenInfoKey.UP.value][status] * self.__x[uid] for uid in self.__x) <= self.__katana.max_status[status] - getattr(self, status)