How to use the pyomo.environ.Objective function in Pyomo

To help you get started, we’ve selected a few Pyomo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Pyomo / pyomo / pyomo / contrib / multistart / test_multi.py View on Github external
def test_var_value_None(self):
        m = ConcreteModel()
        m.x = Var(bounds=(0, 1))
        m.obj = Objective(expr=m.x)
        SolverFactory('multistart').solve(m)
github Pyomo / pyomo / pyomo / contrib / pynumero / examples / feasibility.py View on Github external
def create_basic_model():

    m = aml.ConcreteModel()
    m.x = aml.Var([1, 2, 3], domain=aml.Reals)
    for i in range(1, 4):
        m.x[i].value = i
    m.c1 = aml.Constraint(expr=m.x[1] ** 2 - m.x[2] - 1 == 0)
    m.c2 = aml.Constraint(expr=m.x[1] - m.x[3] - 0.5 == 0)
    m.d1 = aml.Constraint(expr=m.x[1] + m.x[2] <= 100.0)
    m.d2 = aml.Constraint(expr=m.x[2] + m.x[3] >= -100.0)
    m.d3 = aml.Constraint(expr=m.x[2] + m.x[3] + m.x[1] >= -500.0)
    m.x[2].setlb(0.0)
    m.x[3].setlb(0.0)
    m.x[2].setub(100.0)
    m.obj = aml.Objective(expr=m.x[2]**2)
    return m
github sandialabs / chama / chama / design.py View on Github external
scenario_sensors[a] = set()
            scenario_sensors[a].add(i)

        # create the model container
        model = pe.ConcreteModel()

        # x_{a,i} variable indicates which sensor is the first to detect scenario a
        model.x = pe.Var(scenario_sensor_pairs, bounds=(0,1))

        # y_i variable indicates if a sensor is installed or not
        model.y = pe.Var(sensor_set, within=pe.Binary)

        # objective function minimize the sum impact across all scenarios
        def obj_rule(m):
            return 1.0/float(len(scenario_set))*sum(float(impact.loc[a,i])*m.x[a,i] for (a,i) in scenario_sensor_pairs)
        model.obj = pe.Objective(rule=obj_rule)

        # constrain the problem to have only one x value for each scenario
        def limit_x_rule(m, a):
            return sum(m.x[a,i] for i in scenario_sensors[a]) == 1
        model.limit_x = pe.Constraint(scenario_set, rule=limit_x_rule)

        def detect_only_if_sensor_rule(m, a, i):
            return m.x[a,i] <= model.y[i]
        model.detect_only_if_sensor = pe.Constraint(scenario_sensor_pairs, rule=detect_only_if_sensor_rule)

        model.sensor_budget = pe.Constraint(expr=sum(sensor_dict[i].cost*model.y[i] for i in sensor_set if i != -1 and i != '_NotDetected') <= sensor_budget)

        return model
github Pyomo / pyomo / examples / pysp / networkx_scenariotree / ReferenceModel.py View on Github external
def pysp_instance_creation_callback(scenario_name, node_names):

    model = aml.ConcreteModel()
    model.x = aml.Var()
    model.z = aml.Var()
    model.FirstStageCost = aml.Expression(
        expr=5*(model.z**2 + (model.x-1.1)**2))
    model.SecondStageCost = aml.Expression(expr=0.0)
    model.ThirdStageCost = aml.Expression(expr=0.0)
    model.obj = aml.Objective(expr= model.FirstStageCost + \
                                    model.SecondStageCost + \
                                    model.ThirdStageCost)
    model.c = aml.ConstraintList()
    model.c.add(model.z == model.x)
    if scenario_name.startswith("u0"):
        # All scenarios under second-stage node "u0"
        model.xu0 = aml.Var()
        model.c.add(model.xu0 == model.x)
        model.SecondStageCost.expr = (model.xu0 - 1)**2

        model.y0 = aml.Var()
        model.c.add(expr= -10 <= model.y0 <= 10)
        if scenario_name == "u00":
            model.yu00 = aml.Var()
            model.c.add(model.yu00 == model.y0)
            model.ThirdStageCost.expr = (model.yu00 + 1)**2
github IDAES / idaes-pse / idaes / examples / tutorials / Tutorial_2_Basic_Flowsheet_Optimization.py View on Github external
# Initialize Units
    m.fs.Tank1.initialize()
    m.fs.Tank2.initialize(state_args={
            "flow_vol": 1.0,
            "conc_mol_comp": {"H2O": 55388.0,
                              "NaOH": 100.0,
                              "EthylAcetate": 100.0,
                              "SodiumAcetate": 0.0,
                              "Ethanol": 0.0},
            "temperature": 303.15,
            "pressure": 101325.0})

    # =========================================================================
    # Adding Optimization
    m.fs.obj = Objective(
            expr=m.fs.Tank2.outlet.conc_mol_comp[0, "SodiumAcetate"],
            sense=maximize)

    # Add additional constraint
    m.fs.volume_constraint = Constraint(
            expr=m.fs.Tank1.volume[0] + m.fs.Tank2.volume[0] == 3.0)

    # Set bounds for variables
    m.fs.Tank1.volume[0].setlb(0.5)
    m.fs.Tank1.volume[0].setub(5.0)
    m.fs.Tank2.volume[0].setlb(0.5)
    m.fs.Tank2.volume[0].setub(5.0)

    # Unfix heat inputs
    m.fs.Tank1.volume.unfix()
    m.fs.Tank2.volume.unfix()
github FRED-2 / OptiType / model.py View on Github external
model.t_allele = Param(initialize=self.__t_max_allele, mutable=True)

        model.beta = Param(initialize=self.__beta,
                           validate=lambda val, model: 0.0 <= float(self.__beta) <= 0.999,
                           mutable=True)
        model.nof_loci = Param(initialize=len(loci))

        # init variables
        model.x = Var(model.L, domain=Binary)
        model.y = Var(model.R, domain=Binary)

        model.re = Var(model.R, bounds=(0.0, None))
        model.hetero = Var(bounds=(0.0, model.nof_loci))

        # init objective
        model.read_cov = Objective(
            rule=lambda model: sum(model.occ[r] * (model.y[r] - model.beta * (model.re[r])) for r in model.R) - sum(
                model.reconst[a] * model.x[a] for a in model.L), sense=maximize)

        # init Constraints
        model.max_allel_selection = Constraint(model.LociNames, rule=lambda model, l: sum(
            model.x[a] for a in model.Loci[l]) <= model.t_allele)
        model.min_allel_selection = Constraint(model.LociNames,
                                               rule=lambda model, l: sum(model.x[a] for a in model.Loci[l]) >= 1)
        model.is_read_cov = Constraint(model.R,
                                       rule=lambda model, r: sum(model.cov[r, a] * model.x[a] for a in model.L) >=
                                                             model.y[r])
        model.heterozygot_count = Constraint(
            rule=lambda model: model.hetero >= sum(model.x[a] for a in model.L) - model.nof_loci)

        # regularization constraints
        model.reg1 = Constraint(model.R, rule=lambda model, r: model.re[r] <= model.nof_loci * model.y[r])
github grid-parity-exchange / Egret / egret / models / acopf.py View on Github external
)

    ### declare the generator cost objective
    libgen.declare_expression_pgqg_operating_cost(model=model,
                                                  index_set=gen_attrs['names'],
                                                  p_costs=gen_attrs['p_cost'],
                                                  q_costs=gen_attrs.get('q_cost', None)
                                                  )

    obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost)
    if include_feasibility_slack:
        obj_expr += penalty_expr
    if hasattr(model, 'qg_operating_cost'):
        obj_expr += sum(model.qg_operating_cost[gen_name] for gen_name in model.qg_operating_cost)

    model.obj = pe.Objective(expr=obj_expr)

    return model, md
github FZJ-IEK3-VSA / FINE / FINE / expansionModules / robustPipelineSizing.py View on Github external
dic_arc_coef = {}
    # determine coefficients for objective function
    # if for an arc (u,v), u, respectively v, are not in pathNodes, then the coefficient is 0
    # if arc (u,v) of pathNodes satisfies P(startNode, u) subset P(startNode,v), then coefficient is 1, otherwise -1
    for index in range(0, len(pathNodes) - 1):
        # check which direction of the arc is contained in the graph
        if (pathNodes[index], pathNodes[index + 1]) in model.Arcs:
            dic_arc_coef[(pathNodes[index], pathNodes[index + 1])] = 1
        else:
            dic_arc_coef[(pathNodes[index + 1], pathNodes[index])] = -1

    # we set objective
    def obj_rule(model):
        return sum(dic_arc_coef[arc] * model.Flow[arc] for arc in dic_arc_coef.keys())

    model.Obj = py.Objective(rule=obj_rule, sense=py.maximize)

    # Create a solver
    opt = SolverFactory('gurobi')
    # Solve optimization model
    results = opt.solve(model)
    # status of solver
    status = results.solver.status
    # termination condition
    termCondition = results.solver.termination_condition

    # save the solution of the flows in a dictionary key: arcs, values: flow
    dic_scenario_flow = {}

    if status == SolverStatus.error or status == SolverStatus.aborted or status == SolverStatus.unknown:
        utils.output('Solver status:  ' + str(status) + ', termination condition:  ' + str(termCondition) +
                     '. No output is generated.', 0, 0)
github Pyomo / pyomo / doc / attic / old_sphinx_files / getting_started / spyfiles / iterative1.py View on Github external
# @Call_SolverFactory_with_argument
# Create a solver
opt = pyo.SolverFactory('glpk')
# @Call_SolverFactory_with_argument

#
# A simple model with binary variables and
# an empty constraint list.
#
# @Create_base_model
model = pyo.AbstractModel()
model.n = pyo.Param(default=4)
model.x = pyo.Var(pyo.RangeSet(model.n), within=pyo.Binary)
def o_rule(model):
    return pyo.summation(model.x)
model.o = pyo.Objective(rule=o_rule)
# @Create_base_model
# @Create_empty_constraint_list
model.c = pyo.ConstraintList()
# @Create_empty_constraint_list

# Create a model instance and optimize
# @Create_instantiated_model
instance = model.create_instance()
# @Create_instantiated_model
# @Solve_and_refer_to_results
results = opt.solve(instance)
# @Solve_and_refer_to_results
# @Display_updated_value
instance.display()
# @Display_updated_value