def __init__(self, input_data, input_params): self.input_data = input_data self.input_params = input_params self.model = cpx.Model('prod_planning') self._create_decision_variables() self._create_main_constraints() self._set_objective_function()
def solve(self, adj, cost=None): u, s = np.shape(adj) U = range(1, u + 1) S = range(1, s + 1) opt_model = cpx.Model(name="SCP Model") x_vars = {i: opt_model.binary_var(name="x_{0}".format(i)) for i in S} constraints = { j: opt_model.add_constraint( ct=opt_model.sum(adj[j - 1, i - 1] * x_vars[i] for i in S) >= 1, ctname="constraint_{0}".format(j)) for j in U } if cost is not None: objective = opt_model.sum(cost[i - 1] * x_vars[i] for i in S) else: objective = opt_model.sum(x_vars[i] for i in S) time_lim = 10 opt_model.minimize(objective) opt_model.parameters.timelimit.set(time_lim) opt_model.parameters.mip.strategy.lbheur.set(1) opt_model.parameters.mip.cuts.zerohalfcut.set(2) opt_model.solve() opt_df = pd.DataFrame.from_dict(x_vars, orient="index", columns=["variable_object"]) #opt_df.reset_index(inplace=True) opt_df["solution_value"] = opt_df["variable_object"].apply( lambda item: item.solution_value) return opt_df
def __init__(self, models, L=None): self.M = models[list(models.keys())[0]].get_weights()[0].shape[ 0] # number of items in the value model = dimension of input layer self.Models = models # dict of keras models # sorted list of bidders self.sorted_bidders = list(self.Models.keys()) self.sorted_bidders.sort() self.N = len(models) # number of bidders self.Mip = cpx.Model( name="NeuralNetworksMixedIntegerProgram") # docplex instance self.z = {} # MIP variable: see paper self.s = {} # MIP variable: see paper self.y = {} # MIP variable: see paper self.x_star = np.ones( (self.N, self.M)) * (-1) # optimal allocation (-1=not yet solved) self.L = L # global big-M variable: see paper self.soltime = None # timing self.z_help = {} # helper variable for bound tightening self.s_help = {} # helper variable for bound tightening self.y_help = {} # helper variable for bound tightening # upper bounds for MIP variables z,s. Lower bounds are 0 in our setting. self.upper_bounds_z = OrderedDict( list((bidder_name, [ np.array([self.L] * layer.output.shape[1]).reshape(-1, 1) for layer in self._get_model_layers( bidder_name, layer_type=['dense', 'input']) ]) for bidder_name in self.sorted_bidders)) self.upper_bounds_s = OrderedDict( list((bidder_name, [ np.array([self.L] * layer.output.shape[1]).reshape(-1, 1) for layer in self._get_model_layers( bidder_name, layer_type=['dense', 'input']) ]) for bidder_name in self.sorted_bidders))
def init_ET_global_solution(jobs, problem): et_global_solution = ET_global_solution() # Create model opt_model = cpx.Model(name="Calculate E/T Model") opt_model.parameters.simplex.tolerances.feasibility = 0.0000001 n = len(jobs) # Decision parameters end_times = opt_model.continuous_var_list(n, lb=0, name="end_time_of_job_%s") earlis = opt_model.continuous_var_list(n, lb=0, name="earliness_of_job_%s") tardis = opt_model.continuous_var_list(n, lb=0, name="tardiness_of_job_%s") # constraint opt_model.add_constraints_( end_times[i] + problem.processing_times[jobs[i + 1]] <= end_times[i + 1] for i in range(n - 1)) # constraint opt_model.add_constraints_(end_times[i] + earlis[i] - tardis[i] == problem.due_dates[jobs[i]] for i in range(n)) # Objective function objective_function = opt_model.sum( earlis[i] * problem.earliness_penalties[jobs[i]] + tardis[i] * problem.tardiness_penalties[jobs[i]] for i in range(n)) # minimize objective opt_model.minimize(objective_function) opt_model.solve() block_obj = 0 et_global_solution.block_starttimes.append( opt_model.solution.get_value("end_time_of_job_" + str(0)) - problem.processing_times[jobs[0]]) for i in range(n - 1): end1 = opt_model.solution.get_value("end_time_of_job_" + str(i)) end2 = opt_model.solution.get_value("end_time_of_job_" + str(i + 1)) block_obj += opt_model.solution.get_value( "earliness_of_job_" + str(i)) * problem.earliness_penalties[ jobs[i]] + opt_model.solution.get_value( "tardiness_of_job_" + str(i)) * problem.tardiness_penalties[jobs[i]] if end1 + problem.processing_times[jobs[i + 1]] != end2: et_global_solution.block_lasts.append(i) et_global_solution.block_endtimes.append(end1) et_global_solution.block_objs.append(block_obj) et_global_solution.block_starttimes.append( end2 - problem.processing_times[jobs[i + 1]]) block_obj = 0 if i == n - 2: et_global_solution.block_lasts.append(i + 1) et_global_solution.block_endtimes.append(end2) block_obj += opt_model.solution.get_value("earliness_of_job_" + str(i + 1)) * problem.earliness_penalties[ jobs[i + 1]] + opt_model.solution.get_value("tardiness_of_job_" + str(i + 1)) * \ problem.tardiness_penalties[jobs[i + 1]] et_global_solution.block_objs.append(block_obj) return et_global_solution
def __init__(self, bids): self.bids = bids # list of numpy nxd arrays representing elicited bundle-value pairs from each bidder. n=number of elicited bids, d = number of items + 1(value for that bundle). self.N = len(bids) # number of bidders self.M = bids[0].shape[1] - 1 # number of items self.Mip = cpx.Model(name="WDP") # cplex model self.K = [x.shape[0] for x in bids] # number of elicited bids per bidder self.z = {} # decision variables. z(i,k) = 1 <=> bidder i gets the kth bundle out of 1,...,K[i] from his set of bundle-value pairs self.x_star = np.zeros((self.N, self.M)) # optimal allocation of the winner determination problem
def __createModelCPLEX(self): self.model = cpx.Model(self.instanceName) print("CPLEX - Creating Variables...") #Variables for f in self.facilities: self.varFacilityAssignment[f.index] = self.model.binary_var( name="facility-%s" % f.index) for c in self.customers: #Demand is binary because each customer must be served by exaclty one facility self.varCustomerAssignment[f.index, c.index] = self.model.binary_var( name="demand-(%s,%s)" % (f.index, c.index)) print("CPLEX - Creating Constraints...") #Constraints #Ensure all customers are assigned to one facility for customer in self.customers: self.model.add_constraint(ct=self.model.sum( self.varCustomerAssignment[facility.index, customer.index] for facility in self.facilities) == 1, ctname="Demand(%s)" % customer.index) #Ensure the demand carried by the facility is at most its capacity for facility in self.facilities: self.model.add_constraint( ct=self.model.sum(self.varCustomerAssignment[facility.index, customer.index] * customer.demand for customer in self.customers) <= facility.capacity * self.varFacilityAssignment[facility.index], ctname="Capacity(%s)" % facility.index) #Strong Formulation for facility in self.facilities: for customer in self.customers: self.model.add_constraint( ct=self.varCustomerAssignment[facility.index, customer.index] <= facility.capacity * self.varFacilityAssignment[facility.index], ctname="Strong(%s,%s)" % (facility.index, customer.index)) self.model.add_constraint( self.varCustomerAssignment[facility.index, customer.index] >= 0, ctname="Strong2(%s,%s)" % (facility.index, customer.index)) print("CLPEX - Creating Objective Function...") #Objective Function objective = self.model.sum( self.varFacilityAssignment[facility.index] * facility.setup_cost for facility in self.facilities) + self.model.sum( Preprocessing.getEuclideanDistance(facility.location, customer.location) * self.varCustomerAssignment[facility.index, customer.index] for facility in self.facilities for customer in self.customers) self.model.minimize(objective)
def time_block(memoBT, jobs, first, last, problem): para_due_dates = problem.due_dates para_processing_times = problem.processing_times para_earliness_penalties = problem.earliness_penalties para_tardiness_penalties = problem.tardiness_penalties if memoBT[first][last].block_start >= 0: return memoBT[first][last].block_start, memoBT[first][last].block_end, memoBT[first][last].et_penalty,0 # Create model opt_model = cpx.Model(name="Block Timing Model") opt_model.parameters.simplex.tolerances.feasibility = 0.0000001 n = last - first + 1 # Decision parameters end_times = opt_model.continuous_var_list(n, lb=0, ub=utils.BIG_NUMBER, name="end_time_of_job_%s") earlis = opt_model.continuous_var_list(n, lb=0, ub=utils.BIG_NUMBER, name="earliness_of_job_%s") tardis = opt_model.continuous_var_list(n, lb=0, ub=utils.BIG_NUMBER, name="tardiness_of_job_%s") # constraint opt_model.add_constraints_( end_times[i] + para_processing_times[jobs[first + i + 1]] == end_times[i + 1] for i in range(n - 1) ) # constraint opt_model.add_constraints_( end_times[i] + earlis[i] - tardis[i] == para_due_dates[jobs[first + i]] for i in range(n) ) # Objective function objective_function = opt_model.sum( earlis[i] * para_earliness_penalties[jobs[first + i]] + tardis[i] * para_tardiness_penalties[jobs[first + i]] for i in range(n) ) # minimize objective opt_model.minimize(objective_function) # print("**************** Block Timing ****************") # opt_model.print_information() start = time.process_time() opt_model.solve() end = time.process_time() cplex_time = end - start block_end = opt_model.solution.get_value("end_time_of_job_" + str(n - 1)) block_start = opt_model.solution.get_value("end_time_of_job_0") - para_processing_times[jobs[first]] et_penalty = opt_model.solution.get_objective_value() memoBT[first][last].block_start = block_start memoBT[first][last].block_end = block_end memoBT[first][last].et_penalty = et_penalty # print("block start:", block_start) # print("block end:", block_end) # print("E/T penalty:", et_penalty) # print("**************** Block Timing Finished ****************") return block_start, block_end, et_penalty,cplex_time
def init_problem(data, neginf=-1e5): model = cpx.Model(name="MIP Model") T = data['T'] NS = data['num_slopes'] EC, ED = (1 / data['efc']), data['efd'] LOAD = data['load'] prices = data['price'][:, :data['num_slopes']] offsets = build_price(data['price'], data['num_slopes']) t_vars = { j: model.continuous_var(lb=neginf, name="t_{0}".format(j)) for j in range(T) } c_vars = { t: model.continuous_var(lb=0, ub=data['dmax'], name="c_{0}".format(t)) for t in range(T) } d_vars = { t: model.continuous_var(lb=0, ub=data['dmin'], name="d_{0}".format(t)) for t in range(T) } CUB = data['bmax'] - data['charge'] CLB = data['bmin'] - data['charge'] cons_charge_ub = { t: model.add_constraint(ct=model.sum(c_vars[j] - d_vars[j] for j in range(t + 1)) <= CUB, ctname="charge_ub_{0}".format(t)) for t in range(T) } cons_charge_lb = { t: model.add_constraint(ct=model.sum(c_vars[j] - d_vars[j] for j in range(t + 1)) >= CLB, ctname="charge_lb_{0}".format(t)) for t in range(T) } cons_cost = {(t, s): model.add_constraint( ct=prices[t][s] * model.sum(EC * c_vars[t] - ED * d_vars[t]) - t_vars[t] <= -offsets[t][s] - prices[t][s] * LOAD[t], ctname="cost_{0}_{1}".format(t, s)) for t in range(T) for s in range(NS)} objective = model.sum(t_vars[j] for j in range(T)) model.minimize(objective) CONS = [cons_charge_ub, cons_charge_lb, cons_cost] VARS = [t_vars, c_vars, d_vars] return model, CONS, VARS
def asign_charges(batuse): """ Determines how to assign the different charging points of the battery to the discharging points of the battery using the LP formulation. Parameters ----------- batuse: np.ndarray An array with the battery action. Positive if charging, negative if discharging. Returns ------------- asig : np.ndarray Assignament of the different charing points to the discharing times. """ opt_model = cpx.Model(name="Battery") N = batuse.shape[0] set_I = range(0, N) set_J = range(0, N) y = {j: -min(0, batuse[j]) for j in set_J} z = {i: max(0, batuse[i]) for i in set_I} x_vars = {(i, j): opt_model.continuous_var(lb=0.0, name="x_{0}_{1}".format(i, j)) for i in set_I for j in set_J} constraint1 = { j: opt_model.add_constraint(ct=opt_model.sum(x_vars[i, j] for i in set_I if i < j) == y[j], ctname="constraint1_{0}".format(j)) for j in set_J } constraint2 = { i: opt_model.add_constraint(ct=opt_model.sum(x_vars[i, j] for j in set_J) <= z[i], ctname="constraint2_{0}".format(i)) for i in set_I } objective = opt_model.sum(0) opt_model.minimize(objective) opt_model.solve() return x_vars
def __init__(self, bids): # list of numpy nxd arrays representing bundle-value pairs from each bidder. # n=number of elicited bids + 1(the number of units for each item) # d = number of items + 1(value for that bundle). self.bids = bids self.N = len(bids)-1 # number of bidders self.M = bids[0].shape[1] - 1 # number of items self.Mip = cpx.Model(name="WDP") # cplex model self.z = {} # decision variables. z(i) = 1 <=> bidder i gets the bundle self.x_star = np.zeros((self.N, self.M)) # optimal allocation of the wdp self.z_star = np.zeros((1,self.N))
def __init__(self, area_shape, amount_of_drones_used, location_charging_points, minimum_charge, name, logging_obj, scanning_time, scanning_consumption, plotting=True, area_length=3, area_width=3, resolution=1): # Define instance variables self.name = name # Name of the excel sheet in which it saves self.wb = logging_obj # Name of the object so everything is logged in the same file self.area_shape = area_shape # Which shape is chosen by the user self.area_width = area_width # Defines the width in case a rectangle is chosen self.area_length = area_length # Defines the length in case a rectangle is chosen self.resolution = resolution # Resolution of the points in case a rectangle is chosen self.drone_amount = amount_of_drones_used # Amount of the drones used to run the optimization self.cm = cpx.Model('Modelname') # Name of the gurobi model self.area_coord = [ ] # List in list, [node number, x_location node, y_location node] self.i = 0 # i is the name of the node self.x_vars = { } # Dictionary with the links, [node i, node j, drone k] self.result = [] # List with the output of the optimization self.q = {} # Dictionary with charges, [node i, drone k] self.time = {} # Dictionary with the time, [node i, drone k] self.M = 1000 # Big M value, slack self.value_list = [ ] # List in list which contains the possible nodes it can reach from another node self.new_value_list = [] # value_list but rearranged self.amount_of_links = 0 # Counter which counts the total amount of links (including all the drones) self.obj_time = 0 # Represents the time component of the objective function self.obj_dist = 0 # Represents the distance component of the objective function self.obj_charge = 0 # Represents the charge component of the objective function self.obj_function = 0 # Represents the objective function self.location_charg_point = location_charging_points # List in list with the location of the charging points self.amount_of_charg_stat = len( location_charging_points) # Amount of charging points used self.charge_scan = scanning_consumption # Battery charge it takes to scan a landmark self.scan_time = scanning_time # Time it takes to scan a landmark self.min_charge = minimum_charge # Minimum charge it needs to have at all time self.charge_nodes = [0] # Node 0 is always a charge node self.charge_constant = 1.43 # Battery consumption per kilometer in cruise self.time_constant = 0.857 # Time it takes to fly 1 kilometer in cruise self.charge_time = 1.32 # Calculated time it takes to charge 1 percent in minutes self.amount_visits_charg = 2 # Amount a drone can visit a charging point self.amount_of_landmarks = 0 # Amount of landmarks which need to be visited self.landing_takeoff_penalty = 8 # This value is chosen to account for the landing and takeoff time etc. self.plotting = plotting # Regulates if plotting is on or not.
def __init__(self, lower_search_bounds: np.array, upper_search_bounds: np.array, big_m: int = 20, upper_bound: int = 10, time_limit: Union[int, None] = None, relative_gap: Union[float, None] = None, integrality: Union[float, None] = None, log_ouput: bool = False, tighten_IA: bool = False) -> NoReturn: """default constructor :param lower_search_bounds: lower bounds of the search area must have same dimensions as the sample x space) :param upper_search_bounds: upper bounds of the search area must have same dimensions as the sample x space) :param big_m: M from the big M-constraint :param upper_bound: upper bounds for tighten_bounds_IA :param time_limit: time limit for the cplex solver :param relative_gap: relative gap limit for the cplex solver :param integrality: integrality limit for cplex solver :param log_ouput: log the cplex optimization logs :param tighten_IA: integrality limit for cplex solver """ super().__init__(lower_search_bounds, upper_search_bounds) self.upper_bound = upper_bound self.y_main: dict = { } # vector denoting which node in the corresponding layer is active self.z_main: dict = { } # positive components of the output value o of each layer self.s_main: dict = { } # slack variable representing absolute value of the negative components of output o self.y_side: dict = { } # vector denoting which node in the corresponding layer is active self.z_side: dict = { } # positive components of the output value o of each layer self.s_side: dict = { } # slack variable representing absolute value of the negative components of output o self.big_l = big_m self.cpx_MIP = cpx.Model("NN_MIP") self.layers_main = None self.layers_side = None self.upper_bounds_z_main: dict = {} self.upper_bounds_s_main: dict = {} self.upper_bounds_z_side: dict = {} self.upper_bounds_s_side: dict = {} self.upper_search_bounds = upper_search_bounds self.lower_search_bounds = lower_search_bounds self.Model = None self.time_limit = time_limit self.relative_gap = relative_gap self.integrality = integrality self.log_ouput = log_ouput self.tighten_IA = tighten_IA
def lp_max(coef, budget): """ Solve min_a coef.A + intercept, subject to sum A = budget. :return: """ L = len(coef) model = dcpm.Model(name="lp_max") vars = {i: model.binary_var(name="trt_{}".format(i)) for i in range(L)} obj = model.sum(vars[i] * float(coef[i]) for i in range(L)) model.add_constraint(model.sum(vars[i] for i in range(L)) == budget) model.minimize(obj) sol = model.solve(url=None, key=None) argmax = np.array([int(sol.get_value(f'trt_{i}')) for i in range(L)]) return argmax
def solve(self, adj): u, s = np.shape(adj) U = range(1, u + 1) S = range(1, s + 1) opt_model = cpx.Model(name="SCP Model") x_vars = {i: opt_model.binary_var(name="x_{0}".format(i)) for i in S} constraints = {j: opt_model.add_constraint(ct=opt_model.sum(adj[j - 1, i - 1] * x_vars[i] for i in S) >= 1, ctname="constraint_{0}".format(j)) for j in U} objective = opt_model.sum(x_vars[i] for i in S) opt_model.minimize(objective) opt_model.solve() opt_df = pd.DataFrame.from_dict(x_vars, orient="index", columns=["variable_object"]) opt_df.reset_index(inplace=True) opt_df["solution_value"] = opt_df["variable_object"].apply(lambda item: item.solution_value) return opt_df
def best_attack(x, S, t, k): start_program = time.time() m, n = S.shape model = cpx.Model(name="LP Model") a = {i: model.binary_var(name="a_" + str(i)) for i in range(n)} z = {j: model.binary_var(name="z_" + str(j)) for j in range(m)} v = {(i, j): model.binary_var(name="v_" + str(i) + "_" + str(j)) for i in range(n) for j in range(m)} model.add_constraint(model.sum(a[i] for i in range(n)) <= k) # for j in range(m): # s = S[j] # model.add_constraint( model.sum( z[j] * t[i] * (1 - s[i]) * a[i] for i in range(n)) >= 0 ) for j in range(m): s = S[j] model.add_constraint( model.sum(z[j] * t[i] - (1 - s[i]) * v[i, j] * t[i] for i in range(n)) >= 0) for i in range(n): for j in range(m): model.add_constraint(v[i, j] <= z[j]) model.add_constraint(v[i, j] <= a[i]) model.add_constraint(v[i, j] >= z[j] + a[i] - 1) objective = model.sum((1 - z[j]) * x[j] for j in range(m)) model.minimize(objective) # model.print_information() # print() model.solve() a_return = [] for i in range(n): value = int(a[i]) a_return.append(value) return np.array(a_return).reshape((1, np.array(a_return).shape[0]))
def new_model(self, mdl=None, name=None): """ Creates a new model Parameters ---------- mdl : :obj:`docplex.mp.model.Model` Cplex model (default None) name : string Name of the model (default None) Returns ------- Model : :obj:`docplex.mp.model.Model` Cplex model """ return cpx.Model()
def best_defense(y, A, t, l): start_program = time.time() m, n = A.shape model = cpx.Model(name="LP Model") s = {i: model.binary_var(name="s_" + str(i)) for i in range(n)} z = {j: model.binary_var(name="z_" + str(j)) for j in range(m)} v = {(i, j): model.binary_var(name="v_" + str(i) + "_" + str(j)) for i in range(n) for j in range(m)} model.add_constraint(model.sum(s[i] for i in range(n)) <= l) for j in range(m): a = A[j] model.add_constraint( model.sum(z[j] * t[i] * (1 - a[i]) + v[i, j] * t[i] * a[i] + z[j] for i in range(n)) <= 0) for i in range(n): for j in range(m): model.add_constraint(v[i, j] <= z[j]) model.add_constraint(v[i, j] <= s[i]) model.add_constraint(v[i, j] >= z[j] + s[i] - 1) objective = model.sum(z[j] * y[j] for j in range(m)) model.maximize(objective) # model.print_information() # print() model.solve() s_return = [] for i in range(n): value = int(s[i]) s_return.append(value) return np.array(s_return).reshape((1, np.array(s_return).shape[0]))
def _initSolver(self): self.orCntIndex = 0 self.nr_vms = self.problem.nrVM self.nr_comps = self.problem.nrComp self.offers_list = self.problem.offers_list ### Option: default-s-b self.model = cpx.Model(name="Manuver Model") self.model.parameters.timelimit.set(2400.0) ### Option: reduce-set-lpmethod-default-s-b: # http://www-eio.upc.es/lceio/manuals/cplex-11/html/refparameterscplex/refparameterscplex103.html#271905 #self.model.parameters.preprocessing.reduce.set(1) # print(self.model.parameters.lpmethod) # self.model.parameters.lpmethod =3 # print(self.model.parameters.lpmethod) ### sym breaking options ### Option: 0 no-s-b #self.model.parameters.preprocessing.symmetry = 0 ### Option: 1 Exert a moderate level of symmetry breaking #self.model.parameters.preprocessing.symmetry = 1 # ### Option: 2 Exert an aggressive level of symmetry breaking #self.model.parameters.preprocessing.symmetry = 2 # ### Option: 3 Exert a very aggressive level of symmetry breaking #self.model.parameters.preprocessing.symmetry = 3 # ### Option: 4 Exert a highly aggressive level of symmetry breaking #self.model.parameters.preprocessing.symmetry = 4 # ### Option: 5 Exert an extremely aggressive level of symmetry breaking self.model.parameters.preprocessing.symmetry = 5 #0-default CPX_ALG_AUTOMATIC #2-CPX_ALG_DUAL #3-CPX_ALG_NET # CPXPARAM_Preprocessing_Reduce # 1 # CPXPARAM_MIP_Tolerances_AbsMIPGap # 0 # CPXPARAM_MIP_Tolerances_MIPGap # 0 self._define_variables()
def __init__( self, demand=600, # veh / hr / lane sat_flow_rate=1800, # veh / hr / lane flow_rate_reduction=1, g_min=6, # sec g_max=20, # sec time_step=2, # sec / timestep time_range=30, # timesteps (for ease of gauging the program's size) r_left=0.25, r_through=0.5, r_right=0.25, alpha=1, preload=None, model_name='Base Extended Model'): self.model_name = model_name self.model = cpx.Model(name=self.model_name) # Convert all units from seconds to timesteps self.time_step = time_step self.time_range = time_range if (isinstance(demand, int)): demand = list([demand] * 4) elif (isinstance(demand, tuple)): if (len(demand) == 2): demand = list(demand) + list(demand) else: demand = list(demand) self.sat_flow_rate = (float)(sat_flow_rate * time_step) / ( 3600) # veh / timesteps / lane self.demand = [(float)(x * time_step) / (3600) for x in demand] # veh / timesteps / lane self.g_min = g_min / time_step # timesteps self.g_max = g_max / time_step # timesteps self.cell_length = self.cell_length * time_step # ft (scaled) self.turn_ratios = [r_left, r_through, r_right] self.flow_rate_reduction = flow_rate_reduction self.alpha = alpha self.preload = preload
def create_master_problem(J, N_T, N, T, q_true, pi_graph, weight_reg_long_path = 0): """ Create Master Problem model 4 step Master Problem in the first iteration, 2 step in the continuous iteration :param J: list J parameter Master Problem :param N_T: list list of tuple (i_n,t_m), parameter Master Problem :param N: list N parameter Master Problem :param T: list T parameter Master Problem :param q_true: dict q_true parameter Master Problem :param pi_graph: list list of paths as input pool of Master Problem :param weight_reg_long_path: int const weight if want to add regularizer that increase search long_path(with great contribution). I scenario see doc. :return: Model Master Problem model :return: dict dict of (node,time step) that appear in a specific path (identified by index) :return: var x parameter Master Problem :return: var e parameter Master Problem """ mld = cplex.Model('Master problem') x = mld.continuous_var_dict(J, lb=0, name='x') e = mld.continuous_var_dict(N_T, lb=0, name='e') paths_by_node = build_master_problem_contraints(pi_graph, mld, x, e, q_true) if weight_reg_long_path != 0: cotruire espressione e sistemare concatenazione #mld.minimize(mld.sum(e[i, t]) + weight_reg_long_path*x[j] for i in N for t in T for j in paths_by_node[i, t])) else: mld.minimize(mld.sum(e[i, t] for i in N for t in T)) return mld, paths_by_node, x, e
def find_initial_sol(self, Nmin, Nmax): """ Search for an unconstrained solution to warmstart the solver """ print("Doing init with warmstart") OP_initial = cpx.Model(name="Wind Farm Layout Warmstart", log_output=True) x_vars = {(i): OP_initial.binary_var(name="x_{0}".format(i)) for i in self.setV} OP_initial.add_constraint( OP_initial.sum(x_vars[i] for i in self.setV) <= Nmax) OP_initial.add_constraint( OP_initial.sum(x_vars[i] for i in self.setV) >= Nmin) for cnt in range(0, self.n): for el in self.setE[cnt]: OP_initial.add_constraint(x_vars[cnt] + x_vars[el] <= 1) obj = OP_initial.sum(self.Pi * x_vars[i] for i in self.setV) OP_initial.maximize(obj) OP_initial.solve() self.initial_sol = [ OP_initial.solution.get_value(x_vars[element]) for element in self.setV ] initial_sol_indices = [] cnt = 0 for element in self.setV: if OP_initial.solution.get_value(x_vars[element]) == 1: initial_sol_indices.append(cnt) cnt += 1 # save this filename = "wfl_initial_sol_" + str(self.axx) + "_" + str( self.axy) + ".npy" with open(os.path.join(self.dir_sol, filename), "wb") as sol_file: np.save(sol_file, initial_sol_indices)
# In[130]: for obj in OD_infor: obj.find_sp(nx.dijkstra_path(X,obj.origin,obj.destination,weight='length')) obj.find_sd(nx.dijkstra_path_length(X,obj.origin,obj.destination,weight='length')) obj.vector_p(); # In[153]: import docplex.mp.model as cpx opt_model = cpx.Model(name="MIP Model") b_v_x = opt_model.binary_var_list(338, name='x') b_v_y = opt_model.binary_var_list(len(OD_infor)+1, name='y') p = 15; for obj in OD_infor: opt_model.add_constraint(opt_model.sum(opt_model.scal_prod(b_v_x,obj.v_p)) >= b_v_y[obj.number]) opt_model.add_constraint(opt_model.sum(b_v_x) <= p) opt_model.maximize(opt_model.scal_prod(b_v_y[1:],df_r[4])) # In[154]:
def match(self, X, Y, storeDistance=False): # Take the two graphs - they have already the same size self.X = X self.Y = Y if (self.X.nodes() == 1 and self.Y.nodes() == 1): print("No Edges") self.f = list(self.Y.x.keys())[0][0] return nX = self.X.nodes() # set of non-zero nodes (i,i) that are in X or in Y # note. assuming that if there is an edge (i,j), both i and j have non-zero attribute isetn = set((i, j) for ((i, j), y) in self.X.x.items() if y != [0] if i == j).union( set((i, j) for ((i, j), y) in self.Y.x.items() if y != [0] if i == j)) isetn = sorted(isetn) # set of indices i of non-zero nodes (i,i) that are in X or in Y isetnn = [i for (i, j) in isetn] # set of edges btw non-zero nodes that are in X or in Y isete = [(i, j) for i in isetnn for j in isetnn if i != j] # building up the matrix of pairwise distances: # matrix of pairwise distances btw nodes: x_vec_n = self.X.to_vector_with_select_nodes(isetn) y_vec_n = self.Y.to_vector_with_select_nodes(isetn) gas_n = pd.DataFrame(pairwise_distances(x_vec_n, y_vec_n, metric=self.metricNode), columns=y_vec_n.index, index=x_vec_n.index) del x_vec_n, y_vec_n # matrix of pairwise distances btw edges: try: x_vec_e = self.X.to_vector_with_select_edges(isete) y_vec_e = self.Y.to_vector_with_select_edges(isete) gas_e = pd.DataFrame(pairwise_distances(x_vec_e, y_vec_e, metric=self.metricEdge), columns=y_vec_e.index, index=x_vec_e.index) del x_vec_e, y_vec_e except: # degenerate graphs if self.X.edge_attr + self.Y.edge_attr == 0: # if both the two graphs have no edge gas_e = pd.DataFrame() if self.X.edge_attr * self.Y.edge_attr == 0: # if one of the two graphs has no edge self.X.edge_attr = self.Y.edge_attr = max( self.X.edge_attr, self.Y.edge_attr) x_vec_e = self.X.to_vector_with_select_edges(isete) y_vec_e = self.Y.to_vector_with_select_edges(isete) gas_e = pd.DataFrame(pairwise_distances( x_vec_e, y_vec_e, metric=self.metricEdge), columns=y_vec_e.index, index=x_vec_e.index) del x_vec_e, y_vec_e # optimization model: # initialize the model # opt_model = cpx.Model(name="HP Model") opt_model = cpx.Model(name="HP Model", ignore_names=True, checker='off') # faster # list of binary variables: 1 if i match j, 0 otherwise # x_vars is n x n # x_vars = {(i, u): opt_model.binary_var(name="x_{0}_{1}".format(i, u)) # for i in isetnn # for u in isetnn} x_vars = opt_model.binary_var_matrix(isetnn, isetnn, name="x") # constraints - imposing that there is a one to one correspondence between the nodes in the two networks opt_model.add_constraints_( (opt_model.sum(x_vars[i, u] for i in isetnn) == 1 for u in isetnn), (f"constraint_r{u}" for u in isetnn)) opt_model.add_constraints_( (opt_model.sum(x_vars[i, u] for u in isetnn) == 1 for i in isetnn), (f"constraint_c{i}" for i in isetnn)) # objective function - sum the distance between nodes and the distance between edges # e.g. (i,i) is a node in X, (u,u) is a node in Y, (i,j) is an edge in X, (u,v) is an edge in Y. objective = opt_model.sum( x_vars[i, u] * gas_n.loc[f'({i}, {i})', f'({u}, {u})'] for i in isetnn for u in isetnn) + opt_model.sum( x_vars[i, u] * x_vars[j, v] * gas_e.loc[f'({i}, {j})', f'({u}, {v})'] for (i, j) in isete # for i in isetnn for j in isetnn if j!=i for (u, v) in isete) # for u in isetnn for v in isetnn if v!=u # Minimizing the distances as specified in the objective function opt_model.minimize(objective) # Finding the minimum opt_model.solve() if storeDistance: self.distance = opt_model.solution.get_objective_value() # Save in f the permutation: <3 ff = [k for k, z in x_vars.items() if z.solution_value == 1] if len(ff) < nX: # if the number of nodes involved in the matching, i.e. non-zero nodes, is smaller than the total, # set up the permutation vector in the proper way # e.g. X nodes 1,3 Y nodes 1,4 -> isetnn={1,3,4} -> len(x_vars>0)=3 # -> i want to avoid st. like f=[4,1,3] because i want len(f)=nX self.f = list(range(nX)) for (i, u) in ff: self.f[i] = u else: self.f = [u for (i, u) in ff] try: del gas_n, gas_e except AttributeError: pass del opt_model
I_0: initial_inventory c_t: unit production cost in month t d_t: demand of month t Variables: X_t: Amount produced in month t I_t: Inventory at the end of period t Constraints: Inventory Constraints: I_{t-1} + X_t - d_t = I_t Capacity Constraints: X_t <= p Objective: Min Sum(h*I_t + c_t*X_t) """ model = cpx.Model('prod_planning') start = time() # ================== Decision variables ================== production_variables = model.continuous_var_dict(input_df_dict['input_data'].index, name="X") inventory_variables = model.continuous_var_dict(input_df_dict['input_data'].index, name="I") # Alternative way of creating the variables # production_variables = {index: model.continuous_var(name='X_' + str(row['period'])) # for index, row in input_df_dict['input_data'].iterrows()} # # inventory_variables = {index: model.continuous_var(name='I_' + str(row['period'])) # for index, row in input_df_dict['input_data'].iterrows()} logger.debug("var declaration time: {:.6f}".format(time() - start))
def fit_additive_approx_with_solver(U1, U2, weight=1., residual_weight=1.): nA1, nA2 = U1.shape nA = nA1 * nA2 model = dcpm.Model(name='model') vars = {} # Add variables for each player for i in range(nA1): vars[(0, 0, i)] = model.continuous_var(name=f'{(0, 0, i)}', lb=-2) vars[(1, 0, i)] = model.continuous_var(name=f'{(1, 0, i)}') for j in range(nA2): if i == 0: vars[(0, 1, j)] = model.continuous_var(name=f'{(0, 1, j)}', lb=-2) vars[(1, 1, j)] = model.continuous_var(name=f'{(1, 1, j)}', lb=-2) vars[(2, i, j)] = model.continuous_var(name=f'{(2, i, j)}', lb=0) # vars[(3, i, j)] = model.continuous_var(name=f'{(3, i, j)}', lb=0) # vars[(4, i, j)] = model.continuous_var(name=f'{(4, i, j)}', lb=0) # Dummy variables for l1 penalty vars[(5, i, j)] = model.continuous_var(name=f'{(5, i, j)}', lb=0) vars[(6, i, j)] = model.continuous_var(name=f'{(6, i, j)}', lb=0) cost1 = model.sum( (U1[i, j] - vars[(0, 0, i)] - vars[(0, 1, j)] - vars[(2, i, j)])**2 for i in range(nA1) for j in range(nA2)) cost2 = model.sum( (U2[i, j] - vars[(1, 1, i)] - vars[(1, 0, j)] - vars[(2, i, j)])**2 for i in range(nA1) for j in range(nA2)) # cost3 = model.sum(vars[2, i, j]**2 for i in range(nA1) for j in range(nA2)) cost3 = model.sum(vars[5, i, j] + vars[6, i, j] for i in range(nA1) for j in range(nA2)) # cost4 = model.sum(vars[3, i, j]**2 for i in range(nA1) for j in range(nA2)) # cost5 = model.sum(vars[4, i, j] ** 2 for i in range(nA1) for j in range(nA2)) # Constraints for l1 dummy variables for i in range(nA1): for j in range(nA2): model.add_constraint(vars[5, i, j] - vars[6, i, j] - vars[2, i, j] == 0) model.minimize(cost1 + cost2 + weight * cost3) sol = model.solve(url=None, key=None) # Get variables U11_hat = np.zeros_like(U1).astype(float) U12_hat = np.zeros_like(U1).astype(float) U21_hat = np.zeros_like(U1).astype(float) U22_hat = np.zeros_like(U1).astype(float) U1_hat = np.zeros_like(U1).astype(float) for i in range(nA1): for j in range(nA2): U11_hat[i, j] = float(sol.get_value(f'{(0, 0, i)}')) U12_hat[i, j] = float(sol.get_value(f'{(0, 1, j)}')) U21_hat[i, j] = float(sol.get_value(f'{(1, 0, i)}')) U22_hat[i, j] = float(sol.get_value(f'{(1, 1, j)}')) U1_hat[i, j] = float(sol.get_value(f'{(2, i, j)}')) return U11_hat, U12_hat, U21_hat, U22_hat, U1_hat
def solve_test1(n, m, r, s, f_cost, p_list, p_u, delta_k, beta, gamma_k, vp, ld, x_n, y_n, x_m, y_m): I = range(1, n + 1) K = range(1, m + 1) T = range(1, r + 1) L = range(1, s + 1) # vp, ld, x_n, y_n, x_m, y_m = preflist(n, m, r, s,) # [print("v_{0}_{1}_{2}=%d".format(i,k,t) % sum(l * 1 * vp[i, k, t, l] for l in L) ) for i in I for k in K for t in T] # breakpoint() delta = {k: delta_k for k in K } # limit on the individual size of the charging stations gamma = {k: gamma_k for k in K} # capacity of individual charging stations # c = {k: rnd.uniform(.6, 1) for k in K} # installation cost of individual charging stations c = {k: 1 for k in K} # installation cost of individual charging stations # print(c) v = {(i, k, t, l): vp[i, k, t, l] for i in I for k in K for t in T for l in L} # preference list l_d = {(i, k): ld[i, k] * 1 for i in I for k in K} # lambda of the follower problem # big M's M = 1000 M5 = 1000 M6 = 1000 prob = cpx.Model(name="EV Model") # primal leader and follower variables x = {(i, k, t): prob.binary_var(name="x_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} # x = {(i, k, t): prob.continuous_var(lb=0, ub=1, name="x_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} y = { k: prob.integer_var(lb=0, ub=np.inf, name="y_{0}".format(k)) for k in K } z = {k: prob.binary_var(name="z_{0}".format(k)) for k in K} p = {(k, t): prob.continuous_var(lb=0, ub=p_u, name="p_{0}_{1}".format(k, t)) for k in K for t in T} # for i in I: # print v # print(i) # for l in L: # zd = {(i, k, t, l): v[i, k, t, l] for k in K for t in T} # print(np.reshape(list(zd.values()), (m, r))) # dual follower variables pi = {(k, t): prob.continuous_var(lb=0, ub=None, name="pi_{0}_{1}".format(k, t)) for k in K for t in T} phi = {(i, k, t): prob.continuous_var(lb=0, ub=None, name="phi_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} rho = { i: prob.continuous_var(lb=-np.inf, ub=None, name="rho_{0}".format(i)) for i in I } u = {(k, t): prob.binary_var(name="u_{0}_{1}".format(k, t)) for k in K for t in T} # leader constraints con71 = { k: prob.add_constraint(ct=z[k] - prob.sum(x[i, k, t] for i in I for t in T) <= 0, ctname="con71_{0}".format(k)) for k in K } con72 = {(k, t): prob.add_constraint(ct=p[k, t] - prob.sum(x[i, k, t] for i in I for t in T) * p_u <= 0, ctname="con72_{0}_{1}".format(k, t)) for k in K for t in T} con7b = { k: prob.add_constraint(ct=y[k] - delta[k] * z[k] <= 0, ctname="con7b_{0}".format(k)) for k in K } con7c = prob.add_constraint(ct=prob.sum(c[k] * y[k] for k in K) <= beta, ctname="con7c") # follower KKT con7f = {(i, k, t): prob.add_constraint( ct=f_cost * p[k, t] + pi[k, t] + rho[i] + phi[i, k, t] >= -l_d[i, k] - prob.sum(l * p_list * v[i, k, t, l] for l in L), ctname="con7f_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} # [print("v_{0}_{1}_{2} = %d, con7f_{0}_{1}_{2} = %s".format(i, k, t,i,k,t) % (sum(l * p_list * vp[i, k, t, l] for l in L), str(con7f[i,k,t]))) for i in I for k in K for t in T] con7g = {(i, k, t): prob.add_constraint( ct=f_cost * p[k, t] + l_d[i, k] + pi[k, t] + rho[i] + phi[i, k, t] + M * x[i, k, t] <= -prob.sum(l * p_list * v[i, k, t, l] for l in L) + M, ctname="con7g_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} con7h = { (k, t): prob.add_constraint(ct=prob.sum(x[i, k, t] for i in I) - gamma[k] * y[k] <= 0, ctname="con7h_{0}_{1}".format(k, t)) for k in K for t in T } con7i = {(k, t): prob.add_constraint( ct=gamma[k] * y[k] - prob.sum(x[i, k, t] for i in I) + u[k, t] * M5 <= M5, ctname="con7i_{0}_{1}".format(k, t)) for k in K for t in T} con7j = {(k, t): prob.add_constraint(ct=pi[k, t] - u[k, t] * M5 <= 0, ctname="con7j_{0}_{1}".format(k, t)) for k in K for t in T} con7k = { i: prob.add_constraint(ct=prob.sum(x[i, k, t] for k in K for t in T) == 1, ctname="con7k_{0}".format(i)) for i in I } con7l = {(i, k, t): prob.add_constraint(ct=x[i, k, t] - prob.sum(v[i, k, t, l] for l in L) <= 0, ctname="con7l_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} con7m = {(i, k, t): prob.add_constraint(ct=phi[i, k, t] + M6 * x[i, k, t] <= M6 * (1 + prob.sum(v[i, k, t, l] for l in L)), ctname="con7m_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} # con7s = {(i, k, t): prob.add_constraint( # ct=w_linear[i, k, t] -w[i, k, t] ** 2 - 0 <= 0, # ctname="con7s_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} # objective function objective = prob.sum( x[i, k, t] * p[k, t] for i in I for k in K for t in T) - 1 * prob.sum(x[i, k, t] for i in I for k in K for t in T) - 0 * prob.sum(c[k] * y[k] for k in K) prob.maximize(objective) prob.print_information() # breakpoint() # prob.parameters.mip.tolerances.mipgap = 0.001 # prob.parameters.timelimit = 500 s1 = prob.solve(log_output=True) print(prob.get_solve_status()) # prob.print_solution() print(s1.get_objective_value()) x_val = s1.get_value_dict(x) y_val = s1.get_value_dict(y) p_val = s1.get_value_dict(p) z_val = s1.get_value_dict(z) [ print("x_{0}_{1}_{2} = %d".format(i, k, t) % x_val[i, k, t]) for i in I for k in K for t in T if x_val[i, k, t] != 0 ] [print("y_{0}=%d".format(k) % y_val[k]) for k in K if y_val[k] != 0] [print("z_{0}=%d".format(k) % z_val[k]) for k in K if z_val[k] != 0] [ print("p_{0}_{1} = %f".format(k, t) % p_val[k, t]) for k in K for t in T if p_val[k, t] != 0 ] # print(s) # plt.figure(figsize=(10, 5)) img = mpimg.imread('user_cs.png') [ plt.annotate("x_{0}_{1}_{2}".format(i, k, t), (x_n[i] - 0.002, y_n[i] - 0.04)) for i in I for k in K for t in T if x_val[i, k, t] == 1 ] [ plt.annotate("y_{0} = %d".format(k) % y[k], (x_m[k] - 0.002, y_m[k] - 0.04), c='r') for k in K if y_val[k] != 0 ] # plt.axis('equal') plt.savefig('user_cs_2level') plt.show(block=False) plt.close('user_cs_2level') return None
#路径权值,默认为距离的平方 c_vars = {(i, j): math.pow( math.pow(position_vertex_list[i][0] - position_vertex_list[j][0], 2) + math.pow(position_vertex_list[i][1] - position_vertex_list[j][1], 2), 0.5) for i in range(num_vertex) for j in range(num_vertex)} #各种集合 set_without_depot = range(1, num_vertex) set_Vs_c = range(1, num_vertex) set_V = range(num_vertex) set_Vs = range(1, num_sdf + 1) set_Vc = range(num_sdf + 1, num_vertex) set_salesman = range(0, num_salesman) set_group = range(0, num_group) set_sdf_r_c = range(0, num_sdf) #model opt_model = cpx.Model(name="CSP Model") #----------决策变量 x_vars = {(i, j): opt_model.integer_var(lb=0, ub=1, name="x_{0}_{1}".format(i, j)) for i in set_V for j in set_V} z_vars = {(i, j): opt_model.integer_var(lb=0, ub=1, name="z_{0}_{1}".format(i, j)) for i in set_Vc for j in set_Vs} u_vars = {(i, j): opt_model.integer_var(lb=0, ub=len(responsible_salesman_list[j]) - 1, name="u_{0}_{1}".format(i, j)) for i in set_V for j in set_salesman if i in responsible_salesman_list[j]}
def place_request_optimally(self, req): print("\n### OPTIMAL Placement module #################################################\n") if not os.path.isfile(CPLEX_PATH): raise RuntimeError('CPLEX does not exist ({})'.format(CPLEX_PATH)) hosts = self.topology.nodes states = req.get_masters() replicas = req.get_replicas() opt_model = cpx.Model(name="Placement") # Binary variables ################################################################################### print("Creating state mapping variables 1...") x_vars = {(h, s.id): opt_model.binary_var(name="x{0}{1}".format(h, s.id)) for h in hosts for s in states} print("Creating replica mapping variables 2...") y_vars = {(i, s.id): opt_model.binary_var(name="y{0}{1}".format(i, s.id)) for i in hosts for s in states} # Constraints ######################################################################################### print("Creating constraints 1 - states can be mapped into only one server") for s in states: c_name = "c1_{}".format(s.id) opt_model.add_constraint(ct=opt_model.sum(x_vars[i, s.id] for i in hosts) == 1, ctname=c_name) print("Creating constraints 2 - replicas can be mapped into as many servers as many replicas exist") for s in states: c_name = "c2_{}".format(s.id) # print("State: {}\t Replica number:{}\t Replica instance:{}".format(s.id, s.replica_num, # len(req.get_replicas_of_state(s)))) # opt_model.add_constraint( # ct=opt_model.sum(y_vars[i, s.id] for i in hosts) == len(req.get_replicas_of_state(s)), ctname=c_name) opt_model.add_constraint( ct=opt_model.sum(y_vars[i, s.id] for i in hosts) == s.replica_num, ctname=c_name) print("Creating constraints 3 - anti-affinity rules") for h in hosts: for s in states: c_name = "c3_{}_in_{}".format(s.id, h) opt_model.add_constraint(ct=(x_vars[h, s.id] + y_vars[h, s.id]) <= 1, ctname=c_name) print("Creating constraints 4 - server capacity constraint") for h in hosts: c_name = "c4_{}".format(h) opt_model.add_constraint( ct=opt_model.sum((x_vars[h, s.id] + y_vars[h, s.id]) * s.size for s in states) <= self.topology.nodes[h]['capacity'], ctname=c_name) print("Creating Objective function...") def create_w(): # init w w = {(k, s.id): 0 for k in hosts for s in states} for s in states: for k in hosts: writers = s.writers for i in writers: if MasterController.get_host_of_function(i) == k: w[k, s.id] += i.get_writer_rate_of_state(s) return w def create_f(): # init f f = {(i, s.id): 0 for i in hosts for s in states} for s in states: for i in hosts: writers = s.writers for w in writers: if MasterController.get_host_of_function(w) == i: f[i, s.id] = 1 return f def create_g(): # init g g = {(i, s.id): 0 for i in hosts for s in states} for s in states: for i in hosts: readers = s.readers for r in readers: if MasterController.get_host_of_function(r) == i: g[i, s.id] = 1 return g def create_p(): # init p p = {(i, s.id): 0 for i in hosts for s in states} for s in states: for i in hosts: readers = s.readers for r in readers: if MasterController.get_host_of_function(r) == i: p[i, s.id] += r.get_reader_rate_of_state(s) return p w = create_w() f = create_f() g = create_g() p = create_p() # =============================================================================================================== readers_hosts = dict() for s in states: readers_hosts.update({s.id: [MasterController.get_host_of_function(r) for r in s.readers]}) update_cost = sum(sum( x_vars[i, s.id] * y_vars[j, s.id] * MasterController.get_min_delay_between_hosts(i, j) for i in hosts for j in hosts) * sum(w[k, s.id] for k in hosts) for s in states) def get_max_expression(state): max_expression = opt_model.max([opt_model.logical_and(x_vars[(i, state.id)], y_vars[ (j, state.id)]) * MasterController.get_min_delay_between_hosts(i, j) * sum( w[(k, state.id)] for k in hosts) for i in hosts for j in hosts if i != j]) return max_expression update_cost = sum(get_max_expression(s) for s in states) # update_cost = 0 write_cost = sum( x_vars[i, s.id] * f[j, s.id] * w[j, s.id] * MasterController.get_min_delay_between_hosts(i, j) for i in hosts for j in hosts for s in states) def delta_cost(host_i, state): # FIXME: use a dynamic number instead of the 100 below min_delta = opt_model.min((1 - opt_model.logical_or(x_vars[(j, state.id)], y_vars[ (j, state.id)])) * 100 + MasterController.get_min_delay_between_hosts(host_i, j) for j in hosts) return min_delta readers_hosts = dict() for s in states: reader_hosts_list = [] for r in s.readers: host = MasterController.get_host_of_function(r) if host not in reader_hosts_list: reader_hosts_list.append(host) readers_hosts.update({s.id: reader_hosts_list}) reading_cost = sum(delta_cost(i, s) * g[(i, s.id)] * p[(i, s.id)] for s in states for i in readers_hosts[s.id]) quadratic_obj_function = update_cost + write_cost + reading_cost # =============================================================================================================== opt_model.set_objective("min", quadratic_obj_function) print("Exporting the problem") cplex_model_path = "{}/cplex_model".format(MasterController.simulation_name) opt_model.export_as_lp(basename="cplex_model", path=cplex_model_path) t1 = datetime.datetime.now() # solving problem in locally print("\n\nSolving the problem locally") subprocess.call( "{} -c 'read {}.lp' 'set timelimit {}' 'set mip interval 1' 'optimize' 'write {}_solution sol'".format( CPLEX_PATH, cplex_model_path, CPLEX_TIME_LIMIT, cplex_model_path), shell=True) t2 = datetime.datetime.now() try: with open("{}_solution".format(cplex_model_path)) as myfile: head = [next(myfile) for x in range(20)] cost = int(round(float(head[6].split('=')[1].replace('\n', '').replace('"', '')))) print("\n*** Delay cost: {} ***".format(cost)) except: print("\n\nWARNING: No solution exists!") cost = -1 # indicates whether master state s is placed onto host i x_vars = {(h, s.id): 0 for h in hosts for s in states} # indicates whether a slave replica of s is placed onto host i y_vars = {(i, s.id): 0 for i in hosts for s in states} from xml.dom import minidom # parse an xml file by name mydoc = minidom.parse("{}_solution".format(cplex_model_path)) variables = mydoc.getElementsByTagName('variable') # all item attributes print('\nAll variable:') for elem in variables: if "xhost" in elem.attributes['name'].value: if float(elem.attributes['value'].value) > 0.6: host = (elem.attributes['name'].value).split("state")[0][1:] state = "state" + (elem.attributes['name'].value).split("state")[1] x_vars[(host, state)] = 1 elif "yhost" in elem.attributes['name'].value: if float(elem.attributes['value'].value) > 0.6: host = (elem.attributes['name'].value).split("state")[0][1:] state = "state" + (elem.attributes['name'].value).split("state")[1] y_vars[(host, state)] = 1 running_time = t2 - t1 print("RUNNING TIME: {}".format(running_time)) print(x_vars) print(y_vars) return cost, running_time, x_vars, y_vars
def main(): opt_model = cpx.Model(name="MIP Model") opt_model.context.cplex_parameters.workmem = 1024 n = int(sys.stdin.readline()) cur = sys.stdin.readline() variables = {} has_edge = {} opt = None nodes = set() while cur != "": u, v, weight = cur.split(" ") u, v, weight = int(u), int(v), float(weight) isInfWeight = weight == -math.inf nodes.add(u) nodes.add(v) key = (u, v) var = opt_model.integer_var(lb=0, ub=1, name="x_{0}_{1}".format(u, v)) if not isInfWeight: weight = int(weight) cur_opt = abs(weight) * var if weight > 0: cur_opt = (1 - var) * weight if opt is None: opt = cur_opt else: opt += cur_opt variables[key] = var has_edge[key] = weight > 0 cur = sys.stdin.readline() nodes = list(nodes) len_nodes = len(nodes) c_id = 0 for u_idx in range(len_nodes): u = nodes[u_idx] for v_idx in range(u_idx + 1, len_nodes): v = nodes[v_idx] for w_idx in range(v_idx + 1, len_nodes): w = nodes[w_idx] c_id += 1 opt_model.add_constraint( ct=(variables[(u, v)] + variables[(v, w)] - variables[(u, w)] <= 1), ctname=str(c_id)) c_id += 1 opt_model.add_constraint( ct=(variables[(u, v)] - variables[(v, w)] + variables[(u, w)] <= 1), ctname=str(c_id)) c_id += 1 opt_model.add_constraint( ct=(-variables[(u, v)] + variables[(v, w)] + variables[(u, w)] <= 1), ctname=str(c_id)) # for minimization opt_model.minimize(opt) has_sol = opt_model.solve() if has_sol: for key in variables: sol_has_edge = int(float(variables[key])) == 1 if (sol_has_edge and not has_edge[key]) or (not sol_has_edge and has_edge[key]): print(str(key[0]) + " " + str(key[1]))
def solve_problem_static(I, K, T, L, f_cost, p_list, p_u, delta_k, beta, gamma_k, vp, ld, x_n, y_n, x_m, y_m): # I = range(1, n + 1) # K = range(1, m + 1) # T = range(1, r + 1) # L = range(1, s + 1) # vp, ld, x_n, y_n, x_m, y_m = preflist(n, m, r, s,) # [print("v_{0}_{1}_{2}=%d".format(i,k,t) % sum(l * 1 * vp[i, k, t, l] for l in L) ) for i in I for k in K for t in T] # breakpoint() delta = {k: delta_k for k in K} # limit on the individual size of the charging stations gamma = {k: gamma_k for k in K} # capacity of individual charging stations # c = {k: rnd.uniform(.6, 1) for k in K} # installation cost of individual charging stations c = {k: 1 for k in K} # installation cost of individual charging stations # print(c) v = {(i, k, t, l): vp[i, k, t, l] for i in I for k in K for t in T for l in L} # preference list l_d = {(i, k): ld[i, k] * 1 for i in I for k in K} # lambda of the follower problem # big M's M = 1000 M5 = 1000 M6 = 1000 prob = cpx.Model(name="EV Model") # primal leader and follower variables x = {(i, k, t): prob.binary_var(name="x_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} # x = {(i, k, t): prob.continuous_var(lb=0, ub=1, name="x_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} y = {k: prob.integer_var(lb=0, ub=np.inf, name="y_{0}".format(k)) for k in K} z = {k: prob.binary_var(name="z_{0}".format(k)) for k in K} p = {(k, t): prob.continuous_var(lb=0, ub=np.inf, name="p_{0}_{1}".format(k, t)) for k in K for t in T} # for i in I: # print v # print(i) # for l in L: # zd = {(i, k, t, l): v[i, k, t, l] for k in K for t in T} # print(np.reshape(list(zd.values()), (m, r))) # dual follower variables pi = {(k, t): prob.continuous_var(lb=0, ub=None, name="pi_{0}_{1}".format(k, t)) for k in K for t in T} phi = {(i, k, t): prob.continuous_var(lb=0, ub=None, name="phi_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} rho = {i: prob.continuous_var(lb=-np.inf, ub=None, name="rho_{0}".format(i)) for i in I} u = {(k, t): prob.binary_var(name="u_{0}_{1}".format(k, t)) for k in K for t in T} # leader constraints con71 = { k: prob.add_constraint(ct=z[k] - prob.sum(x[i, k, t] for i in I for t in T) <= 0, ctname="con71_{0}".format(k)) for k in K} con72 = {(k, t): prob.add_constraint(ct=p[k, t] - prob.sum(x[i, k, t] for i in I) * p_u <= 0, ctname="con72_{0}_{1}".format(k, t)) for k in K for t in T} con7b = {k: prob.add_constraint(ct=y[k] - delta[k] * z[k] <= 0, ctname="con7b_{0}".format(k)) for k in K} con7c = prob.add_constraint(ct=prob.sum(c[k] * y[k] for k in K) <= beta, ctname="con7c") # follower KKT con7f = {(i, k, t): prob.add_constraint( ct=f_cost * p[k, t] + pi[k, t] + rho[i] + phi[ i, k, t] >= - l_d[i, k] - prob.sum(l * p_list * v[i, k, t, l] for l in L), ctname="con7f_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} # [print("v_{0}_{1}_{2} = %d, con7f_{0}_{1}_{2} = %s".format(i, k, t,i,k,t) % (sum(l * p_list * vp[i, k, t, l] for l in L), str(con7f[i,k,t]))) for i in I for k in K for t in T] con7g = {(i, k, t): prob.add_constraint( ct=f_cost * p[k, t] + l_d[i, k] + pi[k, t] + rho[i] + phi[ i, k, t] + M * x[i, k, t] <= - prob.sum(l * p_list * v[i, k, t, l] for l in L) + M, ctname="con7g_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} con7h = {(k, t): prob.add_constraint( ct=prob.sum(x[i, k, t] for i in I) - gamma[k] * y[k] <= 0, ctname="con7h_{0}_{1}".format(k, t)) for k in K for t in T} con7i = {(k, t): prob.add_constraint( ct=gamma[k] * y[k] - prob.sum(x[i, k, t] for i in I) + u[k, t] * M5 <= M5, ctname="con7i_{0}_{1}".format(k, t)) for k in K for t in T} con7j = {(k, t): prob.add_constraint( ct=pi[k, t] - u[k, t] * M5 <= 0, ctname="con7j_{0}_{1}".format(k, t)) for k in K for t in T} con7k = {i: prob.add_constraint( ct=prob.sum(x[i, k, t] for k in K for t in T) == 1, ctname="con7k_{0}".format(i)) for i in I} con7l = {(i, k, t): prob.add_constraint( ct=x[i, k, t] - prob.sum(v[i, k, t, l] for l in L) <= 0, ctname="con7l_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} con7m = {(i, k, t): prob.add_constraint( ct=phi[i, k, t] + M6 * x[i, k, t] <= M6 * (1 + prob.sum(v[i, k, t, l] for l in L)), ctname="con7m_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} # con7s = {(i, k, t): prob.add_constraint( # ct=w_linear[i, k, t] -w[i, k, t] ** 2 - 0 <= 0, # ctname="con7s_{0}_{1}_{2}".format(i, k, t)) for i in I for k in K for t in T} # objective function objective = prob.sum(x[i, k, t] * p[k, t] for i in I for k in K for t in T) - 1 * prob.sum( x[i, k, t] for i in I for k in K for t in T) - 0 * prob.sum(c[k] * y[k] for k in K) prob.maximize(objective) prob.print_information() # breakpoint() # prob.parameters.mip.tolerances.mipgap = 0.001 # prob.parameters.timelimit = 500 sol_static = prob.solve(log_output=True) print(prob.get_solve_status()) # prob.print_solution() print(sol_static.get_objective_value()) var = {"x": x, "y": y, "z": z, "p": p} return sol_static, var