How to use the pyomo.core.base.value 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 / repn / plugins / cpxlp.py View on Github external
def _get_bound(self, exp):
        if exp is None:
            return None
        if is_fixed(exp):
            return value(exp)
        raise ValueError("non-fixed bound: " + str(exp))
github Pyomo / pyomo / pyomo / contrib / mindtpy / MindtPy_new.py View on Github external
# generate new constraints
        # TODO some kind of special handling if the dual is phenomenally small?
        gen = (constr for constr in MindtPy.jacs
               if (0 if constr.upper is None
                   else abs(value(constr.body) - constr.upper)) +
               (0 if constr.lower is None
                else abs(constr.lower - value(constr.body)))
               > config.ECP_tolerance)
        for constr in gen:
            constr_dir = -1 if value(constr.upper) is None else 1
            rhs = ((0 if constr.upper is None else constr.upper) +
                   (0 if constr.lower is None else constr.lower))
            # this only happens if a constraint is >=
            c = MindtPy.MindtPy_linear_cuts.ecp_cuts.add(
                expr=copysign(1, constr_dir)
                * (sum(value(MindtPy.jacs[constr][id(var)]) * (var - value(var))
                       for var in list(EXPR.identify_variables(constr.body))) +
                   value(constr.body) - rhs) <= 0)
            MindtPy.ECP_constr_map[constr, solve_data.mip_iter] = c
github Pyomo / pyomo / pyomo / contrib / mindtpy / MindtPy_new.py View on Github external
c.activate()
        else:
            for c in MindtPy.nonlinear_constraints:
                c.activate()
        MindtPy.MindtPy_linear_cuts.deactivate()
        getattr(m, 'ipopt_zL_out', _DoNothing()).activate()
        getattr(m, 'ipopt_zU_out', _DoNothing()).activate()

        # Process master problem result
        if master_terminate_cond is tc.optimal:
            # proceed. Just need integer values
            m.solutions.load_from(results)
            self._copy_values(m, solve_data.working_model, config)

            if MindtPy.obj.sense == minimize:
                solve_data.LB = max(value(MindtPy.obj.expr), solve_data.LB)
                solve_data.LB_progress.append(solve_data.LB)
            else:
                solve_data.UB = min(value(MindtPy.obj.expr), solve_data.UB)
                solve_data.UB_progress.append(solve_data.UB)
            config.logger.info(
                'MIP %s: OBJ: %s  LB: %s  UB: %s'
                % (solve_data.mip_iter, value(MindtPy.obj.expr),
                   solve_data.LB, solve_data.UB))
        elif master_terminate_cond is tc.infeasible:
            print('MILP master problem is infeasible. '
                  'Problem may have no more feasible binary configurations.')
            if solve_data.mip_iter == 1:
                print('MindtPy initialization may have generated poor '
                      'quality cuts.')
            # set optimistic bound to infinity
            if MindtPy.obj.sense == minimize:
github Pyomo / pyomo / pyomo / solvers / plugins / solvers / CPLEXDirect.py View on Github external
# fixed variables (as in not outputting them), there
                # is no need to add them to the compiled model.
                continue

            varname = symbol_map.getSymbol( var, labeler )
            var_names.append(symbol_map.getSymbol( var, labeler ))
            var_symbol_pairs.append((var, varname))

            if not var.has_lb():
                var_lbs.append(-CPLEXDirect._cplex_module.infinity)
            else:
                var_lbs.append(value(var.lb))
            if not var.has_ub():
                var_ubs.append(CPLEXDirect._cplex_module.infinity)
            else:
                var_ubs.append(value(var.ub))
            if var.is_binary():
                var_types.append(cplex_instance.variables.type.binary)
                num_binary_variables += 1
            elif var.is_integer():
                var_types.append(cplex_instance.variables.type.integer)
                num_integer_variables += 1
            elif var.is_continuous():
                var_types.append(cplex_instance.variables.type.continuous)
                num_continuous_variables += 1
            else:
                raise TypeError("Invalid domain type for variable with name '%s'. "
                                "Variable is not continuous, integer, or binary.")

        self_variable_symbol_map.addSymbols(var_symbol_pairs)
        cplex_instance.variables.add(names=var_names,
                                     lb=var_lbs,
github Pyomo / pyomo / pyomo / contrib / mindtpy / MindtPy_new.py View on Github external
def _add_int_cut(self, solve_data, config, feasible=False):
        if config.integer_cuts:
            m = solve_data.working_model
            MindtPy = m.MindtPy_utils
            # check to make sure that binary variables are all 0 or 1
            for v in MindtPy.binary_vars:
                if value(abs(v - 1)) > int_tol and value(abs(v)) > int_tol:
                    raise ValueError('Binary {} = {} is not 0 or 1'.format(
                        v.name, value(v)))

            if not MindtPy.binary_vars:  # if no binary variables, skip.
                return

            int_cut = (sum(1 - v for v in MindtPy.binary_vars
                           if value(abs(v - 1)) <= int_tol) +
                       sum(v for v in MindtPy.binary_vars
                           if value(abs(v)) <= int_tol) >= 1)

            if not feasible:
                # Add the integer cut
                MindtPy.MindtPy_linear_cuts.integer_cuts.add(expr=int_cut)
            else:
                MindtPy.MindtPy_linear_cuts.feasible_integer_cuts.add(expr=int_cut)
github Pyomo / pyomo / pyomo / repn / canonical_repn.py View on Github external
def coopr3_generate_canonical_repn(exp, idMap=None, compute_values=True):
    degree = exp.polynomial_degree()

    if idMap is None:
        idMap = {}
    idMap.setdefault(None, {})

    if degree == 0:
        ans = CompiledLinearCanonicalRepn()
        ans.constant = value(exp)
        return ans

    elif degree == 1:
        # varmap is a map from the variable id() to a _VarData.
        # coef is a map from the variable id() to its coefficient.
        coef, varmap = collect_linear_canonical_repn(exp, idMap, compute_values)
        ans = CompiledLinearCanonicalRepn()
        if None in coef:
            val = coef.pop(None)
            if type(val) not in [int,float] or val != 0.0:
                ans.constant = val

        # the six module is inefficient in terms of wrapping iterkeys
        # and itervalues, in the context of Python 2.7. use the native
        # dictionary methods where possible.
        if using_py3:
github Pyomo / pyomo / pyomo / contrib / mindtpy / MindtPy_new.py View on Github external
def _add_oa_cut(self, solve_data, config):
        m = solve_data.working_model
        MindtPy = m.MindtPy_utils
        MindtPy.MindtPy_linear_cuts.nlp_iters.add(solve_data.nlp_iter)
        sign_adjust = -1 if MindtPy.obj.sense == minimize else 1

        # generate new constraints
        # TODO some kind of special handling if the dual is phenomenally small?
        for constr in MindtPy.nonlinear_constraints:
            rhs = ((0 if constr.upper is None else constr.upper) +
                   (0 if constr.lower is None else constr.lower))
            print MindtPy
            c = MindtPy.MindtPy_linear_cuts.oa_cuts.add(
                expr=copysign(1, sign_adjust * m.dual[constr]) * (sum(
                    value(MindtPy.jacs[constr][id(var)]) * (var - value(var))
                    for var in list(EXPR.identify_variables(constr.body))) +
                    value(constr.body) - rhs) +
                MindtPy.MindtPy_linear_cuts.slack_vars[solve_data.nlp_iter,
                                                 MindtPy.nl_map[constr]] <= 0)
            MindtPy.OA_constr_map[constr, solve_data.nlp_iter] = c
github Pyomo / pyomo / pyomo / contrib / mindtpy / MindtPy_new.py View on Github external
def _solve_NLP_feas(self, solve_data, config):
        m = solve_data.working_model.clone()
        MindtPy = m.MindtPy_utils
        MindtPy.MindtPy_objective.deactivate()
        for constr in m.component_data_objects(
                ctype=Constraint, active=True, descend_into=True):
            constr.deactivate()
        MindtPy.MindtPy_feas.activate()
        MindtPy.MindtPy_feas_obj = Objective(
            expr=sum(s for s in MindtPy.MindtPy_feas.slack_var[...]), sense=minimize)
        for v in MindtPy.binary_vars:
            if value(v) > 0.5:
                v.fix(1)
            else:
                v.fix(0)
        # m.pprint()  #print nlp feasibility problem for debugging
        feas_soln = config.nlp_solver.solve(
            m, load_solutions=False, options=config.nlp_solver_kwargs)
        subprob_terminate_cond = feas_soln.solver.termination_condition
        if subprob_terminate_cond is tc.optimal:
            m.solutions.load_from(feas_soln)
            self._copy_values(m, solve_data.working_model, config)
        elif subprob_terminate_cond is tc.infeasible:
            raise ValueError('Feasibility NLP infeasible. '
                             'This should never happen.')
        else:
            raise ValueError(
                'MindtPy unable to handle feasibility NLP termination condition '
github Pyomo / pyomo / pyomo / repn / plugins / mps.py View on Github external
if skip_trivial_constraints:
                    continue
            elif degree is None:
                raise RuntimeError(
                    "Cannot write legal MPS file. Constraint '%s' "
                    "has nonlinear terms that are not quadratic."
                    % constraint_data.name)

            # Create symbol
            con_symbol = create_symbol_func(symbol_map,
                                            constraint_data,
                                            labeler)

            if constraint_data.equality:
                assert value(constraint_data.lower) == \
                    value(constraint_data.upper)
                label = 'c_e_' + con_symbol + '_'
                alias_symbol_func(symbol_map, constraint_data, label)
                output_file.write(" E  %s\n" % (label))
                offset = extract_variable_coefficients(
                    label,
                    repn,
                    column_data,
                    quadmatrix_data,
                    variable_to_column)
                bound = constraint_data.lower
                bound = _get_bound(bound) - offset
                rhs_data.append((label, _no_negative_zero(bound)))
            else:
                if constraint_data.has_lb():
                    if constraint_data.has_ub():
                        label = 'r_l_' + con_symbol + '_'
github Pyomo / pyomo / pyomo / repn / canonical_repn.py View on Github external
def _collect_identity(exp, idMap, multiplier, coef, varmap, compute_values):
    exp = exp.expr
    if exp.is_fixed():
        if compute_values:
            coef[None] += multiplier * value(exp)
        else:
            coef[None] += multiplier * exp
    else:
        _linear_collectors[exp.__class__](exp, idMap, multiplier, coef, varmap, compute_values)