def process(self, times, vehicles, d, pmax, pmin, emin, emax, efinal, peak_shaving, penalization, solver="gurobi"): """The process function creates the pyomo model and solve it. Minimize sum( net_load(t) + sum(power_demand(t, v)))**2 subject to: pmin(t, v) <= power_demand(t, v) <= pmax(t, v) emin(t, v) <= sum(power_demand(t, v)) <= emax(t, v) sum(power_demand(t, v)) >= efinal(v) rampmin(t) <= net_load_ramp(t) + power_demand_ramp(t, v) <= rampmax(t) Args: times (list): timestep list vehicles (list): unique list of vehicle ids d (dict): time - net load at t pmax (dict): (time, id) - power maximum at t for v pmin (dict): (time, id) - power minimum at t for v emin (dict): (time, id) - energy minimum at t for v emax (dict): (time, id) - energy maximum at t for v efinal (dict): id - final SOC solver (string): name of the solver to use (default is gurobi) Return: model (ConcreteModel), result """ # Select gurobi solver with SolverFactory(solver) as opt: # Solver option see Gurobi website # opt.options['Method'] = 1 # Creation of a Concrete Model model = ConcreteModel() # ###### Set model.t = Set(initialize=times, doc='Time', ordered=True) last_t = model.t.last() model.v = Set(initialize=vehicles, doc='Vehicles') # ###### Parameters # Net load model.d = Param(model.t, initialize=d, doc='Net load') # Power model.p_max = Param(model.t, model.v, initialize=pmax, doc='P max') model.p_min = Param(model.t, model.v, initialize=pmin, doc='P min') # Energy model.e_min = Param(model.t, model.v, initialize=emin, doc='E min') model.e_max = Param(model.t, model.v, initialize=emax, doc='E max') model.e_final = Param(model.v, initialize=efinal, doc='final energy balance') # model.beta = Param(initialize=beta, doc='beta') # ###### Variable model.u = Var(model.t, model.v, domain=Integers, doc='Power used') # ###### Rules def maximum_power_rule(model, t, v): return model.u[t, v] <= model.p_max[t, v] model.power_max_rule = Constraint(model.t, model.v, rule=maximum_power_rule, doc='P max rule') def minimum_power_rule(model, t, v): return model.u[t, v] >= model.p_min[t, v] model.power_min_rule = Constraint(model.t, model.v, rule=minimum_power_rule, doc='P min rule') def minimum_energy_rule(model, t, v): return sum(model.u[i, v] for i in range(0, t + 1)) >= model.e_min[t, v] model.minimum_energy_rule = Constraint(model.t, model.v, rule=minimum_energy_rule, doc='E min rule') def maximum_energy_rule(model, t, v): return sum(model.u[i, v] for i in range(0, t + 1)) <= model.e_max[t, v] model.maximum_energy_rule = Constraint(model.t, model.v, rule=maximum_energy_rule, doc='E max rule') def final_energy_balance(model, v): return sum(model.u[i, v] for i in model.t) >= model.e_final[v] model.final_energy_rule = Constraint(model.v, rule=final_energy_balance, doc='E final rule') # Set the objective to be either peak shaving or ramp mitigation if peak_shaving == 'peak_shaving': def objective_rule(model): return sum([ (model.d[t] + sum([model.u[t, v] for v in model.v]))**2 for t in model.t ]) model.objective = Objective(rule=objective_rule, sense=minimize, doc='Define objective function') elif peak_shaving == 'penalized_peak_shaving': def objective_rule(model): return (sum([ (model.d[t] + sum([model.u[t, v] for v in model.v]))**2 for t in model.t ]) + penalization * sum([ sum([model.u[t, v]**2 for v in model.v]) for t in model.t ])) model.objective = Objective(rule=objective_rule, sense=minimize, doc='Define objective function') elif peak_shaving == 'ramp_mitigation': def objective_rule(model): return sum([(model.d[t + 1] - model.d[t] + sum( [model.u[t + 1, v] - model.u[t, v] for v in model.v]))**2 for t in model.t if t != last_t]) model.objective = Objective(rule=objective_rule, sense=minimize, doc='Define objective function') results = opt.solve(model) # results.write() return model, results
#create file names filename = path+"in"+ver+".pkl" D = pickle.load(open(filename,'rb')) filesave = path+model+"_"+ver+"/" #solve shrinking horizon model for i in range(100): #create environmnet with user specified demand user_D = {(1,0): D[:,i]} #assign scenario to retail/market link sample_path = {(1,0): True} #note that the specified demand is sampled from the prob distribution env = or_gym.make("InvManagement-"+ver, env_config={'user_D': user_D, 'sample_path': sample_path}) #create model m=net_im_lp_model(env,perfect_information=True,use_expectation=True) #select solver s=SolverFactory(solver) res=s.solve(m, tee=False) if res['Solver'][0]['Termination condition'][:] != 'optimal': print("Case " + str(i) + " - ERROR: NOT OPTIMAL") else: print("Case " + str(i) + " - " + str(np.sum(list(m.P.get_values().values())))) #extract and pickle data X = m.X.get_values() for key in X.keys(): env.X[key[1]][key[0]] = X[key] Y = m.Y.get_values() for key in Y.keys(): env.Y[key[1:]][key[0]] = Y[key] R = m.R.get_values() for key in R.keys(): env.R[key[1:]][key[0]] = R[key]
def solve_linear_GDP(linear_GDP_model, solve_data, config): """Solves the linear GDP model and attempts to resolve solution issues.""" m = linear_GDP_model GDPopt = m.GDPopt_utils # Transform disjunctions _bigm = TransformationFactory('gdp.bigm') _bigm.handlers[Port] = False _bigm.apply_to(m) preprocessing_transformations = [ # # Propagate variable bounds # 'contrib.propagate_eq_var_bounds', # # Detect fixed variables # 'contrib.detect_fixed_vars', # # Propagate fixed variables # 'contrib.propagate_fixed_vars', # # Remove zero terms in linear expressions # 'contrib.remove_zero_terms', # # Remove terms in equal to zero summations # 'contrib.propagate_zero_sum', # # Transform bound constraints # 'contrib.constraints_to_var_bounds', # # Detect fixed variables # 'contrib.detect_fixed_vars', # # Remove terms in equal to zero summations # 'contrib.propagate_zero_sum', # Remove trivial constraints 'contrib.deactivate_trivial_constraints', ] if config.mip_presolve: try: fbbt(m, integer_tol=config.integer_tolerance) for xfrm in preprocessing_transformations: TransformationFactory(xfrm).apply_to(m) except InfeasibleConstraintException: config.logger.debug("MIP preprocessing detected infeasibility.") mip_result = MasterProblemResult() mip_result.feasible = False mip_result.var_values = list(v.value for v in GDPopt.variable_list) mip_result.pyomo_results = SolverResults() mip_result.pyomo_results.solver.termination_condition = tc.error mip_result.disjunct_values = list(disj.binary_indicator_var.value for disj in GDPopt.disjunct_list) return mip_result # Deactivate extraneous IMPORT/EXPORT suffixes getattr(m, 'ipopt_zL_out', _DoNothing()).deactivate() getattr(m, 'ipopt_zU_out', _DoNothing()).deactivate() # Create solver, check availability if not SolverFactory(config.mip_solver).available(): raise RuntimeError("MIP solver %s is not available." % config.mip_solver) # Callback immediately before solving MIP master problem config.call_before_master_solve(m, solve_data) try: with SuppressInfeasibleWarning(): mip_args = dict(config.mip_solver_args) elapsed = get_main_elapsed_time(solve_data.timing) remaining = max(config.time_limit - elapsed, 1) if config.mip_solver == 'gams': mip_args['add_options'] = mip_args.get('add_options', []) mip_args['add_options'].append('option reslim=%s;' % remaining) elif config.mip_solver == 'multisolve': mip_args['time_limit'] = min( mip_args.get('time_limit', float('inf')), remaining) results = SolverFactory(config.mip_solver).solve(m, **mip_args) except RuntimeError as e: if 'GAMS encountered an error during solve.' in str(e): config.logger.warning( "GAMS encountered an error in solve. Treating as infeasible.") mip_result = MasterProblemResult() mip_result.feasible = False mip_result.var_values = list(v.value for v in GDPopt.variable_list) mip_result.pyomo_results = SolverResults() mip_result.pyomo_results.solver.termination_condition = tc.error mip_result.disjunct_values = list(disj.binary_indicator_var.value for disj in GDPopt.disjunct_list) return mip_result else: raise terminate_cond = results.solver.termination_condition if terminate_cond is tc.infeasibleOrUnbounded: # Linear solvers will sometimes tell me that it's infeasible or # unbounded during presolve, but fails to distinguish. We need to # resolve with a solver option flag on. results, terminate_cond = distinguish_mip_infeasible_or_unbounded( m, config) if terminate_cond is tc.unbounded: # Solution is unbounded. Add an arbitrary bound to the objective and # resolve. This occurs when the objective is nonlinear. The nonlinear # objective is moved to the constraints, and deactivated for the linear # master problem. obj_bound = 1E15 config.logger.warning( 'Linear GDP was unbounded. ' 'Resolving with arbitrary bound values of (-{0:.10g}, {0:.10g}) ' 'on the objective. ' 'Check your initialization routine.'.format(obj_bound)) main_objective = next(m.component_data_objects(Objective, active=True)) GDPopt.objective_bound = Constraint(expr=(-obj_bound, main_objective.expr, obj_bound)) with SuppressInfeasibleWarning(): results = SolverFactory(config.mip_solver).solve( m, **config.mip_solver_args) terminate_cond = results.solver.termination_condition # Build and return results object mip_result = MasterProblemResult() mip_result.feasible = True mip_result.var_values = list(v.value for v in GDPopt.variable_list) mip_result.pyomo_results = results mip_result.disjunct_values = list(disj.binary_indicator_var.value for disj in GDPopt.disjunct_list) if terminate_cond in {tc.optimal, tc.locallyOptimal, tc.feasible}: pass elif terminate_cond is tc.infeasible: config.logger.info( 'Linear GDP is now infeasible. ' 'GDPopt has finished exploring feasible discrete configurations.') mip_result.feasible = False elif terminate_cond is tc.maxTimeLimit: # TODO check that status is actually ok and everything is feasible config.logger.info( 'Unable to optimize linear GDP problem within time limit. ' 'Using current solver feasible solution.') elif (terminate_cond is tc.other and results.solution.status is SolutionStatus.feasible): # load the solution and suppress the warning message by setting # solver status to ok. config.logger.info('Linear GDP solver reported feasible solution, ' 'but not guaranteed to be optimal.') else: raise ValueError('GDPopt unable to handle linear GDP ' 'termination condition ' 'of %s. Solver message: %s' % (terminate_cond, results.solver.message)) return mip_result
""" ####################################################################################################### # a basic unit commitment model for CAISO system # # This is the trial version of the electricity market model # # 4 Zone system # # ####################################################################################################### from __future__ import division # This line is used to ensure that int or long division arguments are converted to floating point values before division is performed from pyomo.environ import * # This command makes the symbols used by Pyomo known to Python from pyomo.opt import SolverFactory import itertools ##Create a solver opt = SolverFactory('cplex') model = AbstractModel() # ###################################################################### ## string indentifiers for the set of generators in different zones. # ###################################################################### # model.Zone5Gas = Set() model.Zone5Generators = Set() #PNW model.Coal = Set() model.Gas = model.Zone5Gas model.Oil = Set()
def optimize_dist(threshold, cost_matrix, pow_range_matrix, distance_matrix, demand_coherent_area, dist_cost_coherent_area, mip_gap, obj_case=1, full_load_hours=3000, max_pipe_length=3000, logFile_path=None): # st = time.time() ''' distance_matrix = np.round(distance_matrix[:7,:7]) demand_coherent_area = np.round(demand_coherent_area[:7]) dist_cost_coherent_area = dist_cost_coherent_area[:7] Number of coherent areas: n demand_coherent_area (n x 1) [MWh]: demand in each coherent area dist_cost_coherent_area (n x 1) [EUR]: distribution cost of each coherent area transmision_line_cost (n x n) [EUR/m]: constant value for the cost of transmission line ''' max_cap = 1000 # range of x variables BigM = 10**6 if len(demand_coherent_area) == 0: term_cond = False dh = np.zeros(6) edge_list = [] return term_cond, dh, np.array(edge_list) if len(demand_coherent_area) == 1: term_cond = True dh = np.zeros(7) dh[0] = 1 covered_demand = demand_coherent_area dist_inv = demand_coherent_area * dist_cost_coherent_area dist_spec_cost = dist_inv / covered_demand trans_inv = 0 trans_spec_cost = 0 trans_line_length = 0 dh[1: 7] = covered_demand, dist_inv, dist_spec_cost, trans_inv, \ trans_spec_cost, trans_line_length edge_list = [] return term_cond, dh, np.array(edge_list) G = np.argmax(demand_coherent_area) G2 = np.argsort(demand_coherent_area)[-2] n = demand_coherent_area.shape[0] ''' # cut to more distant areas # Keep only connections to the x closest Areas x = min(7.0, n-1) distance_matrix_orig = distance_matrix.copy() max_pipe_length2 = np.percentile(distance_matrix, (1 + x) / n * 100.0, axis=0) mask = (np.ones((n, 1), dtype="f4") * max_pipe_length2) NotCloseRegions = (distance_matrix > mask) # if distance_matrix[a, b] > mask and therefore, should be filtered. # accordingly, distance_matrix[b, a] should be filtered as well. Filter_NotCloseRegions = np.logical_and(NotCloseRegions, NotCloseRegions.T) # Assess which regions belong to Category Large Areas ( x Regions) x = min(10.0, n) min_energy_large_area = np.percentile(demand_coherent_area, (1 - x / n) * 100, axis=0) is_large_area_vct = demand_coherent_area >= min_energy_large_area #Matrix: mxm Larger areas are true, others are false maskLA= (np.ones((n, 1), dtype="f4") * is_large_area_vct) # returns nxn matrix showing each coherent area is smaller than which ones maskIsSmallerA = ((np.ones((n, 1), dtype="f4") * demand_coherent_area) < (np.ones((n, 1), dtype="f4") * demand_coherent_area).T) maskLA[maskIsSmallerA] = False # Store the distance of an Area (rows) to all larger Large Areas (in Columns) distance_matrix2 = distance_matrix_orig.copy() # assign a large distance (BigM) to those coherent areas that are not # LA (large area). distance_matrix2[maskLA == False] = BigM # assign a large distance (BigM) to distance from itself with an exception # of greatest coherent area (G). EYE = np.eye(distance_matrix.shape[0], dtype="f4") * BigM EYE[G, G] = 0 EYE[G2, G2] = 0 distance_matrix2 += EYE # Distance of Areas (in rows) to the second closest larger Large Area # but only if distance2closest_LargeArea = np.zeros(n, dtype="f4") idx = np.argsort(distance_matrix2, axis=1)[:, :2] count__ = 0 for i in range(n): dist2closest = distance_matrix_orig[i, idx[i, 0]] dist2_2ndclosest = distance_matrix_orig[i, idx[i, 1]] dist_1to2nd = distance_matrix_orig[idx[i, 1], idx[i, 0]] # if dist2closest=0, it means it is the largest area and the next # largest area is the closest large area! if dist2closest == 0: distance2closest_LargeArea[i] = dist2_2ndclosest count__ += 1 continue if dist2_2ndclosest == 0: distance2closest_LargeArea[i] = dist2closest count__ += 1 continue cos_ = ((dist2closest ** 2 + dist2_2ndclosest ** 2 - dist_1to2nd ** 2)/(2 * dist2closest * dist2_2ndclosest)) if dist2_2ndclosest > dist_1to2nd: # add_2nd_largest = False distance2closest_LargeArea[i] = dist2closest continue elif cos_ <= -1 or cos_ >= 1: distance2closest_LargeArea[i] = dist2_2ndclosest count__ += 1 continue else: arc_between = np.arccos(cos_) * 180 / np.pi if (arc_between < 20): # add_2nd_largest = False distance2closest_LargeArea[i] = dist2closest elif arc_between < (20 + 35 * (dist2_2ndclosest / dist2closest / 1.2 - 1)): # add_2nd_largest = False distance2closest_LargeArea[i] = dist2closest else: distance2closest_LargeArea[i] = dist2_2ndclosest count__ += 1 # print("Allowed connection to 2nd most distant large area: %s" % count__) # Look for closest Large Areas m2 = (distance_matrix2 <= (np.ones((n, 1), dtype="f4") * distance2closest_LargeArea).T) # Build Filter Matrix: True if connection to close Larger Area # Therefore logical_or Filter_CloseLargeAreas = np.logical_or(m2, m2.T) FilterNoConnection = np.logical_and(Filter_NotCloseRegions , Filter_CloseLargeAreas == False) distance_matrix[FilterNoConnection] = BigM # print(np.sum(np.logical_and(distance_matrix <= BigM, distance_matrix > 0))) fix_to_zero_index = np.argwhere(distance_matrix >= max_pipe_length) # print("Connections : %i" %(distance_matrix.size - fix_to_zero_index.shape[0] - n)) # np.savetxt('fix_to_zero_index.csv', fix_to_zero_index, delimiter=",") # distance_matrix3: Distance to a Large Area distance_matrix3 = distance_matrix.copy() distance_matrix3[maskLA==False] = BigM HasConnection2LargerArea_Vctr = np.zeros(n, dtype="int8") # HasConnection2LargerArea_Vctr == 1, if a short distance is available HasConnection2LargerArea_Vctr[np.min(distance_matrix3, axis=1) < max_pipe_length] = 1 for i in range(10): # Built Matrix: For each Area (in Rows), specify if connected Area is # is Connected to a Larger Large Area # Loop to check if it can handle it forward to such a region HasConnection2LargerArea_Vctr_prev = HasConnection2LargerArea_Vctr.copy() HasConnection2LargArea_Mtrx = np.ones((n, 1), dtype="int8") * HasConnection2LargerArea_Vctr D = distance_matrix.copy() D[HasConnection2LargArea_Mtrx==False] = BigM HasConnection2LargerArea_Vctr[np.min(D, axis=1) < max_pipe_length] = 1 if (np.sum(HasConnection2LargerArea_Vctr_prev) == np.sum(HasConnection2LargerArea_Vctr)): break ########################################################################### ########################################################################### AddConection = np.zeros((n, n)).astype(bool) ########################################################################### ########################################################################### if min(HasConnection2LargerArea_Vctr) == 0: # Some regions have no connection to a larger Large Region # print(HasConnection2LargerArea_Vctr) HasConnection2LargArea_Mtrx = np.ones((n, 1), dtype="int8") * HasConnection2LargerArea_Vctr distance_matrix3 = distance_matrix_orig.copy() distance_matrix3[HasConnection2LargArea_Mtrx==0] = BigM ClosestDistance2ConnectedArea = np.min(distance_matrix3, axis=1) AddConection = (distance_matrix_orig < (np.ones((n, 1), dtype="f4") * ClosestDistance2ConnectedArea * 1.15)) # Remove Regions which are already connected AddConection[HasConnection2LargerArea_Vctr == 1, :] = False AddConection = np.maximum(AddConection, AddConection.T) AddConection[distance_matrix <= max_pipe_length] = False distance_matrix[AddConection] = distance_matrix_orig[AddConection] fix_to_zero_index = np.argwhere(np.logical_and( distance_matrix >= max_pipe_length , AddConection==False)) # np.savetxt('AddConection.csv', AddConection, delimiter=",") # np.savetxt('distance_matrix_final.csv', distance_matrix, delimiter=",") # print("Connections after additional Connections: %i" %(distance_matrix.size - fix_to_zero_index.shape[0] - n)) ''' m = en.ConcreteModel() solver = SolverFactory('gurobi', solver_io='python') # the gap between the lower and upper objective bound solver.options["MIPGap"] = mip_gap # the relative difference between the primal and dual objective value solver.options["BarConvTol"] = 1e-1 # set to 1 if you are interested in feasible solutions # set to 2 if no problem with finding good quality solution exist and you want to focus on optimality # set to 3 if the best objective bound is moving very slowly (or not at all), to focus on bound solver.options["MIPFocus"] = 3 # memory used. the rest will be written in the hard drive. # solver.options["NodefileStart"] = 0.5 # number of threads used by the solver # solver.options["Threads"] = 2 solver.options["TimeLimit"] = 300 # ########################################################################## # ########## Sets: # ########################################################################## m.index_row = en.RangeSet(0, n - 1) m.index_col = en.RangeSet(0, n - 1) # ########################################################################## # ########## Parameters: # ########################################################################## m.th = en.Param(m.index_row, initialize=threshold) m.cap_up = en.Param(initialize=np.sum(demand_coherent_area) - demand_coherent_area[G]) def demand(m, i): return demand_coherent_area[i] m.q = en.Param(m.index_row, initialize=demand) def distribution_cost(m, i): return dist_cost_coherent_area[i] m.dist_cost = en.Param(m.index_row, initialize=distribution_cost) def l_length(m, i, j): return distance_matrix[i, j] m.line_length = en.Param(m.index_row, m.index_col, initialize=l_length) # ########################################################################## # ########## Variables: # ########################################################################## m.q_bool = en.Var(m.index_row, domain=en.Binary, initialize=1) m.l_bool = en.Var(m.index_row, m.index_col, domain=en.Binary, initialize=0) m.line_capacity = en.Var(m.index_row, m.index_col, domain=en.NonNegativeReals, bounds=(0, max_cap), initialize=0) m.line_cost = en.Var(m.index_row, m.index_col, domain=en.NonNegativeReals, initialize=0) # set the largest demand zone to be part of the result m.q_bool[G].fix(1) for i in m.index_row: m.l_bool[i, G].fix(0) m.l_bool[i, i].fix(0) m.line_capacity[i, G].fix(0) m.line_capacity[i, i].fix(0) for i in m.index_row: for j in m.index_col: if distance_matrix[i, j] > max_pipe_length: m.l_bool[i, j].fix(0) m.line_capacity[i, j].fix(0) ''' for i in range(len(demand_coherent_area)): # Lowest transmission line capacity is 0.2 Mw. Intercept of the cost function # in the piecewise linear expresion is 242.87. It should be set to zero if the # capacity is less than 0.2 MW if demand_coherent_area[i]/full_load_hours < 0.2: for j in range(len(demand_coherent_area)): m.l_bool[i, j].fix(0) m.line_capacity[i, j].fix(0) m.l_bool[j, i].fix(0) m.line_capacity[j, i].fix(0) ''' # ########################################################################## # ########## Constraints: # ########################################################################## def overall_cost_rule(m): return sum(m.line_cost[i, j] * m.line_length[i, j] for i in m.index_row for j in m.index_col) <= \ sum(m.q_bool[i]*m.q[i]*(m.th[i]-m.dist_cost[i]) for i in m.index_row) m.overall_cost = en.Constraint(rule=overall_cost_rule) def max_edge_number_rule(m): return sum(m.l_bool[i, j] for i in m.index_row for j in m.index_col) == sum(m.q_bool[i] for i in m.index_row) - 1 m.max_edge_number = en.Constraint(rule=max_edge_number_rule) def edge_connectivity_rule(m, i): if i == G: # l_bool[x, G] are already fixed to zero return en.Constraint.Skip else: return m.q_bool[i] <= sum(m.l_bool[j, i] for j in m.index_row) m.edge_connectivity = en.Constraint(m.index_row, rule=edge_connectivity_rule) # m.edge_connectivity_0 = en.Constraint(expr=1 <= sum(m.l_bool[G, j] # for j in m.index_col)) def edge_connectivity_2_rule(m, i, j): if i == G: return en.Constraint.Skip else: return m.l_bool[i, j] <= sum(m.l_bool[h, i] for h in m.index_row if h != j) m.edge_connectivity_2 = en.Constraint(m.index_row, m.index_col, rule=edge_connectivity_2_rule) def edge_connectivity_3_rule(m, i): if i == G: return en.Constraint.Skip else: return sum(m.l_bool[h, i] for h in m.index_row) <= 1 m.edge_connectivity_3 = en.Constraint(m.index_row, rule=edge_connectivity_3_rule) def edge_active_rule(m, i, j): return 2 * (m.l_bool[i, j] + m.l_bool[j, i]) <= m.q_bool[i] + m.q_bool[j] m.edge_active = en.Constraint(m.index_row, m.index_col, rule=edge_active_rule) def capacity_lower_bound_rule(m, i, j): return m.line_capacity[i, j] >= (m.l_bool[i, j] * m.q[j]) / full_load_hours m.capacity_lower_bound = en.Constraint(m.index_row, m.index_col, rule=capacity_lower_bound_rule) def capacity_upper_bound_rule(m, i, j): return m.line_capacity[i, j] <= \ (sum(m.q[h] for h in m.index_row if (h != G and h != i)) - sum(m.l_bool[h, i] * m.q[h] for h in m.index_row if h != G))/full_load_hours m.capacity_upper_bound = en.Constraint(m.index_row, m.index_col, rule=capacity_upper_bound_rule) def force_cap_to_zero_rule(m, i, j): return m.line_capacity[i, j] - m.l_bool[i, j] * m.cap_up <= 0 m.force_cap_to_zero = en.Constraint(m.index_row, m.index_col, rule=force_cap_to_zero_rule) ''' def force_cap_to_zero_2_rule(m, i, j): return m.line_capacity[i, j] + 0.99 >= m.l_bool[i, j] m.force_cap_to_zero_2 = en.Constraint(m.index_row, m.index_col, rule=force_cap_to_zero_2_rule) ''' def capacity_flow_rule(m, i): if i == G: return sum(m.line_capacity[G, h] for h in m.index_col) == \ sum(m.q_bool[h]*m.q[h] for h in m.index_row if h != G)/full_load_hours else: return sum(m.line_capacity[h, i] for h in m.index_row) - \ sum(m.line_capacity[i, h] for h in m.index_col) == \ m.q_bool[i]*m.q[i]/full_load_hours m.capacity_flow = en.Constraint(m.index_row, rule=capacity_flow_rule) def f(m, i, j, x): if x >= pow_range_matrix[0] and x < pow_range_matrix[1]: return x * (cost_matrix[1] - cost_matrix[0]) / ( pow_range_matrix[1] - pow_range_matrix[0]) elif x >= pow_range_matrix[1] and x < pow_range_matrix[2]: return (x - pow_range_matrix[1]) * ( cost_matrix[2] - cost_matrix[1]) / ( pow_range_matrix[2] - pow_range_matrix[1]) + cost_matrix[1] elif x >= pow_range_matrix[2] and x < pow_range_matrix[3]: return (x - pow_range_matrix[2]) * ( cost_matrix[3] - cost_matrix[2]) / ( pow_range_matrix[3] - pow_range_matrix[2]) + cost_matrix[2] elif x >= pow_range_matrix[3] and x < pow_range_matrix[4]: return (x - pow_range_matrix[3]) * ( cost_matrix[4] - cost_matrix[3]) / ( pow_range_matrix[4] - pow_range_matrix[3]) + cost_matrix[3] elif x >= pow_range_matrix[4] and x < pow_range_matrix[5]: return (x - pow_range_matrix[4]) * ( cost_matrix[5] - cost_matrix[4]) / ( pow_range_matrix[5] - pow_range_matrix[4]) + cost_matrix[4] elif x >= pow_range_matrix[5] and x < pow_range_matrix[6]: return (x - pow_range_matrix[5]) * ( cost_matrix[6] - cost_matrix[5]) / ( pow_range_matrix[6] - pow_range_matrix[5]) + cost_matrix[5] elif x >= pow_range_matrix[6] and x < pow_range_matrix[7]: return (x - pow_range_matrix[6]) * ( cost_matrix[7] - cost_matrix[6]) / ( pow_range_matrix[7] - pow_range_matrix[6]) + cost_matrix[6] elif x >= pow_range_matrix[7] and x < pow_range_matrix[8]: return (x - pow_range_matrix[7]) * ( cost_matrix[8] - cost_matrix[7]) / ( pow_range_matrix[8] - pow_range_matrix[7]) + cost_matrix[7] elif x >= pow_range_matrix[8] and x < pow_range_matrix[9]: return (x - pow_range_matrix[8]) * ( cost_matrix[9] - cost_matrix[8]) / ( pow_range_matrix[9] - pow_range_matrix[8]) + cost_matrix[8] elif x >= pow_range_matrix[9] and x < pow_range_matrix[10]: return (x - pow_range_matrix[9]) * ( cost_matrix[10] - cost_matrix[9] ) / (pow_range_matrix[10] - pow_range_matrix[9]) + cost_matrix[9] elif x >= pow_range_matrix[10] and x < pow_range_matrix[11]: return (x - pow_range_matrix[10]) * ( cost_matrix[11] - cost_matrix[10] ) / (pow_range_matrix[11] - pow_range_matrix[10]) + cost_matrix[10] elif x >= pow_range_matrix[11] and x < pow_range_matrix[12]: return (x - pow_range_matrix[11]) * ( cost_matrix[12] - cost_matrix[11] ) / (pow_range_matrix[12] - pow_range_matrix[11]) + cost_matrix[11] elif x >= pow_range_matrix[12] and x < pow_range_matrix[13]: return (x - pow_range_matrix[12]) * ( cost_matrix[13] - cost_matrix[12] ) / (pow_range_matrix[13] - pow_range_matrix[12]) + cost_matrix[12] elif x >= pow_range_matrix[13] and x < pow_range_matrix[14]: return (x - pow_range_matrix[13]) * ( cost_matrix[14] - cost_matrix[13] ) / (pow_range_matrix[14] - pow_range_matrix[13]) + cost_matrix[13] elif x >= pow_range_matrix[14] and x < pow_range_matrix[15]: return (x - pow_range_matrix[14]) * ( cost_matrix[15] - cost_matrix[14] ) / (pow_range_matrix[15] - pow_range_matrix[14]) + cost_matrix[14] else: return (x - pow_range_matrix[15]) * ( cost_matrix[16] - cost_matrix[15] ) / (pow_range_matrix[16] - pow_range_matrix[15]) + cost_matrix[15] m.pw_con = en.Piecewise(m.index_row, m.index_col, m.line_cost, m.line_capacity, pw_pts=list(pow_range_matrix), pw_constr_type='EQ', f_rule=f) def obj_rule_1(m): # OBJ1: Revenue-Oriented Prize Collecting return sum(m.th[i]*m.q_bool[i]*m.q[i] for i in m.index_row) - \ sum(m.line_cost[i, j] * m.line_length[i, j] for i in m.index_row for j in m.index_col) def obj_rule_2(m): # OBJ2: Profit-Oriented Prize Collectiing return sum((m.th[i]-m.dist_cost[i])*m.q_bool[i]*m.q[i] for i in m.index_row) - \ sum(m.line_cost[i, j] * m.line_length[i, j] for i in m.index_row for j in m.index_col) if obj_case == 1: m.obj = en.Objective(rule=obj_rule_1, sense=en.maximize) elif obj_case == 2: m.obj = en.Objective(rule=obj_rule_2, sense=en.maximize) else: raise ValueError('Objective method is selected wrongly! Please enter ' '"1" for Revenue-Oriented Prize Collection or "2" ' 'for Profit-Oriented Prize Collection') results = solver.solve(m, report_timing=False, tee=False, logfile="gurobi.log") print("Solver Termination: ", results.solver.termination_condition) print("Solver Status: ", results.solver.status) term_cond = results.solver.termination_condition == TerminationCondition.optimal if results.solver.status == SolverStatus.aborted: term_cond = "aborted" ''' ################## # save variable values to a csv file var_names = [name(v) for v in m.component_objects(Var)] list_of_vars = [value(v[index]) for v in m.component_objects(Var) for index in v] df = pd.DataFrame() result_series = pd.Series(list_of_vars, index=var_names) result_series.to_csv(outCSV) if done: results.write() print('obejctive = ', value(m.obj)) ################### ''' edge_list = [] dist_inv = 0 trans_inv = 0 covered_demand = 0 trans_line_length = 0 dh = np.zeros(n + 6) for i in range(n): dh[i] = en.value(m.q_bool[i]) covered_demand += demand_coherent_area[i] * en.value(m.q_bool[i]) dist_inv += dh[i] * demand_coherent_area[i] * dist_cost_coherent_area[i] for i in range(n): for j in range(n): if en.value(m.l_bool[i, j]) > 0: trans_inv += en.value(m.line_cost[i, j]) * \ en.value(m.line_length[i, j]) trans_line_length += en.value(m.line_length[i, j]) edge_list.append([i, j, en.value(m.line_capacity[i, j])]) ''' for i in range(n): for j in range(n): if en.value(m.l_bool[i, j]) > 0: print(i, " , ", j, "\t capacity: ", en.value(m.line_capacity[i, j]), "\t line: ", en.value(m.l_bool[i, j])) print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") for i in range(n): for j in range(n): if en.value(m.line_capacity[i, j]) > 0: print(i, " , ", j, "\t capacity: ", en.value(m.line_capacity[i, j]), "\t line: ", en.value(m.l_bool[i, j])) ''' # meter to km trans_line_length /= 1000 dist_spec_cost = dist_inv / covered_demand trans_spec_cost = trans_inv / covered_demand dh[n: n+6] = covered_demand, dist_inv, dist_spec_cost, trans_inv, \ trans_spec_cost, trans_line_length return term_cond, dh, np.array(edge_list)
import pyomo.environ as pyo from pyomo.opt import SolverFactory ## ----------------------- MODELO ------------------------------- opt = SolverFactory('cplex', executable="C:\\Program Files\\IBM\\ILOG\\CPLEX_Studio128\\cplex\\bin\\x64_win64\\cplex") #opt = SolverFactory('glpk') model = pyo.AbstractModel() ## --------------------- CONJUNTOS ---------------------------- model.REF = pyo.Set() model.RACKS = pyo.Set() ## ---------------------- PARÁMETROS ---------------------------- model.AnchoCaja = pyo.Param( model.REF ) model.TipoDist = pyo.Param( model.REF ) model.Espacios = pyo.Param( model.REF, mutable=True ) model.TipoRack = pyo.Param( model.RACKS ) model.Pared = pyo.Param( model.RACKS ) model.Utilizacion = pyo.Param( model.RACKS ) model.Demanda = pyo.Param( model.REF ) model.Frecuencia = pyo.Param( model.REF ) model.Costo = pyo.Param( model.RACKS ) ## ---------------------- VARIABLES ---------------------------- model.x = pyo.Var( model.REF, model.RACKS, domain = pyo.Binary ) model.y = pyo.Var( model.RACKS, domain = pyo.Binary ) ## ---------------------- FUNCIÓN OBJETIVO ---------------------------- def ObjFunc( model ): return sum(model.Demanda[ref]*model.Frecuencia[ref]*model.Costo[rack]*model.x[ref,rack] for ref in model.REF for rack in model.RACKS )
def criticalityCheck(self, x, y, z, rom_params, worstcase=False, M=[0.0]): model = self.model self.setVarValue(x=x, y=y, z=z) self.setBound(x, y, z, 1e10) self.deactiveExtraConObj() self.activateRomCons(x, rom_params) optGJH = SolverFactory('contrib.gjh') optGJH.solve(model, tee=False, symbolic_solver_labels=True) g, J, varlist, conlist = model._gjh_info l = ConcreteModel() l.v = Var(varlist, domain=Reals) for i in varlist: #dummy = model.find_component(i) l.v[i] = 0.0 l.v[i].setlb(-1.0) l.v[i].setub(1.0) if worstcase: if M.all() == 0.0: print( 'WARNING: worstcase criticality was requested but Jacobian error bound is zero' ) l.t = Var(range(0, self.ly), domain=Reals) for i in range(0, self.ly): l.t[i].setlb(-M[i]) l.t[i].setub(M[i]) def linConMaker(l, i): # i should be range(len(conlist) - 1) # because last element of conlist is the objective con_i = model.find_component(conlist[i]) isEquality = con_i.equality isROM = False if conlist[i][:7] == '.' + self.TRF.name + '.rom': isROM = True romIndex = int(filter(str.isdigit, conlist[i])) # This is very inefficient # Fix this later if these problems get big # This is the ith row of J times v Jv = sum(x[2] * l.v[varlist[x[1]]] for x in J if x[0] == i) if isEquality: if worstcase and isROM: return Jv + l.t[romIndex] == 0 else: return Jv == 0 else: lo = con_i.lower up = con_i.upper if lo is not None: level = lo.value - con_i.lslack() if up is not None: return (lo.value <= level + Jv <= up.value) else: return (lo.value <= level + Jv) elif up is not None: level = up.value - con_i.uslack() return (level + Jv <= up.value) else: raise Exception( "This constraint seems to be neither equality or inequality: " + conlist(i)) l.lincons = Constraint(range(len(conlist) - 1), rule=linConMaker) l.obj = Objective(expr=sum(x[1] * l.v[varlist[x[0]]] for x in g)) # Calculate gradient norm for scaling purposes gfnorm = sqrt(sum(x[1]**2 for x in g)) opt = SolverFactory(self.config.solver) opt.options.update(self.config.solver_options) #opt.options['halt_on_ampl_error'] = 'yes' #opt.options['max_iter'] = 5000 results = opt.solve(l, keepfiles=self.keepfiles, tee=self.stream_solver) if ((results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal)): l.solutions.load_from(results) if gfnorm > 1: return True, abs(l.obj()) / gfnorm else: return True, abs(l.obj()) else: print("Waring: Crticality check fails with solver Status: " + str(results.solver.status)) print("And Termination Conditions: " + str(results.solver.termination_condition)) return False, infinity
def total_profit_rule(model): return (model.FirstStageProfit + model.SecondStageProfit) model.Total_Profit_Objective = Objective(rule=total_profit_rule, sense=maximize) # Solve EV for given number of sample realizations with fixed X at X_EV numSamples=20 numX=5 optVal=numpy.array([0 for i in range(numSamples)]) for i in range(numSamples): datafile = '../scenariodata/Scenario' + str(i+1) + '.dat' instance = model.create(datafile) opt = SolverFactory('gurobi') results = opt.solve(instance) optVal[i] = results.solution.objective.f.value # Calculate point est / interval est of objective value z_val = 1.96 EEV = optVal[:].mean() EEV_var = optVal[:].var()*numSamples/(numSamples-1) EEV_halfwidth = z_val*sqrt(EEV_var/numSamples) EEV_CI_lo = EEV - EEV_halfwidth EEV_CI_hi = EEV + EEV_halfwidth print EEV print EEV_CI_lo, EEV_CI_hi
# cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; # Model transport /all/ ; # Solve transport using lp minimizing z ; def objective_rule(model): return sum(model.c[i, j] * model.x[i, j] for i in model.i for j in model.j) model.objective = Objective(rule=objective_rule, sense=minimize, doc='Define objective function') ## Display of the output ## # Display x.l, x.m ; def pyomo_postprocess(options=None, instance=None, results=None): model.x.display() # This is an optional code path that allows the script to be run outside of # pyomo command-line. For example: python transport.py if __name__ == '__main__': # This emulates what the pyomo command-line tools does from pyomo.opt import SolverFactory import pyomo.environ opt = SolverFactory("glpk") results = opt.solve(model) #sends results to stdout results.write() print("\nDisplaying Solution\n" + '-' * 60) pyomo_postprocess(None, instance, results)
""" Created on Tue Jun 20 22:14:07 2017 @author: YSu """ from pyomo.opt import SolverFactory from Dispatch_1_8 import model from pyomo.core import Var from pyomo.core import Param from operator import itemgetter import pandas as pd instance = model.create('data.dat') opt = SolverFactory("cplex") H = instance.HorizonHours K=range(1,H+1) #Space to store results mwh_1=[] mwh_2=[] mwh_3=[] on=[] switch=[] srsv=[] nrsv=[] hydro=[] solar=[] wind=[]
def schedule_steel(selected_tasks, process_time, code_device, transport_time, Dynamic_named): gu_zd, FINERY_PATH_ID_list, ALL_PATH_ID_list, CC_ID_list, CC_IDENTIFY, all_reality_FINERY_PATH_ID_NUM, blank = Dynamic_list( Dynamic_named) list_jiaoci_start, list_luci, list_jiaoci_end, list_remove_end, list_pono_cast, list_CC_ID, list_CC_START_TIME, selected_tasks = task_pack( selected_tasks, CC_ID_list, CC_IDENTIFY) task_inspect(selected_tasks, process_time, code_device, transport_time, ALL_PATH_ID_list) task_test = selected_tasks #data/文件的写入 list_sb, cast_time, list_temp_time = data( task_test, process_time, code_device, transport_time, list_jiaoci_start, list_luci, list_jiaoci_end, list_remove_end, list_pono_cast, list_CC_ID, list_CC_START_TIME, FINERY_PATH_ID_list, ALL_PATH_ID_list, gu_zd, CC_ID_list, all_reality_FINERY_PATH_ID_NUM, blank) jiaoci_beigin_time = predict_jiaoci_beigin_time(cast_time, list_CC_START_TIME, list_jiaoci_start, list_luci) #print("list_CC_START_TIME",list_CC_START_TIME) #模型的计算 model = model_main(list_sb, list_CC_ID, list_pono_cast, gu_zd, ALL_PATH_ID_list, blank) #调度模型 #模型的求解 solver = SolverFactory("gurobi") instance = model.create_instance('foo1.dat') results = solver.solve(instance, tee=True, keepfiles=False) results.write() #结果输出 list_hang = [] list_lie = [] for i in instance.I: list_hang.append(i) #濮e繋绔寸悰灞炬付閸氬酣鍋呮稉顏勫帗缁辩姵妲搁悙澶嬵偧 for j in instance.J: if instance.M[i, j] != blank: for k in instance.K: if instance.x[i, j, k].value == 1: list_hang.append(k) list_hang.sort() list_lie.append(list_hang.copy()) list_hang.clear() list_hang2 = [] list_lie2 = [] #瀵版鍩岄崥鍕嚋瀹搞儱绨惃鍕磻婵濮炲銉︽闂? for i in instance.I: for j in instance.J: if instance.M[i, j] != blank: for k in instance.K: if instance.x[i, j, k].value == 1: list_hang2.append(i) list_hang2.append(k) list_hang2.append(instance.t[i, j].value) list_lie2.append(list_hang2.copy()) list_hang2.clear() luci_reality = [] for i in range(len(list_lie)): luci_reality.append(list_lie[i][-1]) #print(list_lie2) list_result = result_list_to_result(task_test, process_time, list_lie, list_lie2, cast_time, gu_zd, FINERY_PATH_ID_list, ALL_PATH_ID_list, list_temp_time) # #print(task_test) #return list_result return list_result
def initialize(blk, outlvl=idaeslog.NOTSET, optarg={'tol': 1e-8}, solver='ipopt'): ''' Initialisation routine for reaction package. Keyword Arguments: outlvl : sets output level of initialization routine * 0 = Use default idaes.init logger setting * 1 = Maximum output * 2 = Include solver output * 3 = Return solver state for each step in subroutines * 4 = Return solver state for each step in routine * 5 = Final initialization status and exceptions * 6 = No output optarg : solver options dictionary object (default=None) solver : str indicating whcih solver to use during initialization (default = "ipopt") Returns: None ''' init_log = idaeslog.getInitLogger(blk.name, outlvl, tag="reactions") solve_log = idaeslog.getSolveLogger(blk.name, outlvl, tag="reactions") init_log.info_high('Starting initialization') # TODO - Update in the future as needed # Get a single representative block for getting config arguments for k in blk.keys(): break # Fix state variables if not already fixed # Fix state variables of the primary (solid) state block state_var_flags = fix_state_vars(blk[k].config.solid_state_block) # Fix values of secondary (gas) state block variables if not fixed, # as well as the solid density variable. # This is done to keep the initialization problem square Cflag = {} # Gas concentration flag Dflag = {} # Solid density flag for k in blk.keys(): for j in blk[k]._params.gas_component_list: if blk[k].gas_state_ref.dens_mol_comp[j].fixed is True: Cflag[k, j] = True else: Cflag[k, j] = False blk[k].gas_state_ref.dens_mol_comp[j].fix( blk[k].gas_state_ref.dens_mol_comp[j].value) if blk[k].solid_state_ref.dens_mass_skeletal.fixed is True: Dflag[k] = True else: Dflag[k] = False blk[k].solid_state_ref.dens_mass_skeletal.fix( blk[k].solid_state_ref.dens_mass_skeletal.value) # Set solver options opt = SolverFactory(solver) opt.options = optarg # Initialise values for k in blk.keys(): if hasattr(blk[k], "OC_conv_eqn"): calculate_variable_from_constraint(blk[k].OC_conv, blk[k].OC_conv_eqn) if hasattr(blk[k], "OC_conv_temp_eqn"): calculate_variable_from_constraint(blk[k].OC_conv_temp, blk[k].OC_conv_temp_eqn) for j in blk[k]._params.rate_reaction_idx: if hasattr(blk[k], "rate_constant_eqn"): calculate_variable_from_constraint( blk[k].k_rxn[j], blk[k].rate_constant_eqn[j]) if hasattr(blk[k], "gen_rate_expression"): calculate_variable_from_constraint( blk[k].reaction_rate[j], blk[k].gen_rate_expression[j]) # Solve property block if non-empty free_vars = 0 for k in blk.keys(): free_vars += number_unfixed_variables_in_activated_equalities( blk[k]) if free_vars > 0: with idaeslog.solver_log(solve_log, idaeslog.DEBUG) as slc: res = solve_indexed_blocks(opt, [blk], tee=slc.tee) else: res = "" init_log.info_high("reactions initialization complete {}.".format( idaeslog.condition(res))) # --------------------------------------------------------------------- # Revert state vars and other variables to pre-initialization states # Revert state variables of the primary (solid) state block revert_state_vars(blk[k].config.solid_state_block, state_var_flags) for k in blk.keys(): for j in blk[k]._params.gas_component_list: if Cflag[k, j] is False: blk[k].gas_state_ref.dens_mol_comp[j].unfix() if Dflag[k] is False: blk[k].solid_state_ref.dens_mass_skeletal.unfix() init_log = idaeslog.getInitLogger(blk.name, outlvl, tag="reactions") init_log.info_high('States released.')
# # Execution of this script requires that the ipopt # solver is in the current search path for executables # on this system. This example was tested using Ipopt # version 3.10.2 import pyomo.environ from pyomo.core import * from pyomo.opt import SolverFactory ### Create the ipopt solver plugin using the ASL interface solver = 'ipopt' solver_io = 'nl' stream_solver = False # True prints solver output to screen keepfiles = False # True prints intermediate file names (.nl,.sol,...) opt = SolverFactory(solver, solver_io=solver_io) if opt is None: print("") print("ERROR: Unable to create solver plugin for %s "\ "using the %s interface" % (solver, solver_io)) print("") exit(1) ### ### Set Ipopt options to accept the scaling_factor suffix opt.options['nlp_scaling_method'] = 'user-scaling' ### ### Create the example model model = ConcreteModel()
def initialize(blk, flow_mol_comp=None, temperature=None, pressure=None, hold_state=False, outlvl=0, state_vars_fixed=False, solver='ipopt', optarg={'tol': 1e-8}): ''' Initialisation routine for property package. Keyword Arguments: flow_mol_comp : value at which to initialize component flows (default=None) pressure : value at which to initialize pressure (default=None) temperature : value at which to initialize temperature (default=None) outlvl : sets output level of initialisation routine * 0 = no output (default) * 1 = return solver state for each step in routine * 2 = include solver output infomation (tee=True) state_vars_fixed: Flag to denote if state vars have already been fixed. - True - states have already been fixed by the control volume 1D. Control volume 0D does not fix the state vars, so will be False if this state block is used with 0D blocks. - False - states have not been fixed. The state block will deal with fixing/unfixing. optarg : solver options dictionary object (default=None) solver : str indicating whcih solver to use during initialization (default = 'ipopt') hold_state : flag indicating whether the initialization routine should unfix any state variables fixed during initialization (default=False). - True - states varaibles are not unfixed, and a dict of returned containing flags for which states were fixed during initialization. - False - state variables are unfixed after initialization by calling the relase_state method Returns: If hold_states is True, returns a dict containing flags for which states were fixed during initialization. ''' if state_vars_fixed is False: # Fix state variables if not already fixed Fcflag = {} Pflag = {} Tflag = {} for k in blk.keys(): for j in blk[k]._params.component_list: if blk[k].flow_mol_comp[j].fixed is True: Fcflag[k, j] = True else: Fcflag[k, j] = False if flow_mol_comp is None: blk[k].flow_mol_comp[j].fix(1.0) else: blk[k].flow_mol_comp[j].fix(flow_mol_comp[j]) if blk[k].pressure.fixed is True: Pflag[k] = True else: Pflag[k] = False if pressure is None: blk[k].pressure.fix(101325.0) else: blk[k].pressure.fix(pressure) if blk[k].temperature.fixed is True: Tflag[k] = True else: Tflag[k] = False if temperature is None: blk[k].temperature.fix(1500.0) else: blk[k].temperature.fix(temperature) for j in blk[k]._params.component_list: blk[k].mole_frac[j] = \ (value(blk[k].flow_mol_comp[j]) / sum(value(blk[k].flow_mol_comp[i]) for i in blk[k]._params.component_list)) # If input block, return flags, else release state flags = {"Fcflag": Fcflag, "Pflag": Pflag, "Tflag": Tflag} else: # Check when the state vars are fixed already result in dof 0 for k in blk.keys(): if degrees_of_freedom(blk[k]) != 0: raise Exception("State vars fixed but degrees of freedom " "for state block is not zero during " "initialization.") # Set solver options if outlvl > 1: stee = True else: stee = False opt = SolverFactory(solver) opt.options = optarg # --------------------------------------------------------------------- # Initialise values for k in blk.keys(): for j in blk[k]._params.component_list: if hasattr(blk[k], "cp_shomate_eqn"): calculate_variable_from_constraint( blk[k].cp_mol_comp[j], blk[k].cp_shomate_eqn[j]) if hasattr(blk[k], "enthalpy_shomate_eqn"): calculate_variable_from_constraint( blk[k].enth_mol_phase_comp["Vap", j], blk[k].enthalpy_shomate_eqn[j]) if hasattr(blk[k], "entropy_shomate_eqn"): calculate_variable_from_constraint( blk[k].entr_mol_phase_comp["Vap", j], blk[k].entropy_shomate_eqn[j]) if hasattr(blk[k], "partial_gibbs_energy_eqn"): calculate_variable_from_constraint( blk[k].gibbs_mol_phase_comp["Vap", j], blk[k].partial_gibbs_energy_eqn[j]) if hasattr(blk[k], "ideal_gas"): calculate_variable_from_constraint( blk[k].dens_mol_phase["Vap"], blk[k].ideal_gas) if hasattr(blk[k], "mixture_heat_capacity_eqn"): calculate_variable_from_constraint( blk[k].cp_mol, blk[k].mixture_heat_capacity_eqn) if hasattr(blk[k], "mixture_enthalpy_eqn"): calculate_variable_from_constraint(blk[k].enth_mol, blk[k].mixture_enthalpy_eqn) if hasattr(blk[k], "mixture_entropy_eqn"): calculate_variable_from_constraint(blk[k].entr_mol, blk[k].mixture_entropy_eqn) if hasattr(blk[k], "total_flow_eqn"): calculate_variable_from_constraint(blk[k].flow_mol, blk[k].total_flow_eqn) if hasattr(blk[k], "mixture_gibbs_eqn"): calculate_variable_from_constraint(blk[k].gibbs_mol, blk[k].mixture_gibbs_eqn) results = solve_indexed_blocks(opt, blk, tee=stee) if outlvl > 0: if results.solver.termination_condition \ == TerminationCondition.optimal: _log.info('{} Initialisation Step 1 Complete.'.format( blk.name)) else: _log.warning('{} Initialisation Step 1 Failed.'.format( blk.name)) # --------------------------------------------------------------------- if outlvl > 0: if outlvl > 0: _log.info('{} Initialisation Complete.'.format(blk.name)) if state_vars_fixed is False: if hold_state is True: return flags else: blk.release_state(flags)
step = 20 #Si se cambia el step, la grafica del frente Optimo pude obtener mas o menos puntos, ademas de cambiar su forma un poco try: for lim in reversed(range(0, epsilon + 1, step)): print('Epsilon: ', lim) modelo.x = Var(I, J, K, domain=PositiveIntegers) modelo.f1 = Objective(expr=sum(costo[j, k] * modelo.x[i, j, k] for i in I for j in J for k in K), sense=minimize) modelo.f2 = Constraint(expr=sum(delay[j, k] * modelo.x[i, j, k] for i in I for j in J for k in K) <= lim) modelo.inv = Constraint(I, J, rule=inv_constraint) modelo.dem = Constraint(I, K, rule=dem_constraint) SolverFactory('glpk').solve(modelo) modelo.display() f1 = f1 + [value(modelo.f1)] f2 = f2 + [value(modelo.f2)] delete_component(modelo, 'x') delete_component(modelo, 'f1') delete_component(modelo, 'f2') delete_component(modelo, 'inv') delete_component(modelo, 'dem') except Exception: pass #Si entra aca es por que la solución no se puede determinar y ya se acabo la iteración finally: plt.plot(f1, f2, 'o-.') plt.title('Frente Óptimo de Pareto') plt.xlabel('F1') plt.ylabel('F2')
'''Objective function: Formulate problem as binary problem. \sum_{i=1}^{n} \sum_{j=1}^{m} w_{i}*c_{ij}*x_{ij}*n''' if c is None: c = model.c if w is None: w = model.w if nvar is None: nvar = model.nvar return np.sum((np.sum( (np.multiply(model.x[i, j] * nvar, c[i, j] * w[i]) for i in model.I), axis=0) for j in model.J), axis=0) if __name__ == '__main__': import pandas as pd data_dir = os.path.join(os.getcwd(), '../boreal_data') carbon = pd.read_csv(os.path.join(data_dir, 'Carbon_storage.csv')) # Removes all lines containing Nan-values carbon_clean = carbon.dropna(axis=0, how='any') # Let's take a bit smaller sample data = carbon_clean[:1000].values # Problem without any weight so use just ones as weights weights = np.ones(len(data)) problem = BorealWeightedProblem(data, weights) opt = SolverFactory('glpk') res = opt.solve(problem.model, True) # problem.model.x.display()
from pyomo.environ import value from pyomo.opt import SolverFactory from concrete1 import model as instance1 from concrete2 import model as instance2 with SolverFactory("glpk") as opt: results1 = opt.solve(instance1) results2 = opt.solve(instance2) print("x_2 value: %s" % (instance1.x_2.value)) print("x_2 value: %s" % (instance1.x_2())) print("x_2 value: %s" % (value(instance1.x_2))) print("x[2] value: %s" % (instance2.x[2].value)) print("x[2] value: %s" % (instance2.x[2]())) print("x[2] value: %s" % (value(instance2.x[2]))) print("x_2 object: %s" % (instance1.x_2)) print("x[2] object: %s" % (instance2.x[2])) if value(instance1.x_2) == value(instance2.x[2]): print("x_2 == x[2]") else: print("x_2 != x[2]") if instance1.x_2 == instance2.x[2]: print("x_2 == x[2]") else: print("x_2 != x[2]")
#def _momentRule(m, t): #return m.T[t] == sum(m.footRelativeToCOM[foot,'x',t]*m.f[foot,'z',t] - m.footRelativeToCOM[foot,'z',t]*m.f[foot, 'x',t] for foot in m.feet) #m_nlp.momentAbountCOM = Constraint(m_nlp.t, rule=_momentRule) def _hipTorqueRule(m, foot, t): return m.hipTorque[foot, t] == m.p[foot,'x',t]*m.f[foot,'z',t] - m.p[foot,'z',t]*m.f[foot, 'x',t] #m_nlp.hipTorqueConstraint = Constraint(m_nlp.feet, m_nlp.t, rule=_hipTorqueRule) m_nlp.pwSin.deactivate() m_nlp.pwCos.deactivate() def _cos(m, t): return m.cth[t] == cos(m.th[t]) m_nlp.Cos = Constraint(m_nlp.t, rule=_cos) def _sin(m, t): return m.sth[t] == sin(m.th[t]) m_nlp.Sin = Constraint(m_nlp.t, rule=_sin) opt_nlp = SolverFactory('ipopt') opt_minlp = constructCouenneSolver() #opt = constructGurobiSolver(mipgap=0.8, MIPFocus=1, TimeLimit=90., Threads=11) opt = constructGurobiSolver(TimeLimit=480., Threads=11) #opt = constructGurobiSolver(TimeLimit=50., Threads=11) hop.constructVisualizer()
def solve(self): opt = SolverFactory('ipopt') opt.solve(self.m).write() self.problem_solved = True
def main(): m = create_model() optTRF = SolverFactory('trustregion', maximum_iterations=100, verbose=True) optTRF.solve(m, [m.x1], ext_fcn_surrogate_map_rule=basis_rule)
#VIERNES Model.cost[5, 6] = 0 Model.cost[5, 7] = 0 #SABADO Model.cost[6, 7] = 0 Model.cost[6, 1] = 0 #DOMINGO Model.cost[7, 1] = 0 Model.cost[7, 2] = 0 Model.x = Var(Model.N, domain=PositiveIntegers) Model.obj = Objective(expr=sum(Model.x[N] for N in Model.N), sense=minimize) def restriccionDia(Model, c): return sum(Model.x[c] * Model.cost[t, c] for t in Model.N) def restriccionGeneral(Model): i = 0 for c in Model.N: if value(restriccionDia(Model, c)) > dias[c - 1]: i = i + 1 return (i == 7) Model.res1 = Constraint(rule=restriccionGeneral) SolverFactory("glpk").solve(Model) Model.display()
# ___________________________________________________________________________ # # Pyomo: Python Optimization Modeling Objects # Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC # Under the terms of Contract DE-NA0003525 with National Technology and # Engineering Solutions of Sandia, LLC, the U.S. Government retains certain # rights in this software. # This software is distributed under the 3-clause BSD License. # ___________________________________________________________________________ import pyomo.common.unittest as unittest import pyomo.contrib.parmest.parmest as parmest from pyomo.opt import SolverFactory ipopt_available = SolverFactory('ipopt').available() @unittest.skipIf(not parmest.parmest_available, "Cannot test parmest: required dependencies are missing") @unittest.skipIf(not ipopt_available, "The 'ipopt' solver is not available") class TestRooneyBieglerExamples(unittest.TestCase): @classmethod def setUpClass(self): pass @classmethod def tearDownClass(self): pass def test_model(self): from pyomo.contrib.parmest.examples.rooney_biegler import rooney_biegler rooney_biegler.main()
def _perform_queue(self, ah, *args, **kwds): """ Perform the queue operation. This method returns the ActionHandle, and the ActionHandle status indicates whether the queue was successful. """ solver = kwds.pop('solver', kwds.pop('opt', None)) if solver is None: raise ActionManagerError( "No solver passed to %s, use keyword option 'solver'" % (type(self).__name__) ) if not isinstance(solver, str): solver_name = solver.name if solver_name == 'asl': solver_name = \ os.path.basename(solver.executable()) else: solver_name = solver solver = None # # Handle ephemeral solvers options here. These # will override whatever is currently in the options # dictionary, but we will reset these options to # their original value at the end of this method. # user_solver_options = {} # make sure to transfer the options dict on the # solver plugin if the user does not use a string # to identify the neos solver. The ephemeral # options must also go after these. if solver is not None: user_solver_options.update(solver.options) _options = kwds.pop('options', {}) if isinstance(_options, str): _options = OptSolver._options_string_to_dict(_options) user_solver_options.update(_options) user_solver_options.update( OptSolver._options_string_to_dict(kwds.pop('options_string', ''))) # JDS: [5/13/17] The following is a HACK. This timeout flag is # set by pyomo/scripting/util.py:apply_optimizer. If we do not # remove it, it will get passed to the NEOS solver. For solvers # like CPLEX 12.7.0, this will cause a fatal error as it is not # a known option. if user_solver_options.get('timelimit',0) is None: del user_solver_options['timelimit'] opt = SolverFactory('_neos') opt._presolve(*args, **kwds) # # Map NEOS name, using lowercase convention in Pyomo # if len(self._solvers) == 0: for name in self.kestrel.solvers(): if name.endswith('AMPL'): self._solvers[ name[:-5].lower() ] = name[:-5] if solver_name not in self._solvers: raise ActionManagerError( "Solver '%s' is not recognized by NEOS. " "Solver names recognized:\n%s" % (solver_name, str(sorted(self._solvers.keys())))) # # Apply kestrel # # Set the kestrel_options environment # neos_sname = self._solvers[solver_name].lower() os.environ['kestrel_options'] = 'solver=%s' % self._solvers[solver_name] # # Set the <solver>_options environment # solver_options = {} for key in opt.options: solver_options[key]=opt.options[key] solver_options.update(user_solver_options) options = opt._get_options_string(solver_options) if not options == "": os.environ[neos_sname+'_options'] = options # # Generate an XML string using these two environment variables # xml = self.kestrel.formXML(opt._problem_files[0]) (jobNumber, password) = self.kestrel.submit(xml) ah.job = jobNumber ah.password = password # # Cleanup # del os.environ['kestrel_options'] try: del os.environ[neos_sname+"_options"] except: pass # # Store action handle, and return # self._ah[jobNumber] = ah self._neos_log[jobNumber] = (0, "") self._opt_data[jobNumber] = (opt, opt._smap_id, opt._load_solutions, opt._select_index, opt._default_variable_value) self._args[jobNumber] = args return ah
from pyomo.opt import SolverFactory from concrete1 import model as instance # @cmd: solver = SolverFactory('asl:coliny', solver='sco:ps') # @:cmd solver.solve(instance)
def main(): ### Location and date information current_directory = os.path.dirname(os.path.realpath(__file__)) current_date = time.strftime('%m_%d_%Y', time.gmtime()) ### Set Results File Output Directory output_directory = current_directory + '/Solutions/' + 'SAA_DE' + '_' + current_date + '/' ### Loop Info # files = ['modeldata.dat','modeldatab.dat','modeldata3.dat'] # files = ['modeldata_4.dat','modeldata_10.dat','modeldata3.dat','modeldata3_5.dat','modeldata4_3.dat','modeldata4.dat','modeldata4_5.dat','modeldata5.dat','modeldata6.dat'] # files = ['modeldata4_3.dat','modeldata4.dat','modeldata4_5.dat','modeldata5.dat','modeldata6.dat'] # files = ['modeldata6.dat'] files = ['modeldata_4.dat'] # Specify sample size # Number_of_Scenarios = {'modeldatab.dat': [.3, .5, .7, 1.0], 'modeldata.dat': [.3, .5, .7, 1.0], # 'modeldata3.dat': [0.05, 0.10, 0.15, 0.20, 0.25, .3, .4, .5, 0.6, 0.8, 1.0]} # Number_of_Scenarios = {'modeldata6.dat':[.125, 0.25]} # Number_of_Scenarios = {'modeldata4.dat':[0.5]} # 'modeldata_4.dat':[.75] Number_of_Scenarios = { 'modeldata_4.dat': [.75], 'modeldata_10.dat': [.24], 'modeldata3.dat': [.09375], 'modeldata3_5.dat': [.192], 'modeldata4_3.dat': [.148148148], 'modeldata4.dat': [.5], 'modeldata4_5.dat': [.0384], 'modeldata5.dat': [.0625, 0.25, 0.5, 0.75, 1], 'modeldata6.dat': [.125, 0.5, 0.75] } # Solve Count -- The specified number of time we solve the model. # scount = {'modeldata4.dat': 1} scount = { 'modeldata.dat': 1, 'modeldata_4.dat': 1, 'modeldata_10.dat': 1, 'modeldata3.dat': 1, 'modeldata3_5.dat': 1, 'modeldata4_3.dat': 1, 'modeldata4.dat': 1, 'modeldata4_5.dat': 1, 'modeldata5.dat': 1, 'modeldata6.dat': 1 } import_record_sceanrios = 'record_subset_scenarios_10_29_2020.json' with open(import_record_sceanrios) as f: Subset_Scenarios_record = dict( [tuple((tuple(x[0]), x[1])) for x in json.loads(f.read())]) for fl in files: print("Starting File " + str(fl)) ##### Import the data into the system model_data = get_datafile(fl) ### Define number of products and number of trials NP = len(model_data._data['product'][None]) NT = len(model_data._data['trial'][None]) Product = model_data._data['product'][None] Trials = model_data._data['trial'][None] Probability = model_data._data['probability'] ### Generate all possible outcomes Outcomes = itertools.product(range(NT + 1), repeat=NP) Outcomes = list(Outcomes) ### Generate Full Scenario Set scenario = 1 Scenario_Objects = {} Scenario_Outcomes = {} Scenario_List = [] for s in Outcomes: Scenario_Objects[scenario] = scenario_class.scenario( s, Probability, Product, Trials) Scenario_Outcomes[scenario] = s Scenario_List.append(scenario) scenario += 1 Full_Sample_Size = len(Outcomes) Full_Sample_Size = math.ceil(Full_Sample_Size) n = 0 nmax = 1 sub_model_Max = 0 sub_model_AVG = 0 sub_model_Max_alter = 0 sub_model_AVG_alter = 0 full_model_Max = 0 full_model_AVG = 0 reducedfull_model_Max = 0 reducedfull_model_AVG = 0 NAC_AVG = 0 NAC_MAX = 0 NAC_AVG_alter = 0 NAC_MAX_alter = 0 while n < nmax: sub_model_NAC_count = 0 sub_model_NAC_count_alter = 0 full_model_NAC_count = 0 reducedfull_model_NAC_count = 0 for ns in Number_of_Scenarios[fl]: ### Determine Sample Size Sample_Size = ns * len(Outcomes) Sample_Size = math.ceil(Sample_Size) ### Select Scenarios Subset_Scenarios = Subset_Scenarios_record[fl, n, ns] print(len(Subset_Scenarios)) ### Normalize Probability total_probability = 0 for s in Subset_Scenarios: total_probability += Scenario_Objects[s].probability S_Probability = {} for s in Subset_Scenarios: S_Probability[s] = Scenario_Objects[ s].probability / total_probability Pb = {} for s in Scenario_List: Pb[s] = Scenario_Objects[s].probability ### Generate NACs For Subset # print('Generating NACs for Scenarios' + str(Subset_Scenarios)) Uncertain_Parameters = [] for i in Product: Param = UP(str(i), 'endogenous', 'gradual', range(NT + 1), [], range(NT + 1)) Uncertain_Parameters.append(Param) Uncertain_Parameters = tuple(Uncertain_Parameters) ######################################################################################################## ################################### NAC GENERATION 1 ################################################### Ptime = time.process_time() Snac_StartTime = time.time() snacIter = 0 while snacIter < scount[fl]: NAC_Set = NAC_original(Uncertain_Parameters, Subset_Scenarios, Scenario_Outcomes) NAC_Set = list(NAC_Set) phI_IJ = {} for (s, sp) in NAC_Set: i = 0 phI_IJ[(s, sp)] = [] while i < len(Product): phList = [ Scenario_Outcomes[s][i], Scenario_Outcomes[sp][i] ] if phList[0] != phList[1]: phList.sort() Differentiation = (Product[i], phList[0] + 1) phI_IJ[(s, sp)].append(Differentiation) i += 1 snacIter += 1 Snac_Total = time.time() - Snac_StartTime Snac_process_time = time.process_time() - Ptime ## record NAC pairs NAC_AVG += len(NAC_Set) / 1 if len(NAC_Set) > NAC_MAX: NAC_MAX = len(NAC_Set) print('NAC_AVG', NAC_AVG, 'NAC_MAX', NAC_MAX) list_phijj = [x for x in phI_IJ] print('list_phijj', len(list_phijj)) ######################################################################################################## ################################### NAC GENERATION alternative ################################################### ######################################################################################################## ################################### NAC GENERATION 2 ################################################### ######################################################################################################## ################################### NAC GENERATION 3 ################################################### ### Generate NACs for Full Set #################################################################### ### Generate Model #################################################################### Success = {} for s in Scenario_List: for i in Product: Success[( i, s)] = Scenario_Objects[s].success[Product.index(i)] GammaD = {} GammaL = {} revenue_max = {} resource_max = {} for items in model_data._data['max_resource']: resource_max[ items[0]] = model_data._data['max_resource'][items] ## Set Discount Values for items in model_data._data['gammaL']: GammaL[items[0]] = model_data._data['gammaL'][items] for items in model_data._data['gammaD']: GammaD[items[0]] = model_data._data['gammaD'][items] ## Set Maximum Revenue for items in model_data._data['maximum_revenue']: revenue_max[ items[0]] = model_data._data['maximum_revenue'][items] ## Calculate running rev rev_run = M2S_item.calc_rr(revenue_max, GammaL, model_data._data['trial_duration'], Product, Trials, model_data._data['time_step'][None]) ##Calculate open rev rev_open = M2S_item.calc_openrev( revenue_max, GammaL, model_data._data['trial_duration'], Product, Trials, model_data._data['time_step'][None], model_data._data['time_step'][None][-1]) ##Calculate Discounting Factor discounting_factor = M2S_item.calc_discounting_factor( revenue_max, GammaL, model_data._data['trial_cost'], Product, Trials, model_data._data['time_step'][None][-1]) ######################################################################################################## ################################## Create Model 1 ###################################################### opt = SolverFactory("cplex") Ptime = time.process_time() Model1_StartTime = time.time() m1 = 0 while m1 < scount[fl]: sub_model = SAA(Product, Trials, model_data._data['time_step'][None], model_data._data['resource_type'][None], Subset_Scenarios, \ resource_max, GammaL, GammaD, model_data._data['trial_duration'], model_data._data['trial_cost'], \ model_data._data['resource_requirement'], revenue_max, S_Probability, \ Success, len(model_data._data['time_step'][None]), Trials[-1], rev_run, rev_open, discounting_factor, \ phI_IJ, Scenario_Outcomes) m1 += 1 Model1TotalTime = time.time() - Model1_StartTime model_process_time = time.process_time() - Ptime ##### Solve Sub Model Model Ptime = time.process_time() Model1Solve_StartTime = time.time() s1 = 0 while s1 < scount[fl]: sub_results = opt.solve(sub_model) s1 += 1 Model1Solve_TotalTime = time.time() - Model1Solve_StartTime model_solve_process_time = time.process_time() - Ptime sub_model.solutions.load_from(sub_results) ### generate number of NAC constriants sub_model_NAC_count = len(sub_model.NAC_Constraint) + len( sub_model.NAC2_Constraint) + len(sub_model.NAC3_Constraint) print('sub_model_NAC_count', sub_model_NAC_count) sub_model_AVG += sub_model_NAC_count / 1 if sub_model_NAC_count > sub_model_Max: sub_model_Max = sub_model_NAC_count print('sub_model_Max', sub_model_Max, 'sub_model_AVG', sub_model_AVG) ### generate number of NAC constriants ######################################################################################################## ################################## Create Model 1 alternative ###################################################### ######################################################################################################## ################################## Create Model 2 ###################################################### ######################################################################################################## ######################################################################################################## ### Generate New File Name save_file = 'SNAC' + str(fl) + "_" + str(ns) + "_" + str( nmax) + "_iterations" ### Open save file if not os.path.exists(output_directory): os.makedirs(output_directory) if n == 0: f = open(os.path.join(output_directory, save_file), "w") Header = "%-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s" % ( 'SAA_ENPV', 'SNAC_Time', 'SNAC_Model_Time', 'SNAC_Time(P)', 'SNAC_Model_Time(P)', 'SNAC_sol_Time', 'SNAC_sol_Time(P)', 'sub_model_AVG', 'sub_model_Max', 'NAC_Count_AVG', 'NAC_Count_MAX') f.write(Header) f.write('\n') else: f = open(os.path.join(output_directory, save_file), "a") ### Generate file contents RESULTS = "%-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s %-30s" % ( str(round(sub_model.Expected_NPV(), 4)), str(round(Snac_Total, 4)), str(round( Model1TotalTime, 4)), str(round(Snac_process_time, 4)), str(round(model_process_time, 4)), str(round(Model1Solve_TotalTime, 4)), str(model_solve_process_time), str(round( sub_model_AVG, 4)), str(round( sub_model_Max, 4)), str(NAC_AVG), str(NAC_MAX)) f.write(RESULTS) f.write('\n') n += 1 f.write('\n') f.write('----------' + import_record_sceanrios + '---------------') f.write('\n') f.close()
def main(): TAs_df = pd.read_csv("data/TA_apps.csv").set_index("name") str_to_list = lambda s: list(map(int, s.split(","))) TAs_df["can_teach"] = TAs_df["can_teach"].apply(str_to_list) TAs_df["enthusiastic"] = TAs_df["enthusiastic"].apply(str_to_list) TAs = TAs_df.index.values.tolist() courses_df = pd.read_csv("data/MDS_courses_term_1.csv").set_index( "course_number") courses = courses_df.index.values.tolist() blocks = set(courses_df["block"]) model = ConcreteModel() ## Define sets ## # Sets # i TAs / Alice, Bob / # j course_number / 511, 521 / ; model.i = Set(initialize=TAs, doc='TAs') model.j = Set(initialize=courses, doc='course_number') # Variable of 2 dimensions model.x = Var(model.i, model.j, doc='TA Assignment', within=Binary) # Setting up constraints TAS_PER_COURSE = 2 model.limits = ConstraintList() for course in courses: model.limits.add(expr=sum(model.x[TA, course] for TA in TAs) == TAS_PER_COURSE) for TA in TAs: for block in blocks: model.limits.add( expr=sum(model.x[TA, course] for course in courses if courses_df.loc[course]["block"] == block) <= 1) for TA in TAs: for course in courses: if course not in TAs_df.loc[TA][ "can_teach"] and course not in TAs_df.loc[TA][ "enthusiastic"]: model.limits.add(model.x[TA, course] == 0) for TA in TAs: for course in courses: if courses_df.loc[course]["lab_days"][0] not in TAs_df.loc[TA][ "availability"] and courses_df.loc[course]["lab_days"][ 1] not in TAs_df.loc[TA]["availability"]: model.limits.add(model.x[TA, course] == 0) # Setting up Objective model.objective = Objective(expr=sum( model.x[TA, course] for TA in TAs for course in courses if course in TAs_df.loc[TA]["enthusiastic"]), sense=maximize) # Calling Solver opt = SolverFactory("glpk") results = opt.solve(model) results.write() print("\nDisplaying Solution\n" + '-' * 60) pyomo_postprocess(None, model, results) print("We have %d enthusiastic courses out of a possible %d." % (results.Problem()['Lower bound'], len(courses) * TAS_PER_COURSE)) out_df_by_TA = pd.DataFrame("", index=TAs, columns=blocks) for TA in TAs: for course in courses: if model.x[TA, course].value == 1: out_df_by_TA.at[TA, courses_df.loc[course]["block"]] = course out_df_by_TA.to_csv("output.csv") print(out_df_by_TA) for course in courses: print(course, end=": ") for TA in TAs: if model.x[TA, course].value == 1: print(TA, end=', ') print("") # Enthusiastic courses for course in courses: for TA in TAs: if model.x[TA, course].value == 1 and course in TAs_df.loc[TA][ "enthusiastic"]: print("%-7s is enthusiastic about DSCI %d!" % (TA, course))
##################### Define Objective Function ############################## def net_revenue_rule(mdl): return ( # Fixed capacity installtion costs sum(tm.Variable_Install_Cost[f] for f in tm.FEEDERS) + # O&M costs (variable & fixed) sum((tm.om_cost_fix + tm.capacity_factor * tm.om_cost_var) * tm.CapInstalled[f] for f in tm.FEEDERS) + # Transportation costs sum(tm.distances[r] * tm.BiomassTransported[r] * tm.transport_cost # has some glitches for r in tm.ROUTES) + # Biomass acquisition costs. sum(tm.biomass_cost[b] * sum(tm.BiomassTransported[b, f] for f in tm.FEEDERS) for b in tm.LANDINGS) - # Gross profits during the period sum(tm.fit_tariff[f] * tm.CapInstalled[f] * tm.capacity_factor * tm.total_hours * tm.unit_capacity for f in tm.FEEDERS)) # minimize the above value of net cost tm.net_profits = pm.Objective(rule=net_revenue_rule, sense=pm.minimize, doc='Define objective function') ############################ Solve Model #################################### opt = SolverFactory("gurobi") results = opt.solve(tm, tee=True)
def __init__(self, *args, **kwds): if self.__class__ is _ScenarioTreeManagerSolverWorker: raise NotImplementedError( "%s is an abstract class for subclassing" % self.__class__) super(_ScenarioTreeManagerSolverWorker, self).__init__(*args, **kwds) # TODO: Does this import need to be delayed because # it is in a plugins subdirectory? from pyomo.solvers.plugins.solvers.persistent_solver import \ PersistentSolver assert self.manager is not None # solver related objects self._scenario_solvers = {} self._bundle_solvers = {} self._preprocessor = None self._solver_manager = None # there are situations in which it is valuable to snapshot / # store the solutions associated with the scenario # instances. for example, when one wants to use a warm-start # from a particular iteration solve, following a modification # and re-solve of the problem instances in a user-defined # callback. the following nested dictionary is intended to # serve that purpose. The nesting is dependent on whether # bundling and or phpyro is in use self._cached_solutions = {} self._cached_scenariotree_solutions = {} # # initialize the preprocessor # self._preprocessor = None if not self.get_option("disable_advanced_preprocessing"): self._preprocessor = ScenarioTreePreprocessor(self._options, options_prefix=self._options_prefix) assert self._manager.preprocessor is None self._manager.preprocessor = self._preprocessor # # initialize the solver manager # self._solver_manager = SolverManagerFactory( self.get_option("solver_manager"), host=self.get_option('solver_manager_pyro_host'), port=self.get_option('solver_manager_pyro_port')) for scenario in self.manager.scenario_tree._scenarios: assert scenario._instance is not None solver = self._scenario_solvers[scenario.name] = \ SolverFactory(self.get_option("solver"), solver_io=self.get_option("solver_io")) if isinstance(solver, PersistentSolver) and \ self.get_option("disable_advanced_preprocessing"): raise ValueError("Advanced preprocessing can not be disabled " "when persistent solvers are used") if self._preprocessor is not None: self._preprocessor.add_scenario(scenario, scenario._instance, solver) for bundle in self.manager.scenario_tree._scenario_bundles: solver = self._bundle_solvers[bundle.name] = \ SolverFactory(self.get_option("solver"), solver_io=self.get_option("solver_io")) if isinstance(solver, PersistentSolver) and \ self.get_option("disable_advanced_preprocessing"): raise ValueError("Advanced preprocessing can not be disabled " "when persistent solvers are used") bundle_instance = \ self.manager._bundle_binding_instance_map[bundle.name] if self._preprocessor is not None: self._preprocessor.add_bundle(bundle, bundle_instance, solver)
def run_one_year(): T = 60 alldays = pd.date_range('1/1/2011', periods=T, freq='D') dir_work = 'one_year_run' dir_home = os.getcwd() print 'Model run started at {}, T = {}'.format( datetime.datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H:%M:%S'), T, ) sys.stdout.flush() t0 = time() if not os.path.isdir(dir_work): os.mkdir(dir_work) os.chdir(dir_work) # Go to work! df_power, df_load = read_jubeyer() instance = create_model() opt = SolverFactory('cplex') gen_renewable = [g for g in df_power.columns if g.startswith('wind') or g.startswith('solar')] load_bus = [b for b in df_load if b.startswith('bus')] print '{:^8s} {:^11s} {:>15s} {:>15s} {:^22s} {:^15s}'.format( '# of day', 'Date', 'T iter (s)', 'T total (h)', 'Finished at', 'Objective value', ) sys.stdout.flush() for iday in range(0, len(alldays)): t1 = time() thisday = alldays[iday] y = thisday.year m = thisday.month d = thisday.day dir_results = str(thisday.to_pydatetime().date()) try: fcsv = os.path.sep.join( [dir_results, 'UnitOn.csv'] ) df = pd.read_csv(fcsv) solved = True # This case has already been solved. except IOError: solved = False # This case has not been solved yet. if solved: print '{:>8s} {:>11s} {:>15.2f} {:>15.2f} {:>22s} {:>15s}'.format( str(iday+1)+'/'+str(T), str(thisday.to_pydatetime().date()), time() - t1, (time() - t0)/3600, datetime.datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H:%M:%S'), 'N/A' ) continue iselected = (df_power['Year']==y) & (df_power['Month']==m) & (df_power['Day']==d) df_power_tmp = df_power.loc[iselected, :].reset_index() dict_power = dict() for i, row in df_power_tmp.iterrows(): for g in gen_renewable: dict_power[g, i+1] = df_power_tmp.loc[i, g] iselected = (df_load['Year']==y) & (df_load['Month']==m) & (df_load['Day']==d) df_busdemand_tmp = df_load.loc[iselected, :].reset_index() dict_busdemand = dict() for i, row in df_busdemand_tmp.iterrows(): for b in load_bus: dict_busdemand[b, i+1] = df_busdemand_tmp.loc[i, b] df_load_tmp = df_load.loc[iselected, 'LOAD'].reset_index() dict_demand = dict() for i, row in df_load_tmp.iterrows(): dict_demand[i+1] = df_load_tmp.loc[i, 'LOAD'] # Step 1: update power forecast and load, and parameters that are # dependent on them instance.PowerForecast.store_values(dict_power) instance.BusDemand.store_values(dict_busdemand) instance.Demand.store_values(dict_demand) instance.SpinningReserveRequirement.reconstruct() instance.RegulatingReserveRequirement.reconstruct() # Step 2: Update initial minimum online/offline hours of thermal gens # and parameters that are dependent on them, if not the first day (i=0) if iday: # dict_UnitOnT0State = return_unitont0state(instance) previousday = alldays[iday-1] fcsv_previous = os.path.sep.join( [str(previousday.to_pydatetime().date()), 'UnitOn.csv'] ) dict_UnitOnT0State = return_unitont0state_fromcsv(fcsv_previous) instance.UnitOnT0State.store_values(dict_UnitOnT0State) instance.UnitOnT0.reconstruct() instance.InitialTimePeriodsOnLine.reconstruct() instance.InitialTimePeriodsOffLine.reconstruct() dict_PowerGeneratedT0 = dict() for g in instance.ThermalGenerators: dict_PowerGeneratedT0[g] = value( instance.MinimumPowerOutput[g]*instance.UnitOnT0[g] ) instance.PowerGeneratedT0.store_values(dict_PowerGeneratedT0) # Now we can solve the UC model and save results results = opt.solve(instance) instance.solutions.load_from(results) store_csvs(instance, dir_results) print '{:>8s} {:>11s} {:>15.2f} {:>15.2f} {:>22s} {:>15.2f}'.format( str(iday+1)+'/'+str(T), str(thisday.to_pydatetime().date()), time() - t1, (time() - t0)/3600, datetime.datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H:%M:%S'), value(instance.TotalCostObjective), ) sys.stdout.flush() os.chdir(dir_home) # Jobs done, go home!
def solve_NLP(nlp_model, solve_data, config): """Solve the NLP subproblem.""" config.logger.info('Solving nonlinear subproblem for ' 'fixed binaries and logical realizations.') unfixed_discrete_vars = detect_unfixed_discrete_vars(nlp_model) if unfixed_discrete_vars: discrete_var_names = list(v.name for v in unfixed_discrete_vars) config.logger.warning( "Unfixed discrete variables exist on the NLP subproblem: %s" % (discrete_var_names, )) GDPopt = nlp_model.GDPopt_utils preprocessing_transformations = [ # Propagate variable bounds 'contrib.propagate_eq_var_bounds', # Detect fixed variables 'contrib.detect_fixed_vars', # Propagate fixed variables 'contrib.propagate_fixed_vars', # Remove zero terms in linear expressions 'contrib.remove_zero_terms', # Remove terms in equal to zero summations 'contrib.propagate_zero_sum', # Transform bound constraints 'contrib.constraints_to_var_bounds', # Detect fixed variables 'contrib.detect_fixed_vars', # Remove terms in equal to zero summations 'contrib.propagate_zero_sum', # Remove trivial constraints 'contrib.deactivate_trivial_constraints' ] for xfrm in preprocessing_transformations: TransformationFactory(xfrm).apply_to(nlp_model) initialize_NLP(nlp_model, solve_data) # Callback immediately before solving NLP subproblem config.call_before_subproblem_solve(nlp_model, solve_data) nlp_solver = SolverFactory(config.nlp_solver) if not nlp_solver.available(): raise RuntimeError("NLP solver %s is not available." % config.nlp_solver) with SuppressInfeasibleWarning(): results = nlp_solver.solve(nlp_model, **config.nlp_solver_args) nlp_result = SubproblemResult() nlp_result.feasible = True nlp_result.var_values = list(v.value for v in GDPopt.working_var_list) nlp_result.pyomo_results = results nlp_result.dual_values = list( nlp_model.dual.get(c, None) for c in GDPopt.working_constraints_list) subprob_terminate_cond = results.solver.termination_condition if subprob_terminate_cond is tc.optimal: pass elif subprob_terminate_cond is tc.infeasible: config.logger.info('NLP subproblem was locally infeasible.') nlp_result.feasible = False elif subprob_terminate_cond is tc.maxIterations: # TODO try something else? Reinitialize with different initial # value? config.logger.info( 'NLP subproblem failed to converge within iteration limit.') if is_feasible(nlp_model, config): config.logger.info( 'NLP solution is still feasible. ' 'Using potentially suboptimal feasible solution.') else: nlp_result.feasible = False elif subprob_terminate_cond is tc.internalSolverError: # Possible that IPOPT had a restoration failture config.logger.info("NLP solver had an internal failure: %s" % results.solver.message) nlp_result.feasible = False else: raise ValueError('GDPopt unable to handle NLP subproblem termination ' 'condition of %s. Results: %s' % (subprob_terminate_cond, results)) # Call the NLP post-solve callback config.call_after_subproblem_solve(nlp_model, solve_data) # if feasible, call the NLP post-feasible callback if nlp_result.feasible: config.call_after_subproblem_feasible(nlp_model, solve_data) return nlp_result