How to use the pyomo.core.expr.numvalue.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 / core / expr / expr_pyomo5.py View on Github external
def resolve_template(self):
        return self._base.__getitem__(tuple(value(i) for i in self._args_))
github Pyomo / pyomo / pyomo / contrib / preprocessing / plugins / init_vars.py View on Github external
def _apply_to(self, instance, overwrite=False):
        """Apply the transformation.

        Kwargs:
            overwrite: if False, transformation will not overwrite existing
                variable values.
        """
        for var in instance.component_data_objects(
                ctype=Var, descend_into=True):
            if var.fixed:
                continue
            if var.value is not None and not overwrite:
                continue
            if var.lb is not None and value(var.lb) > 0:
                var.set_value(value(var.lb))
            elif var.ub is not None and value(var.ub) < 0:
                var.set_value(value(var.ub))
            else:
                var.set_value(0)
github Pyomo / pyomo / pyomo / solvers / plugins / solvers / cplex_direct.py View on Github external
def _add_var(self, var):
        varname = self._symbol_map.getSymbol(var, self._labeler)
        vtype = self._cplex_vtype_from_var(var)
        if var.has_lb():
            lb = value(var.lb)
        else:
            lb = -self._cplex.infinity
        if var.has_ub():
            ub = value(var.ub)
        else:
            ub = self._cplex.infinity

        self._solver_model.variables.add(lb=[lb], ub=[ub], types=[vtype], names=[varname])

        self._pyomo_var_to_solver_var_map[var] = varname
        self._solver_var_to_pyomo_var_map[varname] = var
        self._pyomo_var_to_ndx_map[var] = self._ndx_count
        self._ndx_count += 1
        self._referenced_variables[var] = 0

        if var.is_fixed():
github Pyomo / pyomo / pyomo / core / kernel / piecewise_library / transforms.py View on Github external
(not input_var.has_lb()) or \
            (not input_var.has_ub())):
                raise PiecewiseValidationError(
                    "Piecewise function input is not a "
                    "variable with finite upper and lower "
                    "bounds: %s. To avoid this error, set the "
                    "'require_bounded_input_variable' keyword "
                    "to False or disable validation."
                    % (str(input_var)))

        if require_variable_domain_coverage and \
           (input_var is not None):
            domain_lb = value(self.breakpoints[0])
            domain_ub = value(self.breakpoints[-1])
            if input_var.has_lb() and \
               value(input_var.lb) < domain_lb:
                raise PiecewiseValidationError(
                    "Piecewise function domain does not include "
                    "the lower bound of the input variable: "
                    "%s.ub = %s > %s. To avoid this error, set "
                    "the 'require_variable_domain_coverage' "
                    "keyword to False or disable validation."
                    % (input_var.name,
                       value(input_var.lb),
                       domain_lb))
            if input_var.has_ub() and \
               value(input_var.ub) > domain_ub:
                raise PiecewiseValidationError(
                    "Piecewise function domain does not include "
                    "the upper bound of the input variable: "
                    "%s.ub = %s > %s. To avoid this error, set "
                    "the 'require_variable_domain_coverage' "
github Pyomo / pyomo / pyomo / core / expr / expr_pyomo5.py View on Github external
def _is_fixed(self, args):
        assert(len(args) == 3)
        if args[0]: #self._if.is_constant():
            if value(self._if):
                return args[1] #self._then.is_constant()
            else:
                return args[2] #self._else.is_constant()
        else:
            return False
github Pyomo / pyomo / pyomo / contrib / preprocessing / plugins / var_aggregator.py View on Github external
def _get_equality_linked_variables(constraint):
    """Return the two variables linked by an equality constraint x == y.

    If the constraint does not match this form, skip it.

    """
    if value(constraint.lower) != 0 or value(constraint.upper) != 0:
        # LB and UB on constraint must be zero; otherwise, return empty tuple.
        return ()
    if constraint.body.polynomial_degree() != 1:
        # must be a linear constraint; otherwise, return empty tuple.
        return ()

    # Generate the standard linear representation
    repn = generate_standard_repn(constraint.body)
    nonzero_coef_vars = tuple(v for i, v in enumerate(repn.linear_vars)
                              # if coefficient on variable is nonzero
                              if repn.linear_coefs[i] != 0)
    if len(nonzero_coef_vars) != 2:
        # Expect two variables with nonzero cofficient in constraint;
        # otherwise, return empty tuple.
        return ()
    if sorted(coef for coef in repn.linear_coefs if coef != 0) != [-1, 1]:
github Pyomo / pyomo / pyomo / solvers / plugins / solvers / mosek_direct.py View on Github external
def set_con_bounds(self, con, constant):

        if con.equality:
            ub = value(con.upper) - constant
            lb = value(con.lower) - constant
            con_type = self._mosek.boundkey.fx
        elif con.has_lb() and con.has_ub():
            ub = value(con.upper) - constant
            lb = value(con.lower) - constant
            con_type = self._mosek.boundkey.ra
        elif con.has_lb():
            ub = 0
            lb = value(con.lower) - constant
            con_type = self._mosek.boundkey.lo
        elif con.has_ub():
            ub = value(con.upper) - constant
            lb = 0
            con_type = self._mosek.boundkey.up
        else:
            ub = 0
            lb = 0
            con_type = self._mosek.boundkey.fr
        return con_type, ub, lb
github Pyomo / pyomo / pyomo / solvers / plugins / solvers / gurobi_persistent.py View on Github external
if con.has_lb():
            if con.has_ub():
                raise ValueError('Range constraints are not supported in cbCut.')
            if not is_fixed(con.lower):
                raise ValueError('Lower bound of constraint {0} is not constant.'.format(con))
        if con.has_ub():
            if not is_fixed(con.upper):
                raise ValueError('Upper bound of constraint {0} is not constant.'.format(con))

        if con.equality:
            self._solver_model.cbCut(lhs=gurobi_expr, sense=self._gurobipy.GRB.EQUAL,
                                     rhs=value(con.lower))
        elif con.has_lb() and (value(con.lower) > -float('inf')):
            self._solver_model.cbCut(lhs=gurobi_expr, sense=self._gurobipy.GRB.GREATER_EQUAL,
                                     rhs=value(con.lower))
        elif con.has_ub() and (value(con.upper) < float('inf')):
            self._solver_model.cbCut(lhs=gurobi_expr, sense=self._gurobipy.GRB.LESS_EQUAL,
                                     rhs=value(con.upper))
        else:
            raise ValueError('Constraint does not have a lower or an upper bound {0} \n'.format(con))
github Pyomo / pyomo / pyomo / solvers / plugins / solvers / gurobi_direct.py View on Github external
def _add_var(self, var):
        varname = self._symbol_map.getSymbol(var, self._labeler)
        vtype = self._gurobi_vtype_from_var(var)
        if var.has_lb():
            lb = value(var.lb)
        else:
            lb = -self._gurobipy.GRB.INFINITY
        if var.has_ub():
            ub = value(var.ub)
        else:
            ub = self._gurobipy.GRB.INFINITY

        gurobipy_var = self._solver_model.addVar(lb=lb, ub=ub, vtype=vtype, name=varname)

        self._pyomo_var_to_solver_var_map[var] = gurobipy_var
        self._solver_var_to_pyomo_var_map[gurobipy_var] = var
        self._referenced_variables[var] = 0

        if var.is_fixed():
            gurobipy_var.setAttr('lb', var.value)
            gurobipy_var.setAttr('ub', var.value)
github Pyomo / pyomo / pyomo / core / expr / expr_pyomo5.py View on Github external
const_ = value(self.constant)
            if not isclose(const_,0):
                tmp = [str(const_)]
        elif self.constant.__class__ in native_numeric_types:
            if not isclose(self.constant, 0):
                tmp = [str(self.constant)]
        else:
            tmp = [self.constant.to_string(compute_values=False)]
        if verbose:
            for c,v in zip(self.linear_coefs, self.linear_vars):
                if smap:                        # TODO: coverage
                    v_ = smap.getSymbol(v)
                else:
                    v_ = str(v)
                if c.__class__ in native_numeric_types or compute_values:
                    c_ = value(c)
                    if isclose(c_,1):
                        tmp.append(str(v_))
                    elif isclose(c_,0):
                        continue
                    else:
                        tmp.append("prod(%s, %s)" % (str(c_),str(v_)))
                else:
                    tmp.append("prod(%s, %s)" % (str(c), v_))
            return "{0}({1})".format(self.getname(), ', '.join(tmp))
        for c,v in zip(self.linear_coefs, self.linear_vars):
            if smap:
                v_ = smap.getSymbol(v)
            else:
                v_ = str(v)
            if c.__class__ in native_numeric_types or compute_values:
                c_ = value(c)