コード例 #1
0
ファイル: ppretty.py プロジェクト: Ramboun/projet_ap
    def print_model_to_stream(self, out, model):
        wrapper = _ExportWrapper(oss=out, indent_str=' ', line_width=78)
        self.prepare(model)
        # header
        self._print_signature(out)
        self._print_encoding(out)
        self._print_model_name(out, model)

        # var containers
        self._print_var_containers(out, model)
        self._print_single_vars(out, model)
        # KPI section
        self._print_kpis(out, wrapper, model)

        self._print_objective(wrapper, model)
        wrapper.write("\nsubject to {")
        wrapper.flush()
        self._print_linear_constraints(wrapper, model)
        self._print_ranges(wrapper, model)
        self._print_logicals(wrapper, model)
        self._print_quadratic_cts(wrapper, model)
        self._print_sos(wrapper, model)
        out.write("}\n")
コード例 #2
0
    def print_model_to_stream(self, out, model):
        # reset noncompliant stats
        self._nb_noncompliant_ids = 0
        self._noncompliant_justifier = None

        if not self._is_injective(self._var_name_map):
            # use indices to differentiate names
            import sys
            sys.__stdout__.write("DOcplex: refine variable names\n")
            k = 0
            for dv, lp_varname in iteritems(self._var_name_map):
                refined_name = "%s#%d" % (lp_varname, k)
                self._var_name_map[dv] = refined_name
                k += 1

        TextModelPrinter.prepare(self, model)
        self_num_printer = self._lp_num_printer
        var_name_map = self._var_name_map
        line_size = model.lp_line_length
        wrapper = _ExportWrapper(out,
                                 indent_str=self.__expr_indent,
                                 line_width=line_size)

        self._print_signature(out)
        self._print_encoding(out)
        self._print_model_name(out, model)
        self._newline(out)

        # ---  print objective
        if model.has_multi_objective():

            out.write(model.objective_sense.name)
            out.write(' multi-objectives')
            self._newline(out)
            env = model.environment
            if env.has_cplex and env.cplex_version.startswith("12.9."):
                mobj_indent = 5

                def make_default_obj_name(o_):
                    return 'obj%d' % o_ if o_ > 0 else 'obj'
            else:
                mobj_indent = 7  # length of 'obj1: '

                def make_default_obj_name(o_):
                    return 'obj%d' % (o_ + 1)

            wrapper.set_indent(mobj_indent * ' ')  # length of 'obj1: '
            for o, ot in enumerate(model.iter_multi_objective_tuples()):
                expr, prio, w, abstol, reltol, oname = ot
                name = oname or make_default_obj_name(o)
                obj_label = '%s%s:' % (' ', name)
                wrapper.write(obj_label)
                wrapper.write('Priority=%g Weight=%g AbsTol=%g RelTol=%g' %
                              (prio, w, abstol, reltol))
                wrapper.flush()
                # print the expression and its constant
                printed = self._print_expr_iter(wrapper,
                                                self_num_printer,
                                                var_name_map,
                                                expr.iter_terms(),
                                                allow_empty=True,
                                                accept_zero=True)
                obj_offset = expr.get_constant()
                if obj_offset:
                    if printed and obj_offset > 0:
                        wrapper.write(u'+')
                    wrapper.write(self_num_printer.to_string(obj_offset))

                wrapper.flush(restart_from_empty_line=True)

        else:
            out.write(model.objective_sense.name)
            self._newline(out)
            self.print_single_objective(wrapper, model, self_num_printer,
                                        var_name_map)

        # wrapper.write(' obj:')
        # objexpr = model.objective_expr
        #
        # if objexpr.is_quad_expr():
        #     objlin = objexpr.linear_part
        # else:
        #     objlin = objexpr
        #
        # if self._print_full_obj:
        #     iter_linear_terms = self._iter_full_linear_obj_terms(model, objlin)
        # elif self._has_sos_or_pwl_constraints(model):
        #     iter_linear_terms = self._iter_completed_linear_obj_terms(model, objlin)
        # else:
        #     # write the linear part first
        #     # prints an expr to a stream
        #     iter_linear_terms = objlin.iter_sorted_terms()
        #
        # printed= self._print_expr_iter(wrapper, self_num_printer, var_name_map, iter_linear_terms,
        #                       allow_empty=True, accept_zero=True)
        #
        # if objexpr.is_quad_expr() and objexpr.has_quadratic_term():
        #     self._print_qexpr_obj(wrapper, self_num_printer, var_name_map,
        #                           quad_expr=objexpr,
        #                           force_initial_plus=printed)
        #     printed = True
        #
        # obj_offset = objexpr.get_constant()
        # if obj_offset:
        #     if printed and obj_offset > 0:
        #         wrapper.write(u'+')
        #     wrapper.write(self._num_to_string(obj_offset))
        # # ---
        #
        # wrapper.flush(print_newline=True)

        out.write("Subject To\n")

        for ct in model.iter_constraints():
            self._print_constraint(wrapper, self_num_printer, var_name_map, ct)
        # for lct in model._iter_implicit_equivalence_cts():
        #     wrapper.begin_line(True)
        #     self._print_logical_ct(wrapper, self_num_printer, var_name_map, lct, '<->')
        #     wrapper.flush(restart_from_empty_line=True)

        # lazy constraints
        self.print_linear_constraint_section(out,
                                             wrapper,
                                             model.iter_lazy_constraints(),
                                             "Lazy Constraints",
                                             ct_prefix="l",
                                             var_name_map=var_name_map,
                                             ctname_map=self._lzc_name_map)
        self.print_linear_constraint_section(out,
                                             wrapper,
                                             model.iter_user_cut_constraints(),
                                             "User Cuts",
                                             ct_prefix="u",
                                             var_name_map=var_name_map,
                                             ctname_map=self._ucc_name_map)

        out.write("\nBounds\n")
        symbolic_num_printer = self._num_printer
        print_var_bounds_fn = self._print_var_bounds
        var_print_name_fn = self._var_print_name
        for dvar in model.iter_variables():
            lp_varname = var_print_name_fn(dvar)
            var_lb = dvar.lb
            var_ub = dvar.ub
            if dvar.is_binary():
                print_var_bounds_fn(out, self_num_printer, lp_varname, var_lb,
                                    var_ub)
            else:

                free_lb = dvar.has_free_lb()
                free_ub = dvar.has_free_ub()
                if free_lb and free_ub:
                    print_var_bounds_fn(out,
                                        self_num_printer,
                                        lp_varname,
                                        lb=None,
                                        ub=None)
                elif free_ub:
                    # print only nonzero lbs
                    if var_lb:
                        print_var_bounds_fn(out,
                                            symbolic_num_printer,
                                            lp_varname,
                                            var_lb,
                                            ub=None)

                else:
                    # save the lb if is zero
                    printed_lb = None if 0 == var_lb else var_lb
                    print_var_bounds_fn(out,
                                        symbolic_num_printer,
                                        lp_varname,
                                        lb=printed_lb,
                                        ub=var_ub)

        # add ranged cts vars
        for rng in model.iter_range_constraints():
            (varname, _, rngval) = self._rangeData[rng]
            self._print_var_bounds(out, self_num_printer, varname, rngval, 0)

        iter_semis = chain(model.iter_semicontinuous_vars(),
                           model.iter_semiinteger_vars())

        self._print_var_block(wrapper, model.iter_binary_vars(), 'Binaries')
        self._print_var_block(
            wrapper,
            chain(model.iter_integer_vars(), model.iter_semiinteger_vars()),
            'Generals')
        self._print_var_block(wrapper, iter_semis, 'Semi-continuous')
        self._print_sos_block(wrapper, model)
        self._print_pwl_block(wrapper, model, self_num_printer, var_name_map)
        out.write("End\n")
コード例 #3
0
ファイル: lp_printer.py プロジェクト: Ramboun/projet_ap
    def print_model_to_stream(self, out, model):
        # reset noncompliant stats
        self._nb_noncompliant_ids = 0
        self._noncompliant_justifier = None

        if not self._is_injective(self._var_name_map):
            # use indices to differentiate names
            sys.__stdout__.write("\DOcplex: refine variable names\n")
            k = 0
            for dv, lp_varname in iteritems(self._var_name_map):
                refined_name = "%s#%d" % (lp_varname, k)
                self._var_name_map[dv] = refined_name
                k += 1

        TextModelPrinter.prepare(self, model)
        self_num_printer = self._lp_num_printer
        var_name_map = self._var_name_map

        self._print_signature(out)
        self._print_encoding(out)
        self._print_model_name(out, model)
        self._newline(out)

        # ---  print objective
        out.write(model.objective_sense.name)
        self._newline(out)
        wrapper = _ExportWrapper(out, indent_str=self.__expr_indent)
        wrapper.write(' obj:')
        objexpr = model.objective_expr

        if objexpr.is_quad_expr():
            objlin = objexpr.linear_part
        else:
            objlin = objexpr

        if self._print_full_obj:
            iter_linear_terms = self._iter_full_linear_obj_terms(model, objlin)

        elif self._has_sos_or_pwl_constraints(model):
            iter_linear_terms = self._iter_completed_linear_obj_terms(
                model, objlin)

        else:
            # write the linear part first
            # prints an expr to a stream
            iter_linear_terms = objlin.iter_sorted_terms()

        printed = self._print_expr_iter(wrapper,
                                        self_num_printer,
                                        var_name_map,
                                        iter_linear_terms,
                                        allow_empty=True,
                                        accept_zero=True)

        if objexpr.is_quad_expr() and objexpr.has_quadratic_term():
            self._print_qexpr_obj(wrapper,
                                  self_num_printer,
                                  var_name_map,
                                  quad_expr=objexpr,
                                  force_initial_plus=printed)
            printed = True

        obj_offset = objexpr.get_constant()
        if obj_offset:
            if printed and obj_offset > 0:
                wrapper.write(u'+')
            wrapper.write(self._num_to_string(obj_offset))
        # ---

        wrapper.flush(print_newline=True)

        out.write("Subject To\n")

        for ct in model.iter_constraints():
            self._print_constraint(wrapper, self_num_printer, var_name_map, ct)
        for lct in model.iter_implicit_equivalence_cts():
            wrapper.begin_line(True)
            self._print_logical_ct(wrapper, self_num_printer, var_name_map,
                                   lct, '<->')
            wrapper.flush(print_newline=True, reset=True)

        out.write("\nBounds\n")
        symbolic_num_printer = self._num_printer
        print_var_bounds_fn = self._print_var_bounds
        var_print_name_fn = self._var_print_name
        for dvar in model.iter_variables():
            lp_varname = var_print_name_fn(dvar)
            var_lb = dvar._get_lb()
            var_ub = dvar._get_ub()
            if dvar.is_binary():
                print_var_bounds_fn(out, self_num_printer, lp_varname, var_lb,
                                    var_ub)
            else:

                free_lb = dvar.has_free_lb()
                free_ub = dvar.has_free_ub()
                if free_lb and free_ub:
                    print_var_bounds_fn(out,
                                        self_num_printer,
                                        lp_varname,
                                        lb=None,
                                        ub=None)
                elif free_ub:
                    # print only nonzero lbs
                    if var_lb:
                        print_var_bounds_fn(out,
                                            symbolic_num_printer,
                                            lp_varname,
                                            var_lb,
                                            ub=None)

                else:
                    # save the lb if is zero
                    printed_lb = None if 0 == var_lb else var_lb
                    print_var_bounds_fn(out,
                                        symbolic_num_printer,
                                        lp_varname,
                                        lb=printed_lb,
                                        ub=var_ub)

        # add ranged cts vars
        for rng in model.iter_range_constraints():
            (varname, _, rngval) = self._rangeData[rng]
            self._print_var_bounds(out, self_num_printer, varname, rngval, 0)

        iter_semis = chain(model.iter_semicontinuous_vars(),
                           model.iter_semiinteger_vars())

        self._print_var_block(wrapper, model.iter_binary_vars(), 'Binaries')
        self._print_var_block(
            wrapper,
            chain(model.iter_integer_vars(), model.iter_semiinteger_vars()),
            'Generals')
        self._print_var_block(wrapper, iter_semis, 'Semi-continuous')
        self._print_sos_block(wrapper, model)
        self._print_pwl_block(wrapper, model, self_num_printer, var_name_map)
        out.write("End\n")