Esempio n. 1
0
    def __init__(self, problem=None, *args, **kwargs):

        super(Model, self).__init__(*args, **kwargs)

        if problem is None:
            self.problem = gurobipy.Model()
            self.problem.params.OutputFlag = 0
            if self.name is not None:
                self.problem.setAttr('ModelName', self.name)
                self.problem.update()

        elif isinstance(problem, gurobipy.Model):
            self.problem = problem
            variables = []
            for gurobi_variable in self.problem.getVars():
                variables.append(Variable(
                    gurobi_variable.getAttr("VarName"),
                    lb=gurobi_variable.getAttr("lb"),
                    ub=gurobi_variable.getAttr("ub"),
                    problem=self,
                    type=_GUROBI_VTYPE_TO_VTYPE[gurobi_variable.getAttr("vType")]
                ))
            super(Model, self)._add_variables(variables)
            constraints = []
            for gurobi_constraint in self.problem.getConstrs():
                sense = gurobi_constraint.Sense
                name = gurobi_constraint.getAttr("ConstrName")
                rhs = gurobi_constraint.RHS
                row = self.problem.getRow(gurobi_constraint)
                lhs = symbolics.add(
                    [symbolics.Real(row.getCoeff(i)) * self.variables[row.getVar(i).VarName] for i in
                     range(row.size())]
                )

                if sense == '=':
                    constraint = Constraint(lhs, name=name, lb=rhs, ub=rhs, problem=self)
                elif sense == '>':
                    constraint = Constraint(lhs, name=name, lb=rhs, ub=None, problem=self)
                elif sense == '<':
                    constraint = Constraint(lhs, name=name, lb=None, ub=rhs, problem=self)
                else:
                    raise ValueError('{} is not a valid sense'.format(sense))
                constraints.append(constraint)
            super(Model, self)._add_constraints(constraints, sloppy=True)

            gurobi_objective = self.problem.getObjective()
            linear_expression = symbolics.add(
                [symbolics.Real(gurobi_objective.getCoeff(i)) * self.variables[gurobi_objective.getVar(i).VarName]
                 for i in range(gurobi_objective.size())]
            )

            self._objective = Objective(
                linear_expression,
                problem=self,
                direction={1: 'min', -1: 'max'}[self.problem.getAttr('ModelSense')]
            )
        else:
            raise TypeError("Provided problem is not a valid Gurobi model.")

        self.configuration = Configuration(problem=self, verbosity=0)
Esempio n. 2
0
 def _get_expression(self):
     if self.problem is not None and self._expression_expired and len(
             self.problem._variables) > 0:
         grb_obj = self.problem.problem.getObjective()
         variables = self.problem._variables
         if self.problem.problem.IsQP:
             quadratic_expression = symbolics.add([
                 symbolics.Real(grb_obj.getCoeff(i)) *
                 variables[grb_obj.getVar1(i).VarName] *
                 variables[grb_obj.getVar2(i).VarName]
                 for i in range(grb_obj.size())
             ])
             linear_objective = grb_obj.getLinExpr()
         else:
             quadratic_expression = symbolics.Real(0.0)
             linear_objective = grb_obj
         linear_expression = symbolics.add([
             symbolics.Real(linear_objective.getCoeff(i)) *
             variables[linear_objective.getVar(i).VarName]
             for i in range(linear_objective.size())
         ])
         self._expression = (linear_expression + quadratic_expression +
                             getattr(self.problem, "_objective_offset", 0))
         self._expression_expired = False
     return self._expression
Esempio n. 3
0
 def _get_expression(self):
     if self.problem is not None:
         variables = self.problem._variables
         all_coefs = self.problem.problem.constraint_coefs
         coefs = [(v, all_coefs.get((self.name, v.name), 0.0))
                  for v in variables]
         expression = add(
             [mul((symbolics.Real(co), v)) for (v, co) in coefs])
         self._expression = expression
     return self._expression
Esempio n. 4
0
 def _get_expression(self):
     if self.problem is not None:
         cplex_problem = self.problem.problem
         cplex_row = cplex_problem.linear_constraints.get_rows(self.name)
         variables = self.problem._variables
         expression = add([
             mul((symbolics.Real(cplex_row.val[i]), variables[ind]))
             for i, ind in enumerate(cplex_row.ind)
         ])
         self._expression = expression
     return self._expression
Esempio n. 5
0
 def _get_expression(self):
     if self.problem is not None:
         col_num = glp_get_num_cols(self.problem.problem)
         ia = intArray(col_num + 1)
         da = doubleArray(col_num + 1)
         nnz = glp_get_mat_row(self.problem.problem, self._index, ia, da)
         constraint_variables = [self.problem._variables[glp_get_col_name(self.problem.problem, ia[i])] for i in
                                 range(1, nnz + 1)]
         expression = symbolics.add(
             [symbolics.mul((symbolics.Real(da[i]), constraint_variables[i - 1])) for i in
              range(1, nnz + 1)])
         self._expression = expression
     return self._expression
Esempio n. 6
0
 def _get_expression(self):
     if self.problem is not None:
         gurobi_problem = self.problem.problem
         gurobi_constraint = self._internal_constraint
         row = gurobi_problem.getRow(gurobi_constraint)
         terms = []
         for i in range(row.size()):
             internal_var_name = row.getVar(i).VarName
             if internal_var_name == self.name + '_aux':
                 continue
             variable = self.problem._variables[internal_var_name]
             coeff = symbolics.Real(row.getCoeff(i))
             terms.append(symbolics.mul((coeff, variable)))
         self._expression = symbolics.add(terms)
     return self._expression
Esempio n. 7
0
 def _get_expression(self):
     if self.problem is not None:
         cplex_problem = self.problem.problem
         try:
             cplex_row = cplex_problem.linear_constraints.get_rows(self.name)
         except CplexSolverError as e:
             if 'CPLEX Error  1219:' not in str(e):
                 raise e
             else:
                 cplex_row = cplex_problem.indicator_constraints.get_linear_components(self.name)
         variables = self.problem._variables
         expression = add(
             [mul((symbolics.Real(cplex_row.val[i]), variables[ind])) for i, ind in
              enumerate(cplex_row.ind)])
         self._expression = expression
     return self._expression
Esempio n. 8
0
    def _initialize_model_from_problem(self, problem):
        try:
            self.problem = problem
            glp_create_index(self.problem)
        except TypeError:
            raise TypeError("Provided problem is not a valid GLPK model.")
        row_num = glp_get_num_rows(self.problem)
        col_num = glp_get_num_cols(self.problem)
        for i in range(1, col_num + 1):
            var = Variable(
                glp_get_col_name(self.problem, i),
                lb=glp_get_col_lb(self.problem, i),
                ub=glp_get_col_ub(self.problem, i),
                problem=self,
                type=_GLPK_VTYPE_TO_VTYPE[
                    glp_get_col_kind(self.problem, i)]
            )
            # This avoids adding the variable to the glpk problem
            super(Model, self)._add_variables([var])
        variables = self.variables

        for j in range(1, row_num + 1):
            ia = intArray(col_num + 1)
            da = doubleArray(col_num + 1)
            nnz = glp_get_mat_row(self.problem, j, ia, da)
            constraint_variables = [variables[ia[i] - 1] for i in range(1, nnz + 1)]

            # Since constraint expressions are lazily retrieved from the solver they don't have to be built here
            # lhs = _unevaluated_Add(*[da[i] * constraint_variables[i - 1]
            #                         for i in range(1, nnz + 1)])
            lhs = 0

            glpk_row_type = glp_get_row_type(self.problem, j)
            if glpk_row_type == GLP_FX:
                row_lb = glp_get_row_lb(self.problem, j)
                row_ub = row_lb
            elif glpk_row_type == GLP_LO:
                row_lb = glp_get_row_lb(self.problem, j)
                row_ub = None
            elif glpk_row_type == GLP_UP:
                row_lb = None
                row_ub = glp_get_row_ub(self.problem, j)
            elif glpk_row_type == GLP_DB:
                row_lb = glp_get_row_lb(self.problem, j)
                row_ub = glp_get_row_ub(self.problem, j)
            elif glpk_row_type == GLP_FR:
                row_lb = None
                row_ub = None
            else:
                raise Exception(
                    "Currently, optlang does not support glpk row type %s"
                    % str(glpk_row_type)
                )
                log.exception()
            if isinstance(lhs, int):
                lhs = symbolics.Integer(lhs)
            elif isinstance(lhs, float):
                lhs = symbolics.Real(lhs)
            constraint_id = glp_get_row_name(self.problem, j)
            for variable in constraint_variables:
                try:
                    self._variables_to_constraints_mapping[variable.name].add(constraint_id)
                except KeyError:
                    self._variables_to_constraints_mapping[variable.name] = set([constraint_id])

            super(Model, self)._add_constraints(
                [Constraint(lhs, lb=row_lb, ub=row_ub, name=constraint_id, problem=self, sloppy=True)],
                sloppy=True
            )

        term_generator = (
            (glp_get_obj_coef(self.problem, index), variables[index - 1])
            for index in range(1, glp_get_num_cols(problem) + 1)
        )
        self._objective = Objective(
            symbolics.add(
                [symbolics.mul((symbolics.Real(term[0]), term[1])) for term in term_generator if
                 term[0] != 0.]
            ),
            problem=self,
            direction={GLP_MIN: 'min', GLP_MAX: 'max'}[glp_get_obj_dir(self.problem)])
        glp_scale_prob(self.problem, GLP_SF_AUTO)
Esempio n. 9
0
 def term_generator():
     for index in range(1, glp_get_num_cols(self.problem.problem) + 1):
         coeff = glp_get_obj_coef(self.problem.problem, index)
         if coeff != 0.:
             yield (symbolics.Real(coeff), variables[index - 1])
Esempio n. 10
0
    def __init__(self, problem=None, *args, **kwargs):

        super(Model, self).__init__(*args, **kwargs)

        if problem is None:
            self.problem = cplex.Cplex()

        elif isinstance(problem, cplex.Cplex):
            self.problem = problem
            zipped_var_args = zip(
                self.problem.variables.get_names(),
                self.problem.variables.get_lower_bounds(),
                self.problem.variables.get_upper_bounds(),
                # self.problem.variables.get_types(), # TODO uncomment when cplex is fixed
            )
            for name, lb, ub in zipped_var_args:
                var = Variable(name, lb=lb, ub=ub,
                               problem=self)  # Type should also be in there
                super(Model, self)._add_variables([
                    var
                ])  # This avoids adding the variable to the glpk problem
            zipped_constr_args = zip(
                self.problem.linear_constraints.get_names(),
                self.problem.linear_constraints.get_rows(),
                self.problem.linear_constraints.get_senses(),
                self.problem.linear_constraints.get_rhs())
            variables = self._variables
            for name, row, sense, rhs in zipped_constr_args:
                constraint_variables = [variables[i - 1] for i in row.ind]

                # Since constraint expressions are lazily retrieved from the solver they don't have to be built here
                # lhs = _unevaluated_Add(*[val * variables[i - 1] for i, val in zip(row.ind, row.val)])
                lhs = symbolics.Integer(0)
                if sense == 'E':
                    constr = Constraint(lhs,
                                        lb=rhs,
                                        ub=rhs,
                                        name=name,
                                        problem=self)
                elif sense == 'G':
                    constr = Constraint(lhs, lb=rhs, name=name, problem=self)
                elif sense == 'L':
                    constr = Constraint(lhs, ub=rhs, name=name, problem=self)
                elif sense == 'R':
                    range_val = self.problem.linear_constraints.get_range_values(
                        name)
                    if range_val > 0:
                        constr = Constraint(lhs,
                                            lb=rhs,
                                            ub=rhs + range_val,
                                            name=name,
                                            problem=self)
                    else:
                        constr = Constraint(lhs,
                                            lb=rhs + range_val,
                                            ub=rhs,
                                            name=name,
                                            problem=self)
                else:  # pragma: no cover
                    raise Exception(
                        '%s is not a recognized constraint sense.' % sense)

                for variable in constraint_variables:
                    try:
                        self._variables_to_constraints_mapping[
                            variable.name].add(name)
                    except KeyError:
                        self._variables_to_constraints_mapping[
                            variable.name] = set([name])

                super(Model, self)._add_constraints([constr], sloppy=True)
            try:
                objective_name = self.problem.objective.get_name()
            except CplexSolverError as e:
                if 'CPLEX Error  1219:' not in str(e):
                    raise e
            else:
                linear_expression = add([
                    mul(symbolics.Real(coeff), variables[index]) for index,
                    coeff in enumerate(self.problem.objective.get_linear())
                    if coeff != 0.
                ])
                try:
                    quadratic = self.problem.objective.get_quadratic()
                except IndexError:
                    quadratic_expression = Zero
                else:
                    quadratic_expression = self._get_quadratic_expression(
                        quadratic)

                self._objective = Objective(
                    linear_expression + quadratic_expression,
                    problem=self,
                    direction={
                        self.problem.objective.sense.minimize: 'min',
                        self.problem.objective.sense.maximize: 'max'
                    }[self.problem.objective.get_sense()],
                    name=objective_name)
        else:
            raise TypeError("Provided problem is not a valid CPLEX model.")
        self.configuration = Configuration(problem=self, verbosity=0)
Esempio n. 11
0
    def _initialize_model_from_problem(self, problem):
        if isinstance(problem, gurobipy.Model):
            self.problem = problem
            variables = []
            for gurobi_variable in self.problem.getVars():
                variables.append(
                    Variable(gurobi_variable.getAttr("VarName"),
                             lb=gurobi_variable.getAttr("lb"),
                             ub=gurobi_variable.getAttr("ub"),
                             problem=self,
                             type=_GUROBI_VTYPE_TO_VTYPE[
                                 gurobi_variable.getAttr("vType")]))
            super(Model, self)._add_variables(variables)
            constraints = []
            for gurobi_constraint in self.problem.getConstrs():
                sense = gurobi_constraint.Sense
                name = gurobi_constraint.getAttr("ConstrName")
                rhs = gurobi_constraint.RHS
                row = self.problem.getRow(gurobi_constraint)
                lhs = symbolics.add([
                    symbolics.Real(row.getCoeff(i)) *
                    self.variables[row.getVar(i).VarName]
                    for i in range(row.size())
                ])

                if sense == '=':
                    constraint = Constraint(lhs,
                                            name=name,
                                            lb=rhs,
                                            ub=rhs,
                                            problem=self)
                elif sense == '>':
                    constraint = Constraint(lhs,
                                            name=name,
                                            lb=rhs,
                                            ub=None,
                                            problem=self)
                elif sense == '<':
                    constraint = Constraint(lhs,
                                            name=name,
                                            lb=None,
                                            ub=rhs,
                                            problem=self)
                else:
                    raise ValueError('{} is not a valid sense'.format(sense))
                constraints.append(constraint)
            super(Model, self)._add_constraints(constraints, sloppy=True)

            gurobi_objective = self.problem.getObjective()
            if self.problem.IsQP:
                quadratic_expression = symbolics.add([
                    symbolics.Real(gurobi_objective.getCoeff(i)) *
                    self.variables[gurobi_objective.getVar1(i).VarName] *
                    self.variables[gurobi_objective.getVar2(i).VarName]
                    for i in range(gurobi_objective.size())
                ])
                linear_objective = gurobi_objective.getLinExpr()
            else:
                quadratic_expression = symbolics.Real(0.0)
                linear_objective = gurobi_objective
            linear_expression = symbolics.add([
                symbolics.Real(linear_objective.getCoeff(i)) *
                self.variables[linear_objective.getVar(i).VarName]
                for i in range(linear_objective.size())
            ])

            self._objective = Objective(quadratic_expression +
                                        linear_expression,
                                        problem=self,
                                        direction={
                                            1: 'min',
                                            -1: 'max'
                                        }[self.problem.getAttr('ModelSense')])
        else:
            raise TypeError("Provided problem is not a valid Gurobi model.")