def __init__(self, convex=True, *args, **kwargs): """Create the flowsheet.""" kwargs.setdefault('name', 'DuranEx3') super(EightProcessFlowsheet, self).__init__(*args, **kwargs) m = self """Set declarations""" I = m.I = RangeSet(2, 25, doc="process streams") J = m.J = RangeSet(1, 8, doc="process units") m.PI = RangeSet(1, 4, doc="integer constraints") m.DS = RangeSet(1, 4, doc="design specifications") """ 1: Unit 8 2: Unit 8 3: Unit 4 4: Unit 4 """ m.MB = RangeSet(1, 7, doc="mass balances") """Material balances: 1: 4-6-7 2: 3-5-8 3: 4-5 4: 1-2 5: 1-2-3 6: 6-7-4 7: 6-7 """ """Parameter and initial point declarations""" # FIXED COST INVESTMENT COEFF FOR PROCESS UNITS # Format: process #: cost fixed_cost = {1: 5, 2: 8, 3: 6, 4: 10, 5: 6, 6: 7, 7: 4, 8: 5} CF = m.CF = Param(J, initialize=fixed_cost) # VARIABLE COST COEFF FOR PROCESS UNITS - STREAMS # Format: stream #: cost variable_cost = { 3: -10, 5: -15, 9: -40, 19: 25, 21: 35, 25: -35, 17: 80, 14: 15, 10: 15, 2: 1, 4: 1, 18: -65, 20: -60, 22: -80 } CV = m.CV = Param(I, initialize=variable_cost, default=0) # initial point information for equipment selection (for each NLP # subproblem) initY = { 'sub1': { 1: 1, 2: 0, 3: 1, 4: 1, 5: 0, 6: 0, 7: 1, 8: 1 }, 'sub2': { 1: 0, 2: 1, 3: 1, 4: 1, 5: 0, 6: 1, 7: 0, 8: 1 }, 'sub3': { 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 0, 7: 0, 8: 1 } } # initial point information for stream flows initX = { 1: 0, 2: 2, 3: 1.5, 4: 0, 5: 0, 6: 0.75, 7: 0.5, 8: 0.5, 9: 0.75, 10: 0, 11: 1.5, 12: 1.34, 13: 2, 14: 2.5, 15: 0, 16: 0, 17: 2, 18: 0.75, 19: 2, 20: 1.5, 21: 0, 22: 0, 23: 1.7, 24: 1.5, 25: 0.5 } """Variable declarations""" # BINARY VARIABLE DENOTING EXISTENCE-NONEXISTENCE Y = m.Y = Var(J, domain=Binary, initialize=initY['sub1']) # FLOWRATES OF PROCESS STREAMS X = m.X = Var(I, domain=NonNegativeReals, initialize=initX) # OBJECTIVE FUNCTION CONSTANT TERM CONSTANT = m.constant = Param(initialize=122.0) """Constraint definitions""" # INPUT-OUTPUT RELATIONS FOR process units 1 through 8 m.inout3 = Constraint(expr=1.5 * m.X[9] + m.X[10] == m.X[8]) m.inout4 = Constraint(expr=1.25 * (m.X[12] + m.X[14]) == m.X[13]) m.inout5 = Constraint(expr=m.X[15] == 2 * m.X[16]) if convex: m.inout1 = Constraint(expr=exp(m.X[3]) - 1 <= m.X[2]) m.inout2 = Constraint(expr=exp(m.X[5] / 1.2) - 1 <= m.X[4]) m.inout7 = Constraint(expr=exp(m.X[22]) - 1 <= m.X[21]) m.inout8 = Constraint(expr=exp(m.X[18]) - 1 <= m.X[10] + m.X[17]) m.inout6 = Constraint(expr=exp(m.X[20] / 1.5) - 1 <= m.X[19]) else: m.inout1 = Constraint(expr=exp(m.X[3]) - 1 == m.X[2]) m.inout2 = Constraint(expr=exp(m.X[5] / 1.2) - 1 == m.X[4]) m.inout7 = Constraint(expr=exp(m.X[22]) - 1 == m.X[21]) m.inout8 = Constraint(expr=exp(m.X[18]) - 1 == m.X[10] + m.X[17]) m.inout6 = Constraint(expr=exp(m.X[20] / 1.5) - 1 == m.X[19]) # Mass balance equations m.massbal1 = Constraint(expr=m.X[13] == m.X[19] + m.X[21]) m.massbal2 = Constraint(expr=m.X[17] == m.X[9] + m.X[16] + m.X[25]) m.massbal3 = Constraint(expr=m.X[11] == m.X[12] + m.X[15]) m.massbal4 = Constraint(expr=m.X[3] + m.X[5] == m.X[6] + m.X[11]) m.massbal5 = Constraint(expr=m.X[6] == m.X[7] + m.X[8]) m.massbal6 = Constraint(expr=m.X[23] == m.X[20] + m.X[22]) m.massbal7 = Constraint(expr=m.X[23] == m.X[14] + m.X[24]) # process specifications m.specs1 = Constraint(expr=m.X[10] <= 0.8 * m.X[17]) m.specs2 = Constraint(expr=m.X[10] >= 0.4 * m.X[17]) m.specs3 = Constraint(expr=m.X[12] <= 5 * m.X[14]) m.specs4 = Constraint(expr=m.X[12] >= 2 * m.X[14]) # Logical constraints (big-M) for each process. # These allow for flow iff unit j exists m.logical1 = Constraint(expr=m.X[2] <= 10 * m.Y[1]) m.logical2 = Constraint(expr=m.X[4] <= 10 * m.Y[2]) m.logical3 = Constraint(expr=m.X[9] <= 10 * m.Y[3]) m.logical4 = Constraint(expr=m.X[12] + m.X[14] <= 10 * m.Y[4]) m.logical5 = Constraint(expr=m.X[15] <= 10 * m.Y[5]) m.logical6 = Constraint(expr=m.X[19] <= 10 * m.Y[6]) m.logical7 = Constraint(expr=m.X[21] <= 10 * m.Y[7]) m.logical8 = Constraint(expr=m.X[10] + m.X[17] <= 10 * m.Y[8]) # pure integer constraints m.pureint1 = Constraint(expr=m.Y[1] + m.Y[2] == 1) m.pureint2 = Constraint(expr=m.Y[4] + m.Y[5] <= 1) m.pureint3 = Constraint(expr=m.Y[6] + m.Y[7] - m.Y[4] == 0) m.pureint4 = Constraint(expr=m.Y[3] - m.Y[8] <= 0) """Cost (objective) function definition""" m.cost = Objective(expr=sum(Y[j] * CF[j] for j in J) + sum(X[i] * CV[i] for i in I) + CONSTANT, sense=minimize) """Bound definitions""" # x (flow) upper bounds # x_ubs = {3: 2, 5: 2, 9: 2, 10: 1, 14: 1, 17: 2, 19: 2, 21: 2, 25: 3} x_ubs = { 2: 10, 3: 2, 4: 10, 5: 2, 9: 2, 10: 1, 14: 1, 17: 2, 18: 10, 19: 2, 20: 10, 21: 2, 22: 10, 25: 3 } # add bounds for variables in nonlinear constraints for i, x_ub in iteritems(x_ubs): X[i].setub(x_ub)
def test_obj_con_cache(self): model = ConcreteModel() model.x = Var() model.c = Constraint(expr=model.x >= 1) model.obj = Objective(expr=model.x * 2) with TempfileManager.new_context() as TMP: lp_file = TMP.create_tempfile(suffix='.lp') model.write(lp_file, format='lp') self.assertFalse(hasattr(model, '_repn')) with open(lp_file) as FILE: lp_ref = FILE.read() lp_file = TMP.create_tempfile(suffix='.lp') model._gen_obj_repn = True model.write(lp_file) self.assertEqual(len(model._repn), 1) self.assertIn(model.obj, model._repn) obj_repn = model._repn[model.obj] with open(lp_file) as FILE: lp_test = FILE.read() self.assertEqual(lp_ref, lp_test) lp_file = TMP.create_tempfile(suffix='.lp') model._gen_obj_repn = None model._gen_con_repn = True model.write(lp_file) self.assertEqual(len(model._repn), 2) self.assertIn(model.obj, model._repn) self.assertIn(model.c, model._repn) self.assertIs(obj_repn, model._repn[model.obj]) obj_repn = model._repn[model.obj] c_repn = model._repn[model.c] with open(lp_file) as FILE: lp_test = FILE.read() self.assertEqual(lp_ref, lp_test) lp_file = TMP.create_tempfile(suffix='.lp') model._gen_obj_repn = None model._gen_con_repn = None model.write(lp_file) self.assertEqual(len(model._repn), 2) self.assertIn(model.obj, model._repn) self.assertIn(model.c, model._repn) self.assertIs(obj_repn, model._repn[model.obj]) self.assertIs(c_repn, model._repn[model.c]) with open(lp_file) as FILE: lp_test = FILE.read() self.assertEqual(lp_ref, lp_test) lp_file = TMP.create_tempfile(suffix='.lp') model._gen_obj_repn = True model._gen_con_repn = True model.write(lp_file) self.assertEqual(len(model._repn), 2) self.assertIn(model.obj, model._repn) self.assertIn(model.c, model._repn) self.assertIsNot(obj_repn, model._repn[model.obj]) self.assertIsNot(c_repn, model._repn[model.c]) obj_repn = model._repn[model.obj] c_repn = model._repn[model.c] with open(lp_file) as FILE: lp_test = FILE.read() self.assertEqual(lp_ref, lp_test) lp_file = TMP.create_tempfile(suffix='.lp') model._gen_obj_repn = False model._gen_con_repn = False import pyomo.repn.plugins.ampl.ampl_ as ampl_ gsr = ampl_.generate_standard_repn try: def dont_call_gsr(*args, **kwargs): self.fail("generate_standard_repn should not be called") ampl_.generate_standard_repn = dont_call_gsr model.write(lp_file) finally: ampl_.generate_standard_repn = gsr self.assertEqual(len(model._repn), 2) self.assertIn(model.obj, model._repn) self.assertIn(model.c, model._repn) self.assertIs(obj_repn, model._repn[model.obj]) self.assertIs(c_repn, model._repn[model.c]) with open(lp_file) as FILE: lp_test = FILE.read() self.assertEqual(lp_ref, lp_test)
def __init__(self, y, x, tau, cutactive, active, cet=CET_ADDI, fun=FUN_PROD, rts=RTS_VRS): """CQR+G model Args: y (float): output variable. x (float): input variables. tau (float): quantile. cutactive (float): active concavity constraint. active (float): violated concavity constraint. cet (String, optional): CET_ADDI (additive composite error term) or CET_MULT (multiplicative composite error term). Defaults to CET_ADDI. fun (String, optional): FUN_PROD (production frontier) or FUN_COST (cost frontier). Defaults to FUN_PROD. rts (String, optional): RTS_VRS (variable returns to scale) or RTS_CRS (constant returns to scale). Defaults to RTS_VRS. """ # TODO(error/warning handling): Check the configuration of the model exist self.x = x self.y = y self.tau = tau self.cet = cet self.fun = fun self.rts = rts self.cutactive = cutactive self.active = to_2d_list(trans_list(active)) # Initialize the CNLS model self.__model__ = ConcreteModel() # Initialize the sets self.__model__.I = Set(initialize=range(len(self.y))) self.__model__.J = Set(initialize=range(len(self.x[0]))) # Initialize the variables self.__model__.alpha = Var(self.__model__.I, doc='alpha') self.__model__.beta = Var(self.__model__.I, self.__model__.J, bounds=(0.0, None), doc='beta') self.__model__.epsilon = Var(self.__model__.I, doc='resiudual') self.__model__.epsilon_plus = Var( self.__model__.I, bounds=(0.0, None), doc='positive error term') self.__model__.epsilon_minus = Var( self.__model__.I, bounds=(0.0, None), doc='negative error term') self.__model__.frontier = Var(self.__model__.I, bounds=(0.0, None), doc='estimated frontier') # Setup the objective function and constraints self.__model__.objective = Objective(rule=self.__objective_rule(), sense=minimize, doc='objective function') self.__model__.error_decomposition = Constraint(self.__model__.I, rule=self.__error_decomposition(), doc='decompose error term') self.__model__.regression_rule = Constraint(self.__model__.I, rule=self.__regression_rule(), doc='regression equation') if self.cet == CET_MULT: self.__model__.log_rule = Constraint(self.__model__.I, rule=self.__log_rule(), doc='log-transformed regression equation') self.__model__.afriat_rule = Constraint(self.__model__.I, rule=self.__afriat_rule(), doc='elementary Afriat approach') self.__model__.sweet_rule = Constraint(self.__model__.I, self.__model__.I, rule=self.__sweet_rule(), doc='sweet spot approach') self.__model__.sweet_rule2 = Constraint(self.__model__.I, self.__model__.I, rule=self.__sweet_rule2(), doc='sweet spot-2 approach') # Optimize model self.optimization_status = 0 self.problem_status = 0
def _solve_sigma_given_delta(var_est_object, solver, **kwds): """Solves the delta (device variance) with provided variances :param VarianceEstimator var_est_object: The variance estimation object :param str solver: The solver being used (currently not used) :param dict kwds: The dict of user settings passed on from the ReactionModel :return residuals: The results from the variance estimation :return variances_dict: dictionary containing the model variance values :return stop_it: boolean indicator showing whether no solution was found (True) or if there is a solution (False) :return solver_results: dictionary containing solver options for IPOPT :rtype: ResultsObject :Keyword Args: - delta (float): the device variance - tee (bool,optional): flag to tell the optimizer whether to stream output to the terminal or not - profile_time (bool,optional): flag to tell pyomo to time the construction and solution of the model. Default False - subset_lambdas (array_like,optional): Set of wavelengths to used in initialization problem (Weifeng paper). Default all wavelengths. - solver_opts (dict, optional): dictionary containing solver options for IPOPT """ solver_opts = kwds.pop('solver_opts', dict()) tee = kwds.pop('tee', False) set_A = kwds.pop('subset_lambdas', list()) profile_time = kwds.pop('profile_time', False) delta_sq = kwds.pop('delta', dict()) #species_list = kwds.pop('subset_components', None) model = var_est_object.model.clone() if not set_A: set_A = var_est_object.model.meas_lambdas # if not hasattr(var_est_object, '_sublist_components'): # list_components = [] # if species_list is None: # list_components = [k for k in var_est_object._mixture_components] # else: # for k in species_list: # if k in var_est_object._mixture_components: # list_components.append(k) # else: # warnings.warn("Ignored {} since is not a mixture component of the model".format(k)) # var_est_object._sublist_components = list_components var_est_object._warn_if_D_negative() ntp = len(var_est_object.model.times_spectral) obj = 0.0 for t in var_est_object.model.times_spectral: for l in set_A: D_bar = sum(var_est_object.model.C[t, k] * var_est_object.model.S[l, k] for k in var_est_object.comps['unknown_absorbance']) obj += 0.5 / delta_sq * (var_est_object.model.D[t, l] - D_bar)**2 inlog = {k: 0 for k in var_est_object.comps['unknown_absorbance']} var_est_object.model.eps = Param(initialize=1e-8) variances_dict = {k: 0 for k in var_est_object.comps['unknown_absorbance']} for t in var_est_object.model.times_spectral: for k in var_est_object.comps['unknown_absorbance']: inlog[k] += ((var_est_object.model.C[t, k] - var_est_object.model.Z[t, k])**2) for k in var_est_object.comps['unknown_absorbance']: obj += 0.5 * ntp * log(inlog[k] / ntp + var_est_object.model.eps) var_est_object.model.init_objective = Objective(expr=obj) opt = SolverFactory(solver) for key, val in solver_opts.items(): opt.options[key] = val try: solver_results = opt.solve(var_est_object.model, tee=tee, report_timing=profile_time) residuals = (value(var_est_object.model.init_objective)) for t in var_est_object.model.times_spectral: for k in var_est_object.comps['unknown_absorbance']: variances_dict[k] += 1 / ntp * ( (value(var_est_object.model.C[t, k]) - value(var_est_object.model.Z[t, k]))**2) print("Variances") for k in var_est_object.comps['unknown_absorbance']: print(k, variances_dict[k]) print("Parameter estimates") for k, v in var_est_object.model.P.items(): print(k, v.value) stop_it = False except: print("FAILED AT THIS ITERATION") variances_dict = None residuals = 0 stop_it = True solver_results = None for k, v in var_est_object.model.P.items(): print(k, v.value) var_est_object.model = model for k, v in var_est_object.model.P.items(): print(k, v.value) var_est_object.model.del_component('init_objective') var_est_object.model.del_component('eps') return residuals, variances_dict, stop_it, solver_results
def TSPSYM(G): n = G.number_of_nodes() m = ConcreteModel() m.N = RangeSet(n) m.A = Set(initialize=((i, j) for i, j in G.edges()), dimen=2) m.x = Var(m.A, domain=NonNegativeReals, bounds=lambda m: (0, 1)) m.obj = Objective(expr=sum(G[i][j]['weight'] * m.x[i, j] for i, j in G.edges())) m.degree = ConstraintList() for i in m.N: Es = [] for v, w in G.edges(i): if v > w: v, w = w, v Es.append((v, w)) m.degree.add(sum(m.x[v, w] for v, w in Es) == 2) m.subtour = ConstraintList() solver = SolverFactory('gurobi') it = 0 Cold = [] while it <= 100: it += 1 sol = solver.solve(m, tee=False, load_solutions=False) sol_json = sol.json_repn() if sol_json['Solver'][0]['Status'] != 'ok': return None, [] m.solutions.load_from(sol) print(it, 'LP:', m.obj()) selected = [] values = [] for i, j in m.A: if i < j: if m.x[i, j]() > 0.0001: selected.append((i - 1, j - 1)) values.append(m.x[i, j]()) PlotTour(Ls, selected, values) # Costruiamo un grafo supporto H = nx.Graph() for i, j in m.A: H.add_edge(i, j, weight=m.x[i, j]()) cut_value, S = nx.stoer_wagner(H) # Taglio di peso minimo if cut_value >= 2: break Es = [] for i in S[0]: for j in S[1]: if i < j: Es.append((i, j)) else: Es.append((j, i)) m.subtour.add(sum(m.x[i, j] for i, j in Es) >= 2) return m.obj(), selected
def __init__(self, z_ideal, z_nadir, z_ref, data, to_minmax, to_stay, to_detoriate, curr_values, weights=None, nvar=None, eps=0.00001, roo=0.01): ''' Implements the NIMBUS method. Ideal, nadir and ref vectors of equal length data, data without normalization to_minmax, array of indices for the objectives to be improved to_stay, array of indices for the objectives to stay the same to_detoriate, array of indices for the objectives to detoriate to a limit curr_values, current values of all the objectives as a vector''' if len(z_ideal) != len(z_nadir) or len(z_ideal) != len(z_ref): print("Length of given vectors don't match") return super().__init__(data, weights, nvar, False) model = self.model model.to_minmax = Set(within=NonNegativeIntegers, initialize=to_minmax) model.to_stay = Set(within=NonNegativeIntegers, initialize=to_stay) model.to_detoriate = Set(within=NonNegativeIntegers, initialize=to_detoriate) model.lim = Set(within=NonNegativeIntegers, initialize=np.concatenate( (to_minmax, to_stay, to_detoriate))) limits = np.zeros(len(z_ref)) limits[to_minmax] = curr_values[to_minmax] limits[to_stay] = curr_values[to_stay] limits[to_detoriate] = z_ref[to_detoriate] model.k = Param(within=NonNegativeIntegers, initialize=len(z_ref)) model.H = RangeSet(0, model.k - 1) def init_ideal(model, h): return z_ideal[h] model.ideal = Param(model.H, initialize=init_ideal) def init_nadir(model, h): return z_nadir[h] - eps model.nadir = Param(model.H, initialize=init_nadir) def init_utopia(model, h): return z_ideal[h] + eps model.utopia = Param(model.H, initialize=init_utopia) def init_ref(model, h): return z_ref[h] model.ref = Param(model.H, initialize=init_ref) def init_limits(model, i): return limits[i] model.limits = Param(model.lim, initialize=init_limits) model.roo = roo model.maximum = Var() def nimbusconst_max(model, i): ''' Nimbus constraint: Set lower limits for all the objectives, because improving means now maximizing. Works when all the lower limits set properly as opposite to the Nimbus method''' return self.obj_fun(model, data)[i] >= model.limits[i] model.ConstraintNimbus = Constraint(model.lim, rule=nimbusconst_max) def minmaxconst(model, i): ''' Constraint: The new "maximum" variable, that will be minimized in the optimization, must be greater than any of the original divisions used in original ASF formulation.''' return model.maximum >= \ np.divide(np.subtract(self.obj_fun(model, data)[i], model.ref[i]), np.subtract(model.nadir[i], model.utopia[i])) model.ConstraintMax = Constraint(model.to_minmax, rule=minmaxconst) def asf_fun(model): return model.maximum \ + model.roo*sum([np.divide(self.obj_fun(model, data)[h], model.nadir[h] - model.utopia[h]) for h in model.H]) if hasattr(model, 'OBJ'): del model.OBJ # Delete previous Objective to suppress warnings model.OBJ = Objective(rule=asf_fun, sense=minimize) self.model = model self._modelled = True
def __init__(self, results, threshold=None, k=10, solver="glpk", verbosity=0): """ :param result: Epitope prediction result object from which the epitope selection should be performed :type result: :class:`~Fred2.Core.Result.EpitopePredictionResult` :param dict(str,float) threshold: A dictionary scoring the binding thresholds for each HLA :class:`~Fred2.Core.Allele.Allele` key = allele name; value = the threshold :param int k: The number of epitopes to select :param str solver: The solver to be used (default glpk) :param int verbosity: Integer defining whether additional debugg prints are made >0 => debug mode """ #check input data if not isinstance(results, EpitopePredictionResult): raise ValueError( "first input parameter is not of type EpitopePredictionResult") _alleles = copy.deepcopy(results.columns.values.tolist()) #test if allele prob is set, if not set allele prob uniform #if only partly set infer missing values (assuming uniformity of missing value) prob = [] no_prob = [] for a in _alleles: if a.prob is None: no_prob.append(a) else: prob.append(a) if len(no_prob) > 0: #group by locus no_prob_grouped = {} prob_grouped = {} for a in no_prob: no_prob_grouped.setdefault(a.locus, []).append(a) for a in prob: prob_grouped.setdefault(a.locus, []).append(a) for g, v in no_prob_grouped.items(): total_loc_a = len(v) if g in prob_grouped: remaining_mass = 1.0 - sum(a.prob for a in prob_grouped[g]) for a in v: a.prob = remaining_mass / total_loc_a else: for a in v: a.prob = 1.0 / total_loc_a probs = {a.name: a.prob for a in _alleles} if verbosity: for a in _alleles: print(a.name, a.prob) #start constructing model self.__solver = SolverFactory(solver) self.__verbosity = verbosity self.__changed = True self.__alleleProb = _alleles self.__k = k self.__result = None self.__thresh = {} if threshold is None else threshold # Variable, Set and Parameter preparation alleles_I = {} variations = [] epi_var = {} imm = {} peps = {} cons = {} #unstack multiindex df to get normal df based on first prediction method #and filter for binding epitopes method = results.index.values[0][1] res_df = results.xs(results.index.values[0][1], level="Method") res_df = res_df[res_df.apply( lambda x: any(x[a] > self.__thresh.get(a.name, -float("inf")) for a in res_df.columns), axis=1)] for tup in res_df.itertuples(): p = tup[0] seq = str(p) peps[seq] = p for a, s in itr.izip(res_df.columns, tup[1:]): if method in ["smm", "smmpmbec", "arb", "comblibsidney"]: try: thr = min( 1., max( 0.0, 1.0 - math.log(self.__thresh.get(a.name), 50000)) ) if a.name in self.__thresh else -float("inf") except: thr = 0 if s >= thr: alleles_I.setdefault(a.name, set()).add(seq) imm[seq, a.name] = min(1., max(0.0, 1.0 - math.log(s, 50000))) else: if s > self.__thresh.get(a.name, -float("inf")): alleles_I.setdefault(a.name, set()).add(seq) imm[seq, a.name] = s prots = set(pr for pr in p.get_all_proteins()) cons[seq] = len(prots) for prot in prots: variations.append(prot.gene_id) epi_var.setdefault(prot.gene_id, set()).add(seq) self.__peptideSet = peps #calculate conservation variations = set(variations) total = len(variations) for e, v in cons.items(): try: cons[e] = v / total except ZeroDivisionError: cons[e] = 1 model = ConcreteModel() #set definition model.Q = Set(initialize=variations) model.E = Set(initialize=set(peps.keys())) model.A = Set(initialize=list(alleles_I.keys())) model.E_var = Set(model.Q, initialize=lambda mode, v: epi_var[v]) model.A_I = Set(model.A, initialize=lambda model, a: alleles_I[a]) #parameter definition model.k = Param(initialize=self.__k, within=PositiveIntegers, mutable=True) model.p = Param(model.A, initialize=lambda model, a: probs[a]) model.c = Param(model.E, initialize=lambda model, e: cons[e], mutable=True) #threshold parameters model.i = Param(model.E, model.A, initialize=lambda model, e, a: imm[e, a]) model.t_allele = Param(initialize=0, within=NonNegativeIntegers, mutable=True) model.t_var = Param(initialize=0, within=NonNegativeIntegers, mutable=True) model.t_c = Param(initialize=0.0, within=NonNegativeReals, mutable=True) # Variable Definition model.x = Var(model.E, within=Binary) model.y = Var(model.A, within=Binary) model.z = Var(model.Q, within=Binary) # Objective definition model.Obj = Objective( rule=lambda model: sum(model.x[e] * sum(model.p[a] * model.i[e, a] for a in model.A) for e in model.E), sense=maximize) #Obligatory Constraint (number of selected epitopes) model.NofSelectedEpitopesCov = Constraint( rule=lambda model: sum(model.x[e] for e in model.E) <= model.k) #optional constraints (in basic model they are disabled) model.IsAlleleCovConst = Constraint( model.A, rule=lambda model, a: sum(model.x[e] for e in model.A_I[a]) >= model.y[a]) model.MinAlleleCovConst = Constraint(rule=lambda model: sum( model.y[a] for a in model.A) >= model.t_allele) model.IsAntigenCovConst = Constraint( model.Q, rule=lambda model, q: sum(model.x[e] for e in model.E_var[q]) >= model.z[q]) model.MinAntigenCovConst = Constraint( rule=lambda model: sum(model.z[q] for q in model.Q) >= model.t_var) model.EpitopeConsConst = Constraint( model.E, rule=lambda model, e: (1 - model.c[e]) * model.x[e] <= 1 - model.t_c) #generate instance self.instance = model if self.__verbosity > 0: print("MODEL INSTANCE") self.instance.pprint() #constraints self.instance.IsAlleleCovConst.deactivate() self.instance.MinAlleleCovConst.deactivate() self.instance.IsAntigenCovConst.deactivate() self.instance.MinAntigenCovConst.deactivate() self.instance.EpitopeConsConst.deactivate()
def create_model(): m = ConcreteModel() m.tf = Param(initialize=20) m.t = ContinuousSet(bounds=(0, m.tf)) m.i = Set(initialize=[0, 1, 2, 3, 4, 5], ordered=True) m.j = Set(initialize=[0, 1], ordered=True) m.ij = Set(initialize=[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (4, 0)], ordered=True) #Set Parameters m.eps = Param(initialize=0.75, mutable=True) m.sig = Param(initialize=0.15, mutable=True) m.xi = Param(initialize=0.983, mutable=True) m.omeg = Param(initialize=1.0 / 10, mutable=True) m.Y0 = Param(initialize=55816.0, mutable=True) m.phi0 = Param(initialize=0.493, mutable=True) d = {} d[(0, 0)] = 0.0222 d[(0, 1)] = 0.0222 d[(1, 0)] = 1 / 7.1 d[(1, 1)] = 1 / 7.1 d[(2, 0)] = 1 / 8.1 d[(2, 1)] = 1 / (8.1 + 5) d[(3, 0)] = 1 / 2.7 d[(4, 0)] = 1 / 2.1 m.dd = Param(m.ij, initialize=d, mutable=True) d_inv = {} d_inv[(1, 0)] = 7.1 d_inv[(2, 0)] = 8.1 d_inv[(3, 0)] = 2.7 d_inv[(4, 0)] = 2.1 m.dd_inv = Param(m.ij, initialize=d_inv, default=0, mutable=True) I = {} I[(0, 0)] = 0.9 * m.dd[(0, 0)] * m.Y0 I[(1, 0)] = 0.04 * m.dd[(0, 0)] * m.Y0 I[(2, 0)] = 0.04 * m.dd[(0, 0)] * m.Y0 I[(3, 0)] = 0.02 * m.dd[(0, 0)] * m.Y0 m.II = Param(m.ij, initialize=I, default=0, mutable=True) p = {} p[(4, 0)] = 0.667 m.pp = Param(m.ij, initialize=p, default=2, mutable=True) b = {} b[(1, 0)] = 0.066 b[(1, 1)] = 0.066 b[(2, 0)] = 0.066 b[(2, 1)] = 0.066 * (1 - 0.25) b[(3, 0)] = 0.147 b[(4, 0)] = 0.147 m.bb = Param(m.ij, initialize=b, default=0, mutable=True) eta00 = {} eta00[(0, 0)] = 0.505 eta00[(0, 1)] = 0.505 eta00[(1, 0)] = 0.505 eta00[(1, 1)] = 0.505 eta00[(2, 0)] = 0.307 eta00[(2, 1)] = 0.4803 eta00[(3, 0)] = 0.235 eta00[(4, 0)] = 0.235 m.eta00 = Param(m.ij, initialize=eta00, mutable=True) eta01 = {} eta01[(0, 0)] = 0.505 eta01[(0, 1)] = 0.6287 eta01[(1, 0)] = 0.505 eta01[(1, 1)] = 0.6287 eta01[(2, 0)] = 0.307 eta01[(2, 1)] = 0.4803 eta01[(3, 0)] = 0.235 eta01[(4, 0)] = 0.235 m.eta01 = Param(m.ij, initialize=eta01, mutable=True) m.kp = Param(initialize=1000.0, mutable=True) m.kt = Param(initialize=1000.0, mutable=True) m.rr = Param(initialize=0.05, mutable=True) c = {} c[(0, 0)] = 3307 c[(0, 1)] = 3307 c[(1, 0)] = 5467 c[(1, 1)] = 5467 c[(2, 0)] = 5467 c[(2, 1)] = 5467 c[(3, 0)] = 12586 c[(4, 0)] = 35394 m.cc = Param(m.ij, initialize=c, mutable=True) q = {} q[(0, 0)] = 1 q[(0, 1)] = 1 q[(1, 0)] = 1 q[(1, 1)] = 1 q[(2, 0)] = 0.83 q[(2, 1)] = 0.83 q[(3, 0)] = 0.42 q[(4, 0)] = 0.17 m.qq = Param(m.ij, initialize=q, mutable=True) m.aa = Param(initialize=0.0001, mutable=True) #Set Variables m.yy = Var(m.t, m.ij) m.L = Var(m.t) m.vp = Var(m.t, initialize=0.75, bounds=(0, 0.75)) m.vt = Var(m.t, initialize=0.75, bounds=(0, 0.75)) m.dyy = DerivativeVar(m.yy, wrt=m.t) m.dL = DerivativeVar(m.L, wrt=m.t) def CostFunc(m): return m.L[m.tf] m.cf = Objective(rule=CostFunc) def _initDistConst(m): return (m.phi0 * m.Y0) / sum(m.dd_inv[kk] for kk in m.ij) m.idc = Expression(rule=_initDistConst) m.yy[0, (0, 0)].fix(value((1 - m.phi0) * m.Y0)) m.yy[0, (0, 1)].fix(0) m.yy[0, (1, 0)].fix(value(m.dd_inv[(1, 0)] * m.idc)) m.yy[0, (1, 1)].fix(0) m.yy[0, (2, 0)].fix(value(m.dd_inv[(2, 0)] * m.idc)) m.yy[0, (2, 1)].fix(0) m.yy[0, (3, 0)].fix(value(m.dd_inv[(3, 0)] * m.idc)) m.yy[0, (4, 0)].fix(value(m.dd_inv[(4, 0)] * m.idc)) m.L[0].fix(0) #ODEs def _yy00(m, t): return sum(m.pp[kk]*m.yy[t,kk] for kk in m.ij)*m.dyy[t,(0,0)] == \ sum(m.pp[kk]*m.yy[t,kk] for kk in m.ij)*(m.II[(0,0)]-\ (m.vp[t]+m.dd[(0,0)])*m.yy[t,(0,0)]+m.omeg*m.yy[t,(0,1)])-\ m.pp[(0,0)]*sum(m.bb[kk]*m.eta00[kk]*m.pp[kk]*m.yy[t,kk] for kk in m.ij)*m.yy[t,(0,0)] m.yy00DiffCon = Constraint(m.t, rule=_yy00) def _yy01(m, t): return sum(m.pp[kk]*m.yy[t,kk] for kk in m.ij)*m.dyy[t,(0,1)] == \ sum(m.pp[kk]*m.yy[t,kk] for kk in m.ij)*(m.vp[t]*m.yy[t,(0,0)]- (m.dd[(0,0)]+m.omeg)*m.yy[t,(0,1)])-\ m.pp[(0,1)]*(1-m.eps)*sum(m.bb[kk]*m.eta01[kk]* m.pp[kk]*m.yy[t,kk] for kk in m.ij)*m.yy[t,(0,1)] m.yy01DiffCon = Constraint(m.t, rule=_yy01) def _yy10(m, t): return sum(m.pp[kk]*m.yy[t,kk] for kk in m.ij)*m.dyy[t,(1,0)] == \ sum(m.pp[kk]*m.yy[t,kk] for kk in m.ij)*\ (m.II[(1,0)]-((m.sig*m.xi)+m.vp[t]+m.dd[(1,0)]+m.dd[(0,0)])* m.yy[t,(1,0)]+m.omeg*m.yy[t,(1,1)] )+m.pp[(0,0)]*sum(m.bb[kk]*m.eta00[kk]* m.pp[kk]*m.yy[t,kk] for kk in m.ij)*m.yy[t,(0,0)] m.yy10DiffCon = Constraint(m.t, rule=_yy10) def _yy11(m, t): return sum(m.pp[kk]*m.yy[t,kk] for kk in m.ij)*m.dyy[t,(1,1)] == \ sum(m.pp[kk]*m.yy[t,kk] for kk in m.ij)*(m.vp[t]*m.yy[t,(1,0)]-\ (m.omeg+(m.sig*m.xi)+m.dd[(1,1)]+m.dd[(0,0)])*m.yy[t,(1,1)])+\ m.pp[(0,1)]*(1-m.eps)*sum(m.bb[kk]*m.eta01[kk]* m.pp[kk]*m.yy[t,kk] for kk in m.ij)*m.yy[t,(0,1)] m.yy11DiffCon = Constraint(m.t, rule=_yy11) def _yy20(m, t): return m.dyy[t,(2,0)] == \ m.II[(2,0)]+m.sig*m.xi*(m.yy[t,(1,0)]+m.yy[t,(1,1)])-\ (m.vt[t]+m.dd[(2,0)]+m.dd[(0,0)])*m.yy[t,(2,0)] m.yy20DiffCon = Constraint(m.t, rule=_yy20) def _yy21(m, t): return m.dyy[t,(2,1)] == \ m.vt[t]*m.yy[t,(2,0)]-(m.dd[(2,1)]+m.dd[(0,0)])*m.yy[t,(2,1)] m.yy21DiffCon = Constraint(m.t, rule=_yy21) def _yy30(m, t): return m.dyy[t,(3,0)] == \ m.II[(3,0)]+m.dd[(1,0)]*m.yy[t,(1,0)]+\ m.dd[(1,1)]*m.yy[t,(1,1)]+m.dd[(2,0)]*m.yy[t,(2,0)]+\ m.dd[(2,1)]*m.yy[t,(2,1)]-\ (m.dd[(3,0)]+m.dd[(0,0)])*m.yy[t,(3,0)] m.yy30DiffCon = Constraint(m.t, rule=_yy30) def _yy40(m, t): return m.dyy[t, (4,0)] == \ m.dd[(3,0)]*m.yy[t,(3,0)]-(m.dd[(4,0)]+\ m.dd[(0,0)])*m.yy[t,(4,0)] m.yy40DiffCon = Constraint(m.t, rule=_yy40) def _L(m, t): return m.dL[t] == \ exp(-m.rr*t)*(m.aa*(m.kp*m.vp[t]*(m.yy[t,(0,0)]+m.yy[t,(1,0)]) \ +(m.kt*m.vt[t]*m.yy[t,(2,0)])+sum(m.cc[kk]*m.yy[t,kk] for kk in m.ij)) \ -(1-m.aa)*sum(m.qq[kk]*m.yy[t,kk] for kk in m.ij)) m.LDiffCon = Constraint(m.t, rule=_L) return m
def __init__(self, y, x, b=None, gy=[1], gx=[1], gb=None, fun=FUN_PROD, tau=0.5): """CQR DDF Args: y (float): output variable. x (float): input variables. b (float), optional): undesirable output variables. Defaults to None. gy (list, optional): output directional vector. Defaults to [1]. gx (list, optional): input directional vector. Defaults to [1]. gb (list, optional): undesirable output directional vector. Defaults to None. fun (String, optional): FUN_PROD (production frontier) or FUN_COST (cost frontier). Defaults to FUN_PROD. tau (float, optional): quantile. Defaults to 0.5. """ # TODO(error/warning handling): Check the configuration of the model exist self.y, self.x, self.b, self.gy, self.gx, self.gb = tools.assert_valid_direciontal_data( y, x, b, gy, gx, gb) self.tau = tau self.fun = fun self.rts = RTS_VRS self.__model__ = ConcreteModel() # Initialize the sets self.__model__.I = Set(initialize=range(len(self.y))) self.__model__.J = Set(initialize=range(len(self.x[0]))) self.__model__.K = Set(initialize=range(len(self.y[0]))) # Initialize the variables self.__model__.alpha = Var(self.__model__.I, doc='alpha') self.__model__.beta = Var(self.__model__.I, self.__model__.J, bounds=(0.0, None), doc='beta') self.__model__.gamma = Var(self.__model__.I, self.__model__.K, bounds=(0.0, None), doc='gamma') self.__model__.epsilon = Var(self.__model__.I, doc='residuals') self.__model__.epsilon_plus = Var(self.__model__.I, bounds=(0.0, None), doc='positive error term') self.__model__.epsilon_minus = Var(self.__model__.I, bounds=(0.0, None), doc='negative error term') if type(self.b) != type(None): self.__model__.L = Set(initialize=range(len(self.b[0]))) self.__model__.delta = Var(self.__model__.I, self.__model__.L, bounds=(0.0, None), doc='delta') self.__model__.objective = Objective(rule=self._CQR__objective_rule(), sense=minimize, doc='objective function') self.__model__.error_decomposition = Constraint( self.__model__.I, rule=self._CQR__error_decomposition(), doc='decompose error term') self.__model__.regression_rule = Constraint( self.__model__.I, rule=self.__regression_rule(), doc='regression equation') self.__model__.translation_rule = Constraint( self.__model__.I, rule=self._CNLSDDF__translation_property(), doc='translation property') self.__model__.afriat_rule = Constraint(self.__model__.I, self.__model__.I, rule=self.__afriat_rule(), doc='afriat inequality') # Optimize model self.optimization_status = 0 self.problem_status = 0
def generate_model(data): # unpack and fix the data cameastemp = data['Ca_meas'] cbmeastemp = data['Cb_meas'] ccmeastemp = data['Cc_meas'] trmeastemp = data['Tr_meas'] cameas = {} cbmeas = {} ccmeas = {} trmeas = {} for i in cameastemp.keys(): cameas[float(i)] = cameastemp[i] cbmeas[float(i)] = cbmeastemp[i] ccmeas[float(i)] = ccmeastemp[i] trmeas[float(i)] = trmeastemp[i] m = ConcreteModel() # # Measurement Data # m.measT = Set(initialize=sorted(cameas.keys())) m.Ca_meas = Param(m.measT, initialize=cameas) m.Cb_meas = Param(m.measT, initialize=cbmeas) m.Cc_meas = Param(m.measT, initialize=ccmeas) m.Tr_meas = Param(m.measT, initialize=trmeas) # # Parameters for semi-batch reactor model # m.R = Param(initialize=8.314) # kJ/kmol/K m.Mwa = Param(initialize=50.0) # kg/kmol m.rhor = Param(initialize=1000.0) # kg/m^3 m.cpr = Param(initialize=3.9) # kJ/kg/K m.Tf = Param(initialize=300) # K m.deltaH1 = Param(initialize=-40000.0) # kJ/kmol m.deltaH2 = Param(initialize=-50000.0) # kJ/kmol m.alphaj = Param(initialize=0.8) # kJ/s/m^2/K m.alphac = Param(initialize=0.7) # kJ/s/m^2/K m.Aj = Param(initialize=5.0) # m^2 m.Ac = Param(initialize=3.0) # m^2 m.Vj = Param(initialize=0.9) # m^3 m.Vc = Param(initialize=0.07) # m^3 m.rhow = Param(initialize=700.0) # kg/m^3 m.cpw = Param(initialize=3.1) # kJ/kg/K m.Ca0 = Param(initialize=data['Ca0']) # kmol/m^3) m.Cb0 = Param(initialize=data['Cb0']) # kmol/m^3) m.Cc0 = Param(initialize=data['Cc0']) # kmol/m^3) m.Tr0 = Param(initialize=300.0) # K m.Vr0 = Param(initialize=1.0) # m^3 m.time = ContinuousSet(bounds=(0, 21600), initialize=m.measT) # Time in seconds # # Control Inputs # def _initTc(m, t): if t < 10800: return data['Tc1'] else: return data['Tc2'] m.Tc = Param( m.time, initialize=_initTc, default=_initTc) # bounds= (288,432) Cooling coil temp, control input def _initFa(m, t): if t < 10800: return data['Fa1'] else: return data['Fa2'] m.Fa = Param( m.time, initialize=_initFa, default=_initFa) # bounds=(0,0.05) Inlet flow rate, control input # # Parameters being estimated # m.k1 = Var(initialize=14, bounds=(2, 100)) # 1/s Actual: 15.01 m.k2 = Var(initialize=90, bounds=(2, 150)) # 1/s Actual: 85.01 m.E1 = Var(initialize=27000.0, bounds=(25000, 40000)) # kJ/kmol Actual: 30000 m.E2 = Var(initialize=45000.0, bounds=(35000, 50000)) # kJ/kmol Actual: 40000 # m.E1.fix(30000) # m.E2.fix(40000) # # Time dependent variables # m.Ca = Var(m.time, initialize=m.Ca0, bounds=(0, 25)) m.Cb = Var(m.time, initialize=m.Cb0, bounds=(0, 25)) m.Cc = Var(m.time, initialize=m.Cc0, bounds=(0, 25)) m.Vr = Var(m.time, initialize=m.Vr0) m.Tr = Var(m.time, initialize=m.Tr0) m.Tj = Var( m.time, initialize=310.0, bounds=(288, None)) # Cooling jacket temp, follows coil temp until failure # # Derivatives in the model # m.dCa = DerivativeVar(m.Ca) m.dCb = DerivativeVar(m.Cb) m.dCc = DerivativeVar(m.Cc) m.dVr = DerivativeVar(m.Vr) m.dTr = DerivativeVar(m.Tr) # # Differential Equations in the model # def _dCacon(m, t): if t == 0: return Constraint.Skip return m.dCa[t] == m.Fa[t] / m.Vr[t] - m.k1 * exp( -m.E1 / (m.R * m.Tr[t])) * m.Ca[t] m.dCacon = Constraint(m.time, rule=_dCacon) def _dCbcon(m, t): if t == 0: return Constraint.Skip return m.dCb[t] == m.k1*exp(-m.E1/(m.R*m.Tr[t]))*m.Ca[t] - \ m.k2*exp(-m.E2/(m.R*m.Tr[t]))*m.Cb[t] m.dCbcon = Constraint(m.time, rule=_dCbcon) def _dCccon(m, t): if t == 0: return Constraint.Skip return m.dCc[t] == m.k2 * exp(-m.E2 / (m.R * m.Tr[t])) * m.Cb[t] m.dCccon = Constraint(m.time, rule=_dCccon) def _dVrcon(m, t): if t == 0: return Constraint.Skip return m.dVr[t] == m.Fa[t] * m.Mwa / m.rhor m.dVrcon = Constraint(m.time, rule=_dVrcon) def _dTrcon(m, t): if t == 0: return Constraint.Skip return m.rhor*m.cpr*m.dTr[t] == \ m.Fa[t]*m.Mwa*m.cpr/m.Vr[t]*(m.Tf-m.Tr[t]) - \ m.k1*exp(-m.E1/(m.R*m.Tr[t]))*m.Ca[t]*m.deltaH1 - \ m.k2*exp(-m.E2/(m.R*m.Tr[t]))*m.Cb[t]*m.deltaH2 + \ m.alphaj*m.Aj/m.Vr0*(m.Tj[t]-m.Tr[t]) + \ m.alphac*m.Ac/m.Vr0*(m.Tc[t]-m.Tr[t]) m.dTrcon = Constraint(m.time, rule=_dTrcon) def _singlecooling(m, t): return m.Tc[t] == m.Tj[t] m.singlecooling = Constraint(m.time, rule=_singlecooling) # Initial Conditions def _initcon(m): yield m.Ca[m.time.first()] == m.Ca0 yield m.Cb[m.time.first()] == m.Cb0 yield m.Cc[m.time.first()] == m.Cc0 yield m.Vr[m.time.first()] == m.Vr0 yield m.Tr[m.time.first()] == m.Tr0 m.initcon = ConstraintList(rule=_initcon) # # Stage-specific cost computations # def ComputeFirstStageCost_rule(model): return 0 m.FirstStageCost = Expression(rule=ComputeFirstStageCost_rule) def AllMeasurements(m): return sum( (m.Ca[t] - m.Ca_meas[t])**2 + (m.Cb[t] - m.Cb_meas[t])**2 + (m.Cc[t] - m.Cc_meas[t])**2 + 0.01 * (m.Tr[t] - m.Tr_meas[t])**2 for t in m.measT) def MissingMeasurements(m): if data['experiment'] == 1: return sum( (m.Ca[t] - m.Ca_meas[t])**2 + (m.Cb[t] - m.Cb_meas[t])**2 + (m.Cc[t] - m.Cc_meas[t])**2 + (m.Tr[t] - m.Tr_meas[t])**2 for t in m.measT) elif data['experiment'] == 2: return sum((m.Tr[t] - m.Tr_meas[t])**2 for t in m.measT) else: return sum( (m.Cb[t] - m.Cb_meas[t])**2 + (m.Tr[t] - m.Tr_meas[t])**2 for t in m.measT) m.SecondStageCost = Expression(rule=MissingMeasurements) def total_cost_rule(model): return model.FirstStageCost + model.SecondStageCost m.Total_Cost_Objective = Objective(rule=total_cost_rule, sense=minimize) # Discretize model disc = TransformationFactory('dae.collocation') disc.apply_to(m, nfe=20, ncp=4) return m
def test_module_example(self): from pyomo.environ import ConcreteModel, Var, Objective, units model = ConcreteModel() model.acc = Var() model.obj = Objective(expr=(model.acc*units.m/units.s**2 - 9.81*units.m/units.s**2)**2) self.assertEqual('m**2/s**4', str(units.get_units(model.obj.expr)))
def Model_Resolution(model, datapath="Example/data.dat"): ''' This function creates the model and call Pyomo to solve the instance of the proyect :param model: Pyomo model as defined in the Model_creation library :param datapath: path to the input data file :return: The solution inside an object call instance. ''' from Constraints_Thermal import Net_Present_Cost, Solar_Energy,State_of_Charge,\ Maximun_Charge, Minimun_Charge, Max_Power_Battery_Charge, Max_Power_Battery_Discharge, Max_Bat_in, Max_Bat_out, \ Financial_Cost, Energy_balance, Maximun_Lost_Load,Scenario_Net_Present_Cost, Scenario_Lost_Load_Cost, \ Initial_Inversion, Operation_Maintenance_Cost, Total_Finalcial_Cost, Battery_Reposition_Cost, Maximun_Diesel_Energy, Diesel_Comsuption,Diesel_Cost_Total, \ Solar_Thermal_Energy, State_Of_Charge_Tank, Maximun_Tank_Charge, Maximum_Boiler_Energy, \ NG_Consumption, Maximum_Resistance_Thermal_Energy, Total_Thermal_Energy_Demand, Thermal_Energy_Balance, Total_Electrical_Resistance_Demand, SC_Financial_Cost, \ Tank_Financial_Cost, Boiler_Financial_Cost , Resistance_Financial_Cost, NG_Cost_Total # OBJETIVE FUNTION: model.ObjectiveFuntion = Objective(rule=Net_Present_Cost, sense=minimize) # CONSTRAINTS #Energy constraints model.EnergyBalance = Constraint(model.scenario, model.periods, rule=Energy_balance) model.MaximunLostLoad = Constraint( model.scenario, rule=Maximun_Lost_Load) # Maximum permissible lost load model.ScenarioLostLoadCost = Constraint(model.scenario, rule=Scenario_Lost_Load_Cost) model.TotalThermalEnergyDemand = Constraint( model.scenario, model.classes, model.periods, rule=Total_Thermal_Energy_Demand) model.ThermalEnergyBalance = Constraint(model.scenario, model.classes, model.periods, rule=Thermal_Energy_Balance) model.TotalElectricalResistanceDemand = Constraint( model.scenario, model.periods, rule=Total_Electrical_Resistance_Demand) # Solar Collectors Constraints model.SolarThermalEnergy = Constraint(model.scenario, model.classes, model.periods, rule=Solar_Thermal_Energy) # PV constraints model.SolarEnergy = Constraint( model.scenario, model.periods, rule=Solar_Energy) # Energy output of the solar panels # Battery constraints model.StateOfCharge = Constraint( model.scenario, model.periods, rule=State_of_Charge) # State of Charge of the battery model.MaximunCharge = Constraint( model.scenario, model.periods, rule=Maximun_Charge) # Maximun state of charge of the Battery model.MinimunCharge = Constraint( model.scenario, model.periods, rule=Minimun_Charge) # Minimun state of charge model.MaxPowerBatteryCharge = Constraint( rule=Max_Power_Battery_Charge) # Max power battery charge constraint model.MaxPowerBatteryDischarge = Constraint( rule=Max_Power_Battery_Discharge ) # Max power battery discharge constraint model.MaxBatIn = Constraint( model.scenario, model.periods, rule=Max_Bat_in) # Minimun flow of energy for the charge fase model.Maxbatout = Constraint( model.scenario, model.periods, rule=Max_Bat_out) #minimun flow of energy for the discharge fase # Tank Constraints model.StateOfChargeTank = Constraint(model.scenario, model.classes, model.periods, rule=State_Of_Charge_Tank) model.MaximumTankCharge = Constraint(model.scenario, model.classes, model.periods, rule=Maximun_Tank_Charge) # Boiler Constraints model.MaximumBoilerEnergy = Constraint(model.scenario, model.classes, model.periods, rule=Maximum_Boiler_Energy) model.NGConsumption = Constraint(model.scenario, model.classes, model.periods, rule=NG_Consumption) model.NGCostTotal = Constraint(model.scenario, rule=NG_Cost_Total) # Electrical Resistance Constraint model.MaximumResistanceThermalEnergy = Constraint( model.scenario, model.classes, model.periods, rule=Maximum_Resistance_Thermal_Energy) # Diesel Generator constraints model.MaximunDieselEnergy = Constraint( model.scenario, model.periods, rule=Maximun_Diesel_Energy ) # Maximun energy output of the diesel generator model.DieselComsuption = Constraint( model.scenario, model.periods, rule=Diesel_Comsuption) # Diesel comsuption model.DieselCostTotal = Constraint(model.scenario, rule=Diesel_Cost_Total) # Financial Constraints model.SCFinancialCost = Constraint(rule=SC_Financial_Cost) model.TankFinancialCost = Constraint(rule=Tank_Financial_Cost) model.BoilerFinancialCost = Constraint(rule=Boiler_Financial_Cost) model.ResistanceFinancialCost = Constraint(rule=Resistance_Financial_Cost) model.FinancialCost = Constraint(rule=Financial_Cost) # Financial cost model.ScenarioNetPresentCost = Constraint(model.scenario, rule=Scenario_Net_Present_Cost) model.InitialInversion = Constraint(rule=Initial_Inversion) model.OperationMaintenanceCost = Constraint( rule=Operation_Maintenance_Cost) model.TotalFinalcialCost = Constraint(rule=Total_Finalcial_Cost) model.BatteryRepositionCost = Constraint(rule=Battery_Reposition_Cost) instance = model.create_instance(datapath) # load parameters opt = SolverFactory('cplex') # Solver use during the optimization results = opt.solve(instance, tee=True) # Solving a model instance instance.solutions.load_from(results) # Loading solution into instance return instance
def Model_Resolution_Dispatch(model, datapath="Example/data_Dispatch.dat"): ''' This function creates the model and call Pyomo to solve the instance of the proyect :param model: Pyomo model as defined in the Model_creation library :return: The solution inside an object call instance. ''' from Constraints_Dispatch import Net_Present_Cost, State_of_Charge, Maximun_Charge, \ Minimun_Charge, Max_Bat_in, Max_Bat_out, \ Energy_balance, Maximun_Lost_Load, Generator_Cost_1_Integer, \ Total_Cost_Generator_Integer, \ Scenario_Lost_Load_Cost, \ Generator_Bounds_Min_Integer, Generator_Bounds_Max_Integer,Energy_Genarator_Energy_Max_Integer # OBJETIVE FUNTION: model.ObjectiveFuntion = Objective(rule=Net_Present_Cost, sense=minimize) # CONSTRAINTS #Energy constraints model.EnergyBalance = Constraint(model.periods, rule=Energy_balance) # Energy balance model.MaximunLostLoad = Constraint( rule=Maximun_Lost_Load) # Maximum permissible lost load # Battery constraints model.StateOfCharge = Constraint( model.periods, rule=State_of_Charge) # State of Charge of the battery model.MaximunCharge = Constraint( model.periods, rule=Maximun_Charge) # Maximun state of charge of the Battery model.MinimunCharge = Constraint( model.periods, rule=Minimun_Charge) # Minimun state of charge model.MaxBatIn = Constraint( model.periods, rule=Max_Bat_in) # Minimun flow of energy for the charge fase model.Maxbatout = Constraint( model.periods, rule=Max_Bat_out) #minimun flow of energy for the discharge fase #Diesel Generator constraints model.GeneratorBoundsMin = Constraint(model.periods, rule=Generator_Bounds_Min_Integer) model.GeneratorBoundsMax = Constraint(model.periods, rule=Generator_Bounds_Max_Integer) model.GeneratorCost1 = Constraint(model.periods, rule=Generator_Cost_1_Integer) model.EnergyGenaratorEnergyMax = Constraint( model.periods, rule=Energy_Genarator_Energy_Max_Integer) model.TotalCostGenerator = Constraint(rule=Total_Cost_Generator_Integer) # Financial Constraints model.ScenarioLostLoadCost = Constraint(rule=Scenario_Lost_Load_Cost) instance = model.create_instance( "Example/data_dispatch.dat") # load parameters opt = SolverFactory('cplex') # Solver use during the optimization # opt.options['emphasis_memory'] = 'y' # opt.options['node_select'] = 3 results = opt.solve( instance, tee=True, options_string="mipgap=0.03") # Solving a model instance # instance.write(io_options={'emphasis_memory':True}) #options_string="mipgap=0.03", timelimit=1200 instance.solutions.load_from(results) # Loading solution into instance return instance
def Model_Resolution_binary(model, datapath="Example/data_binary.dat"): ''' This function creates the model and call Pyomo to solve the instance of the proyect :param model: Pyomo model as defined in the Model_creation library :return: The solution inside an object call instance. ''' from Constraints_binary import Net_Present_Cost, Solar_Energy, State_of_Charge, Maximun_Charge, \ Minimun_Charge, Max_Power_Battery_Charge, Max_Power_Battery_Discharge, Max_Bat_in, Max_Bat_out, \ Financial_Cost, Energy_balance, Maximun_Lost_Load, Generator_Cost_1_binary, Generator_Total_Period_Energy_binary, \ Total_Cost_Generator_binary, Initial_Inversion, Operation_Maintenance_Cost,Total_Finalcial_Cost,\ Battery_Reposition_Cost, Scenario_Lost_Load_Cost, Sceneario_Generator_Total_Cost, \ Scenario_Net_Present_Cost, Generator_Bounds_Min_binary, Generator_Bounds_Max_binary,Energy_Genarator_Energy_Max_binary # OBJETIVE FUNTION: model.ObjectiveFuntion = Objective(rule=Net_Present_Cost, sense=minimize) # CONSTRAINTS #Energy constraints model.EnergyBalance = Constraint(model.scenario, model.periods, rule=Energy_balance) # Energy balance model.MaximunLostLoad = Constraint( model.scenario, rule=Maximun_Lost_Load) # Maximum permissible lost load # PV constraints model.SolarEnergy = Constraint( model.scenario, model.periods, rule=Solar_Energy) # Energy output of the solar panels # Battery constraints model.StateOfCharge = Constraint( model.scenario, model.periods, rule=State_of_Charge) # State of Charge of the battery model.MaximunCharge = Constraint( model.scenario, model.periods, rule=Maximun_Charge) # Maximun state of charge of the Battery model.MinimunCharge = Constraint( model.scenario, model.periods, rule=Minimun_Charge) # Minimun state of charge model.MaxPowerBatteryCharge = Constraint( rule=Max_Power_Battery_Charge) # Max power battery charge constraint model.MaxPowerBatteryDischarge = Constraint( rule=Max_Power_Battery_Discharge ) # Max power battery discharge constraint model.MaxBatIn = Constraint( model.scenario, model.periods, rule=Max_Bat_in) # Minimun flow of energy for the charge fase model.Maxbatout = Constraint( model.scenario, model.periods, rule=Max_Bat_out) #minimun flow of energy for the discharge fase #Diesel Generator constraints model.GeneratorBoundsMin = Constraint(model.scenario, model.periods, rule=Generator_Bounds_Min_binary) model.GeneratorBoundsMax = Constraint(model.scenario, model.periods, rule=Generator_Bounds_Max_binary) model.GeneratorCost1 = Constraint(model.scenario, model.periods, rule=Generator_Cost_1_binary) model.EnergyGenaratorEnergyMax = Constraint( model.scenario, model.periods, rule=Energy_Genarator_Energy_Max_binary) model.TotalCostGenerator = Constraint(model.scenario, rule=Total_Cost_Generator_binary) model.GeneratorTotalPeriodEnergybinary = Constraint( model.scenario, model.periods, rule=Generator_Total_Period_Energy_binary) # Financial Constraints model.FinancialCost = Constraint(rule=Financial_Cost) # Financial cost model.InitialInversion = Constraint(rule=Initial_Inversion) model.OperationMaintenanceCost = Constraint( rule=Operation_Maintenance_Cost) model.TotalFinalcialCost = Constraint(rule=Total_Finalcial_Cost) model.BatteryRepositionCost = Constraint(rule=Battery_Reposition_Cost) model.ScenarioLostLoadCost = Constraint(model.scenario, rule=Scenario_Lost_Load_Cost) model.ScenearioGeneratorTotalCost = Constraint( model.scenario, rule=Sceneario_Generator_Total_Cost) model.ScenarioNetPresentCost = Constraint(model.scenario, rule=Scenario_Net_Present_Cost) instance = model.create_instance( "Example/data_binary.dat") # load parameters opt = SolverFactory('cplex') # Solver use during the optimization # opt.options['emphasis_memory'] = 'y' # opt.options['node_select'] = 3 results = opt.solve( instance, tee=True, options_string="mipgap=0.11") # Solving a model instance # instance.write(io_options={'emphasis_memory':True}) #options_string="mipgap=0.03", timelimit=1200 instance.solutions.load_from(results) # Loading solution into instance return instance
def get_master_problem(self, relax=True): """This function builds the master/original problem (master problem if relax=True, original proble if relax=False)""" # Create model model = AbstractModel() # Create sets # bars model.sBars = Set() # products model.sProducts = Set() # patterns model.sPatterns = Set() # Create subset # this subset represents feasible combinations of bars and patterns model.sBars_sPatterns = Set(dimen=2) # this subset represents feasible combinations of bars, patterns and products model.sBars_sPatterns_sProducts = Set(dimen=3) # Create parameters # length for each bar model.pBarLength = Param(model.sBars, mutable=True) # length for each product model.pProductLength = Param(model.sProducts, mutable=True) # demand for each product model.pProductDemand = Param(model.sProducts, mutable=True) # number of products per bar and pattern model.pNumberProductsPerBarPattern = Param( model.sBars_sPatterns_sProducts, mutable=True ) # Create variables # domain depending on the problem (master or original) if relax: vartype = NonNegativeReals else: vartype = NonNegativeIntegers # number of patterns required per bar type model.vNumPatternsUsedPerBar = Var( model.sBars_sPatterns, domain=vartype, initialize=0 ) # Create constraints def c01_demand_satisfaction(model, iProduct): """demand satisfaction for each product""" return ( sum( model.pNumberProductsPerBarPattern[iBar, iPattern, iProduct] * model.vNumPatternsUsedPerBar[iBar, iPattern] for iBar in model.sBars for iPattern in model.sPatterns if (iBar, iPattern, iProduct) in model.sBars_sPatterns_sProducts ) >= model.pProductDemand[iProduct] ) # Create objective function def obj_expression(model): """minimum total loss of material""" return sum( model.pBarLength[iBar] * model.vNumPatternsUsedPerBar[iBar, iPattern] for iBar in model.sBars for iPattern in model.sPatterns if (iBar, iPattern) in model.sBars_sPatterns ) - sum( model.pProductLength[iProduct] * model.pProductDemand[iProduct] for iProduct in model.sProducts ) # Active constraints model.c01_demand_satisfaction = Constraint( model.sProducts, rule=c01_demand_satisfaction ) # Add objective function model.f_obj = Objective(rule=obj_expression, sense=minimize) return model
def build(number_of_stages=2): # ---building model--- m = ConcreteModel() m.fs = FlowsheetBlock(default={"dynamic": False}) m.fs.properties = props.NaClParameterBlock() m.fs.costing = WaterTAPCosting() m.fs.NumberOfStages = Param(initialize=number_of_stages) m.fs.StageSet = RangeSet(m.fs.NumberOfStages) m.fs.LSRRO_StageSet = RangeSet(2, m.fs.NumberOfStages) m.fs.NonFinal_StageSet = RangeSet(m.fs.NumberOfStages - 1) m.fs.feed = Feed(default={"property_package": m.fs.properties}) m.fs.product = Product(default={"property_package": m.fs.properties}) m.fs.disposal = Product(default={"property_package": m.fs.properties}) # Add the mixers m.fs.Mixers = Mixer( m.fs.NonFinal_StageSet, default={ "property_package": m.fs.properties, "momentum_mixing_type": MomentumMixingType.equality, # booster pump will match pressure "inlet_list": ["upstream", "downstream"], }, ) total_pump_work = 0 # Add the pumps m.fs.PrimaryPumps = Pump(m.fs.StageSet, default={"property_package": m.fs.properties}) for pump in m.fs.PrimaryPumps.values(): pump.costing = UnitModelCostingBlock( default={"flowsheet_costing_block": m.fs.costing}) m.fs.costing.cost_flow( pyunits.convert(pump.work_mechanical[0], to_units=pyunits.kW), "electricity") # Add the equalizer pumps m.fs.BoosterPumps = Pump(m.fs.LSRRO_StageSet, default={"property_package": m.fs.properties}) for pump in m.fs.BoosterPumps.values(): pump.costing = UnitModelCostingBlock( default={"flowsheet_costing_block": m.fs.costing}) m.fs.costing.cost_flow( pyunits.convert(pump.work_mechanical[0], to_units=pyunits.kW), "electricity") # Add the stages ROs m.fs.ROUnits = ReverseOsmosis0D( m.fs.StageSet, default={ "property_package": m.fs.properties, "has_pressure_change": True, "pressure_change_type": PressureChangeType.calculated, "mass_transfer_coefficient": MassTransferCoefficient.calculated, "concentration_polarization_type": ConcentrationPolarizationType.calculated, }, ) for ro_unit in m.fs.ROUnits.values(): ro_unit.costing = UnitModelCostingBlock( default={"flowsheet_costing_block": m.fs.costing}) # Add EnergyRecoveryDevice m.fs.EnergyRecoveryDevice = EnergyRecoveryDevice( default={"property_package": m.fs.properties}) m.fs.EnergyRecoveryDevice.costing = UnitModelCostingBlock( default={ "flowsheet_costing_block": m.fs.costing, }) m.fs.costing.cost_flow( pyunits.convert(m.fs.EnergyRecoveryDevice.work_mechanical[0], to_units=pyunits.kW), "electricity", ) # additional variables or expressions # system water recovery m.fs.water_recovery = Var( initialize=0.5, bounds=(0, 1), domain=NonNegativeReals, units=pyunits.dimensionless, doc="System Water Recovery", ) m.fs.eq_water_recovery = Constraint( expr=sum(m.fs.feed.flow_mass_phase_comp[0, "Liq", :]) * m.fs.water_recovery == sum(m.fs.product.flow_mass_phase_comp[ 0, "Liq", :])) # costing m.fs.costing.cost_process() product_flow_vol_total = m.fs.product.properties[0].flow_vol m.fs.costing.add_annual_water_production(product_flow_vol_total) m.fs.costing.add_LCOW(product_flow_vol_total) m.fs.costing.add_specific_energy_consumption(product_flow_vol_total) # objective m.fs.objective = Objective(expr=m.fs.costing.LCOW) # connections # Connect the feed to the first pump m.fs.feed_to_pump = Arc(source=m.fs.feed.outlet, destination=m.fs.PrimaryPumps[1].inlet) # Connect the primary RO permeate to the product m.fs.primary_RO_to_product = Arc(source=m.fs.ROUnits[1].permeate, destination=m.fs.product.inlet) # Connect the Pump n to the Mixer n m.fs.pump_to_mixer = Arc( m.fs.NonFinal_StageSet, rule=lambda fs, n: { "source": fs.PrimaryPumps[n].outlet, "destination": fs.Mixers[n].upstream, }, ) # Connect the Mixer n to the Stage n m.fs.mixer_to_stage = Arc( m.fs.NonFinal_StageSet, rule=lambda fs, n: { "source": fs.Mixers[n].outlet, "destination": fs.ROUnits[n].inlet, }, ) # Connect the Stage n to the Pump n+1 m.fs.stage_to_pump = Arc( m.fs.NonFinal_StageSet, rule=lambda fs, n: { "source": fs.ROUnits[n].retentate, "destination": fs.PrimaryPumps[n + 1].inlet, }, ) # Connect the Stage n to the Eq Pump n m.fs.stage_to_eq_pump = Arc( m.fs.LSRRO_StageSet, rule=lambda fs, n: { "source": fs.ROUnits[n].permeate, "destination": fs.BoosterPumps[n].inlet, }, ) # Connect the Eq Pump n to the Mixer n-1 m.fs.eq_pump_to_mixer = Arc( m.fs.LSRRO_StageSet, rule=lambda fs, n: { "source": fs.BoosterPumps[n].outlet, "destination": fs.Mixers[n - 1].downstream, }, ) # Connect the Pump N to the Stage N last_stage = m.fs.StageSet.last() m.fs.pump_to_stage = Arc( source=m.fs.PrimaryPumps[last_stage].outlet, destination=m.fs.ROUnits[last_stage].inlet, ) # Connect Final Stage to EnergyRecoveryDevice Pump m.fs.stage_to_erd = Arc( source=m.fs.ROUnits[last_stage].retentate, destination=m.fs.EnergyRecoveryDevice.inlet, ) # Connect the EnergyRecoveryDevice to the disposal m.fs.erd_to_disposal = Arc(source=m.fs.EnergyRecoveryDevice.outlet, destination=m.fs.disposal.inlet) # additional bounding for b in m.component_data_objects(Block, descend_into=True): # NaCl solubility limit if hasattr(b, "mass_frac_phase_comp"): b.mass_frac_phase_comp["Liq", "NaCl"].setub(0.26) TransformationFactory("network.expand_arcs").apply_to(m) return m
def test_solve1(self): model = ConcreteModel() model.A = RangeSet(1,4) model.x = Var(model.A, bounds=(-1,1)) def obj_rule(model): return sum_product(model.x) model.obj = Objective(rule=obj_rule) def c_rule(model): expr = 0 for i in model.A: expr += i*model.x[i] return expr == 0 model.c = Constraint(rule=c_rule) opt = SolverFactory('glpk') results = opt.solve(model, symbolic_solver_labels=True) model.solutions.store_to(results) results.write(filename=join(currdir,"solve1.out"), format='json') with open(join(currdir,"solve1.out"), 'r') as out, \ open(join(currdir,"solve1.txt"), 'r') as txt: self.assertStructuredAlmostEqual(json.load(txt), json.load(out), abstol=1e-4, allow_second_superset=True) # def d_rule(model): return model.x[1] >= 0 model.d = Constraint(rule=d_rule) model.d.deactivate() results = opt.solve(model) model.solutions.store_to(results) results.write(filename=join(currdir,"solve1x.out"), format='json') with open(join(currdir,"solve1x.out"), 'r') as out, \ open(join(currdir,"solve1.txt"), 'r') as txt: self.assertStructuredAlmostEqual(json.load(txt), json.load(out), abstol=1e-4, allow_second_superset=True) # model.d.activate() results = opt.solve(model) model.solutions.store_to(results) results.write(filename=join(currdir,"solve1a.out"), format='json') with open(join(currdir,"solve1a.out"), 'r') as out, \ open(join(currdir,"solve1a.txt"), 'r') as txt: self.assertStructuredAlmostEqual(json.load(txt), json.load(out), abstol=1e-4, allow_second_superset=True) # model.d.deactivate() def e_rule(model, i): return model.x[i] >= 0 model.e = Constraint(model.A, rule=e_rule) for i in model.A: model.e[i].deactivate() results = opt.solve(model) model.solutions.store_to(results) results.write(filename=join(currdir,"solve1y.out"), format='json') with open(join(currdir,"solve1y.out"), 'r') as out, \ open(join(currdir,"solve1.txt"), 'r') as txt: self.assertStructuredAlmostEqual(json.load(txt), json.load(out), abstol=1e-4, allow_second_superset=True) # model.e.activate() results = opt.solve(model) model.solutions.store_to(results) results.write(filename=join(currdir,"solve1b.out"), format='json') with open(join(currdir,"solve1b.out"), 'r') as out, \ open(join(currdir,"solve1b.txt"), 'r') as txt: self.assertStructuredAlmostEqual(json.load(txt), json.load(out), abstol=1e-4, allow_second_superset=True)
m.init_fas_cap_ns = Constraint(T[0:1], NS, rule=init_fas_cap_ns_rule) # # Objective function # def obj_rule(m): """ Returns the minimized total system costs. [10^9 €] """ return sum(sum(m.CF[t, s] + m.CI[t, s] + m.CS[t, s] for t in T) for s in S) m.system_costs = Objective(sense=minimize, rule=obj_rule) # # Subject to # def cf_rule(m, t, s): """ Returns the total fuel costs of all ships in each timestep. [10^9 €] """ return (m.CF[t, s] >= m.fa[t, s] * cf[t, s]) m.costs_fuel = Constraint(T, S, rule=cf_rule)
def __init__(self, z_ideal, z_nadir, z_ref, data, scalarization='ASF', weights=None, nvar=None, eps=0.01, roo=0.01, sense='maximize', frees=[]): if len(z_ideal) != len(z_nadir) or len(z_ideal) != len(z_ref): print("Length of given vectors don't match") return super().__init__(data, weights, nvar, False) model = self.model # Initialize ASF parameters model.k = Param(within=NonNegativeIntegers, initialize=len(z_ideal)) model.H = RangeSet(0, model.k - 1) model.frees = Set(within=NonNegativeIntegers, initialize=frees) def init_ideal(model, h): return z_ideal[h] def init_nadir(model, h): return z_nadir[h] - eps def init_utopia(model, h): return z_ideal[h] + eps def init_ref(model, h): return z_ref[h] def init_free(model, h): if h in model.frees: return init_utopia(model, h) else: return init_ref(model, h) model.roo = roo scalarization = scalarization.upper() if sense == 'minimize': if scalarization == 'GUESS': model.z1 = Param(model.H, initialize=init_nadir) model.z2 = Param(model.H, initialize=init_nadir) model.z3 = Param(model.H, initialize=init_ref) model.z4 = Param(model.H, initialize=init_nadir) if model.frees: model.z5 = Param(model.H, initialize=init_free) else: model.z5 = Param(model.H, initialize=init_ref) elif scalarization == 'STOM': model.z1 = Param(model.H, initialize=init_utopia) model.z2 = Param(model.H, initialize=init_ref) model.z3 = Param(model.H, initialize=init_utopia) model.z4 = Param(model.H, initialize=init_ref) model.z5 = Param(model.H, initialize=init_utopia) else: # scalarization == 'ASF' model.z1 = Param(model.H, initialize=init_ref) model.z2 = Param(model.H, initialize=init_nadir) model.z3 = Param(model.H, initialize=init_utopia) model.z4 = Param(model.H, initialize=init_nadir) model.z5 = Param(model.H, initialize=init_utopia) else: # sense == 'maximize' if scalarization == 'GUESS': model.z1 = Param(model.H, initialize=init_nadir) model.z2 = Param(model.H, initialize=init_nadir) model.z3 = Param(model.H, initialize=init_ref) if model.frees: model.z4 = Param(model.H, initialize=init_free) else: model.z4 = Param(model.H, initialize=init_ref) model.z5 = Param(model.H, initialize=init_nadir) elif scalarization == 'STOM': model.z1 = Param(model.H, initialize=init_utopia) model.z2 = Param(model.H, initialize=init_ref) model.z3 = Param(model.H, initialize=init_utopia) model.z4 = Param(model.H, initialize=init_utopia) model.z5 = Param(model.H, initialize=init_ref) else: # scalarization == 'ASF' model.z1 = Param(model.H, initialize=init_ref) model.z2 = Param(model.H, initialize=init_nadir) model.z3 = Param(model.H, initialize=init_utopia) model.z4 = Param(model.H, initialize=init_utopia) model.z5 = Param(model.H, initialize=init_nadir) model.maximum = Var() def minmaxconst(model, h): ''' Constraint: The new "maximum" variable, that will be minimized in the optimization, must be greater than any of the original divisions used in original problem formulation.''' if h in model.frees: return model.maximum >= -np.inf else: return model.maximum >= \ np.divide(np.subtract(self.obj_fun(model, data)[h], model.z1[h]), model.z2[h] - model.z3[h]) model.ConstraintMax = Constraint(model.H, rule=minmaxconst) def asf_fun(model): return model.maximum \ + model.roo*sum([np.divide(self.obj_fun(model, data)[h], model.z4[h] - model.z5[h]) for h in model.H]) if hasattr(model, 'OBJ'): del model.OBJ # Delete previous Objective to suppress warnings model.OBJ = Objective(rule=asf_fun, sense=minimize) self.model = model self._modelled = True
def test_keys_empty(self): """Test keys method""" model = ConcreteModel() model.o = Objective() self.assertEqual(list(model.o.keys()), [])
def _solve_delta_given_sigma(var_est_object, solver, **kwds): """Solves the delta with provided variances :param VarianceEstimator var_est_object: The variance estimation object :param str solver: The solver being used (currently not used) :param dict kwds: The dict of user settings passed on from the ReactionModel :return results: The results from the variance estimation :rtype: ResultsObject """ solver_opts = kwds.pop('solver_opts', dict()) tee = kwds.pop('tee', False) set_A = kwds.pop('subset_lambdas', list()) profile_time = kwds.pop('profile_time', False) sigmas = kwds.pop('init_sigmas', dict() or float()) sigmas_sq = dict() if not set_A: set_A = var_est_object.model.meas_lambdas if isinstance(sigmas, float): for k in var_est_object.comps['unknown_absorbance']: sigmas_sq[k] = sigmas elif isinstance(sigmas, dict): keys = sigmas.keys() for k in var_est_object.comps['unknown_absorbance']: if k not in keys: sigmas_sq[k] = sigmas print("Solving delta from given sigmas\n") print(sigmas_sq) var_est_object._warn_if_D_negative() obj = 0.0 ntp = len(var_est_object.model.times_spectral) nwp = len(var_est_object.model.meas_lambdas) inlog = 0 nc = len(var_est_object.comps['unknown_absorbance']) for t in var_est_object.model.times_spectral: for l in set_A: D_bar = sum(var_est_object.model.C[t, k] * var_est_object.model.S[l, k] for k in var_est_object.comps['unknown_absorbance']) #D_bar = sum(var_est_object.model.C[t, k] * var_est_object.model.S[l, k] for k in var_est_object._sublist_components) inlog += (var_est_object.model.D[t, l] - D_bar)**2 # Orig had sublist in both parts - is this an error? # Concentration - correct # Move this to objectives module for t in var_est_object.model.times_spectral: for k in var_est_object.comps['unknown_absorbance']: #obj += conc_objective(var_est_object.model, sigma=sigmas_sq) obj += 0.5 * ((var_est_object.model.C[t, k] - var_est_object.model.Z[t, k])**2) / (sigmas_sq[k]) obj += (nwp) * log((inlog) + 1e-12) var_est_object.model.init_objective = Objective(expr=obj) opt = SolverFactory(solver) for key, val in solver_opts.items(): opt.options[key] = val solver_results = opt.solve(var_est_object.model, tee=tee, report_timing=profile_time) residuals = (value(var_est_object.model.init_objective)) print("residuals: ", residuals) for k, v in var_est_object.model.P.items(): print(k, v.value) etaTeta = 0 for t in var_est_object.model.times_spectral: for l in set_A: D_bar = sum( value(var_est_object.model.C[t, k]) * value(var_est_object.model.S[l, k]) for k in var_est_object.comps['unknown_absorbance']) etaTeta += (value(var_est_object.model.D[t, l]) - D_bar)**2 deltasq = etaTeta / (ntp * nwp) var_est_object.model.del_component('init_objective') return deltasq
def test_len_empty(self): """Test len method""" model = ConcreteModel() model.o = Objective() self.assertEqual(len(model.o), 0)
def TSP(G): n = G.number_of_nodes() m = ConcreteModel() m.N = RangeSet(n) m.A = Set(initialize=((i, j) for i, j in G.edges()), dimen=2) m.x = Var(m.A, domain=NonNegativeReals, bounds=lambda m: (0, 1)) m.obj = Objective(expr=sum(G[i][j]['weight'] * m.x[i, j] for i, j in G.edges())) m.outdegree = ConstraintList() for i in m.N: m.outdegree.add(sum(m.x[v, w] for v, w in G.out_edges(i)) == 1) m.indegree = ConstraintList() for i in m.N: m.indegree.add(sum(m.x[v, w] for v, w in G.in_edges(i)) == 1) m.noarcs = ConstraintList() for i, j in m.A: if i < j: m.noarcs.add(m.x[i, j] + m.x[j, i] <= 1) m.subtour = ConstraintList() solver = SolverFactory('gurobi') it = 0 Cold = [] while it <= 10: it += 1 sol = solver.solve(m, tee=False, load_solutions=False) sol_json = sol.json_repn() if sol_json['Solver'][0]['Status'] != 'ok': return None, [] m.solutions.load_from(sol) print(it, 'LP:', m.obj()) selected = [] values = [] for i, j in m.A: if i < j: if m.x[i, j]() > 0.0001 or m.x[j, i]() > 0.0001: selected.append((i - 1, j - 1)) values.append(m.x[i, j]() + m.x[j, i]()) PlotTour(Ls, selected, values) # sleep(1) # Costruiamo un grafo supporto H = nx.Graph() for i, j in m.A: if i < j: if m.x[i, j]() > 0.001 or m.x[j, i]() > 0.001: H.add_edge(i, j) Cs = nx.cycle_basis(H) if Cs == Cold: break Cold = Cs for cycle in Cs: Es = [] for i in cycle: for j in H.nodes(): if j not in cycle: Es.append((i, j)) if len(Es) > 0: m.subtour.add(sum(m.x[i, j] for i, j in Es) >= 1) return m.obj(), selected
def test_dim(self): """Test dim method""" model = self.create_model() model.obj = Objective(model.A, model.A) self.assertEqual(model.obj.dim(), 2)
def __init__(self, y, x, z, Cutactive, cet='addi', fun='prod', rts='vrs'): """ y : Output x : Input z : Contexutal variable cet = "addi" : Additive composite error term = "mult" : Multiplicative composite error term fun = "prod" : Production frontier = "cost" : Cost frontier rts = "vrs" : Variable returns to scale = "crs" : Constant returns to scale """ # TODO(error/warning handling): Check the configuration of the model exist self.x = x self.y = y self.z = z self.cet = cet self.fun = fun self.rts = rts if type(self.x[0]) != list: self.x = [] for x_value in x.tolist(): self.x.append([x_value]) if type(self.z[0]) != list: self.z = [] for z_value in z.tolist(): self.z.append([z_value]) self.Cutactive = Cutactive # Initialize the CNLS model self.__model__ = ConcreteModel() # Initialize the sets self.__model__.I = Set(initialize=range(len(self.y))) self.__model__.J = Set(initialize=range(len(self.x[0]))) self.__model__.K = Set(initialize=range(len(self.z[0]))) # Initialize the variables self.__model__.alpha = Var(self.__model__.I, doc='alpha') self.__model__.beta = Var(self.__model__.I, self.__model__.J, bounds=(0.0, None), doc='beta') self.__model__.lamda = Var(self.__model__.K, doc='Zvalue') self.__model__.epsilon = Var(self.__model__.I, doc='residual') self.__model__.frontier = Var(self.__model__.I, bounds=(0.0, None), doc='estimated frontier') # Setup the objective function and constraints self.__model__.objective = Objective(rule=self.__objective_rule(), sense=minimize, doc='objective function') self.__model__.regression_rule = Constraint( self.__model__.I, rule=self.__regression_rule(), doc='regression equation') if self.cet == "mult": self.__model__.log_rule = Constraint( self.__model__.I, rule=self.__log_rule(), doc='log-transformed regression equation') self.__model__.afriat_rule = Constraint( self.__model__.I, rule=self.__afriat_rule(), doc='elementary Afriat approach') self.__model__.sweet_rule = Constraint(self.__model__.I, self.__model__.I, rule=self.__sweet_rule(), doc='sweet spot approach') # Optimize model self.optimization_status = 0 self.problem_status = 0
def test_abstract_index(self): model = AbstractModel() model.A = Set() model.B = Set() model.C = model.A | model.B model.x = Objective(model.C)
return model.P[i, j] + model.P[h, k] <= 1 model.no_cross_pair = ConstraintList() for i in range(1, n + 1): for j in range(i, n + 1): for h in range(1, i): for k in range(i + 1, j): model.no_cross_pair.add(no_cross_rule(model, i, j, h, k)) model.no_cross_pair.add(no_cross_rule(model, j, i, h, k)) model.no_cross_pair.add(no_cross_rule(model, i, j, k, h)) model.no_cross_pair.add(no_cross_rule(model, j, i, k, h)) #obj function model.obj = Objective(expr=sum(model.P[i, j] for i in model.I for j in model.J), sense=maximize) sol = SolverFactory('glpk').solve(model) sol_json = sol.json_repn() if sol_json['Solver'][0]['Status'] != 'ok': print("Problem unsolved") if sol_json['Solver'][0]['Termination condition'] != 'optimal': print("Problem unsolved") for i in range(1, n + 1): for j in range(1, n + 1): if model.P[i, j] == 1: print(i, j)
def optimize_dist(network, R, B, num_balls, goal='min', print_res=False): N = len(network) goal = minimize if goal == 'min' else maximize # Define exposure function def exposure(model): exp = 0 for node in network: # For each node r_sum, b_sum = 0, 0 for i, rel in enumerate(node): # Sum all neighbours if rel != 0: # Only sum if there is a connection r_sum += model.Fixed[i] b_sum += model.Variable[i] if (r_sum + b_sum) != 0: # Only add to sum if non-zero denominator. exp += r_sum / (r_sum + b_sum) else: exp += .5 return exp # Constraint function for problem def ball_constraint(model): return summation(model.Variable) == num_balls # Function to initialize black values def black_init(_, i): return B[i] # Function to initialize red values def red_init(_, i): return R[i] # Initialize values model = ConcreteModel() model.F = RangeSet(0, N - 1) model.V = RangeSet(0, N - 1) # Initialize fixed balls model.Fixed = Param(model.F, within=NonNegativeIntegers, default=red_init) # Initialize variable balls model.Variable = Var(model.V, domain=NonNegativeIntegers, bounds=(0, num_balls), initialize=black_init) # Define objective function model.exposure = Objective(rule=exposure, sense=goal) # Define constraint function model.constraint = Constraint(rule=ball_constraint) # Initialize (ipopt) solver # solver = SolverFactory('ipopt', executable=ipopt_path) solver = SolverFactory( 'bonmin', executable='~/.bonmin/Bonmin-1.8.7/build/bin/bonmin') # Solve model solver.solve(model) # Print resulting distribution optimal = [0] * N if print_res: print('Variable_Fixed') for i in range(N): optimal[i] = model.Variable[i]() # round() if print_res: print(str(i) + ': ' + str(optimal[i]) + '_' + str(model.Fixed[i])) if print_res: print('Final exposure: ' + str(round(model.exposure() * 1000) / 1000)) return optimal
def test_pickle_abstract_model_constant_objective(self): model = AbstractModel() model.A = Objective(expr=1) str = pickle.dumps(model) tmodel = pickle.loads(str) self.verifyModel(model, tmodel)
def _reformulate(self, c, param, uncset, counterpart, root=False): """ Reformulate an uncertain constraint or objective c: Constraint or Objective param: UncParam uncset: UncSet counterpart: Block """ # Check constraint/objective repn = self.generate_repn_param(c) assert repn.is_linear(), ( "Constraint {} should be linear in " "unc. parameters".format(c.name)) # Generate robust counterpart det = quicksum(x[0]*x[1].nominal for x in zip(repn.linear_coefs, repn.linear_vars)) det += repn.constant param_var_dict = {id(param): var for param, var in zip(repn.linear_vars, repn.linear_coefs)} # padding = sqrt( var^T * cov^-1 * var ) padding = quicksum(param_var_dict[id(param[ind_i])] * uncset.cov[i][j] * param_var_dict[id(param[ind_j])] for i, ind_i in enumerate(param) for j, ind_j in enumerate(param)) if c.ctype is Constraint: # For upper bound: det + padding <= b if c.has_ub(): counterpart.upper = Block() if root: expr = det + sqrt(padding) <= c.upper robust = Constraint(expr=expr) else: counterpart.upper.padding = Var(bounds=(0, float('inf'))) pvar = counterpart.upper.padding robust = Constraint(expr=det + pvar <= c.upper()) deterministic = Constraint(expr=padding <= pvar**2) counterpart.upper.det = deterministic counterpart.upper.rob = robust # For lower bound: det - padding >= b if c.has_lb(): counterpart.lower = Block() if root: expr = det - sqrt(padding) >= c.lower robust = Constraint(expr=expr) else: counterpart.lower.padding = Var(bounds=(0, float('inf'))) pvar = counterpart.lower.padding robust = Constraint(expr=c.lower() <= det - pvar) deterministic = Constraint(expr=padding <= pvar**2) counterpart.lower.det = deterministic counterpart.lower.rob = robust else: # For minimization: min det + padding # For maximization: max det - padding sense = c.sense if root: expr = det + c.sense*sqrt(padding) robust = Objective(expr=expr, sense=sense) else: counterpart.padding = Var(bounds=(0, float('inf'))) pvar = counterpart.padding robust = Objective(expr=det + sense*pvar, sense=sense) deterministic = Constraint(expr=padding <= pvar**2) counterpart.det = deterministic counterpart.rob = robust