예제 #1
0
 def print_signature(out):
     from docplex.version import docplex_version_string
     osa = OutputStreamAdapter(out)
     osa.write("<?xml version='1.0' encoding='utf-8'?>\n")
     osa.write(
         "<!-- This file has been generated by DOcplex version {}  -->\n".
         format(docplex_version_string))
예제 #2
0
    def print_many_solutions(cls,
                             sol_seq,
                             out,
                             use_lp_names,
                             write_level,
                             effort_level=None):
        osa = OutputStreamAdapter(out)
        osa.write(cls.mst_header)
        cls.print_signature(out, write_level)
        # <CPLEXSolutions version="1.0">
        osa.write(cls.many_solution_start_tag)
        osa.write("\n")

        efforts = [EffortLevel.Auto]
        if effort_level is None:
            pass
        elif is_iterable(effort_level):
            efforts = effort_level
        else:
            efforts = [effort_level]

        for sol, effort in izip_longest(sol_seq,
                                        efforts,
                                        fillvalue=EffortLevel.Auto):
            cls.print_one_solution(sol,
                                   out,
                                   print_header=False,
                                   use_lp_names=use_lp_names,
                                   write_level=write_level,
                                   effort_level=effort)

        # <CPLEXSolutions version="1.0">
        osa.write(cls.many_solution_end_tag)
        osa.write("\n")
예제 #3
0
 def print_signature(out, write_level):
     from docplex.version import docplex_version_string
     osa = OutputStreamAdapter(out)
     osa.write(
         "<!-- This file has been generated by DOcplex version {}  -->\n".
         format(docplex_version_string))
     osa.write("<!-- Write level is WriteLevel.{0} -->\n".format(
         write_level.name))
예제 #4
0
 def print_to_stream2(cls, out, solutions, indent=None, **kwargs):
     # solutions can be either a plain solution or a sequence or an iterator
     sol_to_print = list(solutions) if is_iterable(solutions) else [solutions]
     # encode all solutions in dict ready for json output
     encoder = SolutionJSONEncoder(**kwargs)
     solutions_as_dict = [encoder.default(sol) for sol in sol_to_print]
     # use an output stream adapter for py2/py3 and str/unicode compatibility
     osa = OutputStreamAdapter(out)
     if len(sol_to_print) == 1:  # if only one solution, use at root node
         osa.write(json.dumps(solutions_as_dict[0], indent=indent))
     else:  # for multiple solutions, we want a "CPLEXSolutions" root
         osa.write(json.dumps({"CPLEXSolutions": solutions_as_dict}, indent=indent))
예제 #5
0
    def print_many_solutions(cls, sol_seq, out):
        osa = OutputStreamAdapter(out)
        osa.write(cls.mst_header)
        cls.print_signature(out)
        # <CPLEXSolutions version="1.0">
        osa.write(cls.many_solution_start_tag)
        osa.write("\n")

        for sol in sol_seq:
            cls.print_one_solution(sol, out, print_header=False)

        # <CPLEXSolutions version="1.0">
        osa.write(cls.many_solution_end_tag)
        osa.write("\n")
예제 #6
0
    def print(cls, out, model):
        osa = OutputStreamAdapter(out)

        cls.print_signature(out)
        # <CPLEXSolution version="1.0">
        osa.write(cls.annotations_start_tag)

        osa.write(cls.benders_start_tag)
        for cplex_scope, annotated in iteritems(model.get_annotations_by_scope()):
            anno_objtype = cls.cplex_anno_objtype_map.get(cplex_scope)
            if anno_objtype is not None and annotated:
                osa.write("  <object type='{0:d}'>\n".format(anno_objtype))
                for obj, benders_value in annotated:                   # FIXME: what if no name??
                    obj_index = obj.index  # not checked
                    anno_name = cls._anno_name(cplex_scope, obj.get_name(), obj_index)
                    if obj_index >= 0 :
                        # ignore objects not added to the model
                        osa.write("   <anno name='{0}' index='{1:d}' value='{2:d}'/>\n".format(anno_name, obj_index, benders_value))
                osa.write('  </object>\n')
        #  </CPLEXAnnotations>
        osa.write(cls.benders_end_tag)
        osa.write(cls.annotations_end_tag)
        osa.write('\n')
예제 #7
0
    def print_one_solution(cls, sol, out, print_header=True):
        osa = OutputStreamAdapter(out)
        if print_header:
            osa.write(cls.mst_header)
            cls.print_signature(out)
        # <CPLEXSolution version="1.0">
        osa.write(cls.one_solution_start_tag)
        osa.write("\n")

        # <header
        # problemName="foo"
        # objectiveValue="42"
        # />
        osa.write(" <header\n   problemName=\"{0}\"\n".format(
            sol.problem_name))
        if sol.has_objective():
            osa.write("   objectiveValue=\"{0:g}\"\n".format(
                sol.objective_value))
        osa.write("  />\n")

        # prepare reduced costs
        """ For mst, we don't want this !
        model = sol.model
        if not model._solves_as_mip():
            reduced_costs = model.reduced_costs(model.iter_variables())
        else:
            reduced_costs = []
        """
        #  <variables>
        #    <variable name="x1" index ="1" value="3.14"/>
        #  </variables>
        osa.write(" <variables>\n")
        """ For mst, we don't want this !
        for (dvar, rc) in zip_longest(model.iter_variables(), reduced_costs,
                                      fillvalue=None):
            var_name = dvar.name
            var_value = sol[dvar]
            var_index = dvar.index
            rc_string = ""
            if rc is not None:
                rc_string = "reducedCost=\"{}\"".format(rc)
            osa.write("  <variable name=\"{0}\" index=\"{1}\" value=\"{2:g}\" {3}/>\n"
              .format(var_name, var_index, var_value, rc_string))
        """
        for dvar, val in sol.iter_var_values():
            var_name = dvar.print_name()
            var_value = sol[dvar]
            var_index = dvar.index
            osa.write(
                "  <variable name=\"{0}\" index=\"{1}\" value=\"{2:g}\"/>\n".
                format(var_name, var_index, var_value))
        osa.write(" </variables>\n")

        #  </CPLEXSolution version="1.0">
        osa.write(cls.one_solution_end_tag)
        osa.write("\n")
예제 #8
0
    def print_one_solution(cls,
                           sol,
                           out,
                           print_header=True,
                           use_lp_names=True,
                           write_level=None,
                           effort_level=None):
        osa = OutputStreamAdapter(out)
        if print_header:
            osa.write(cls.mst_header)
            cls.print_signature(out, write_level)
        # <CPLEXSolution version="1.0">
        osa.write(cls.one_solution_start_tag)
        osa.write("\n")

        # <header
        # problemName="foo"
        # objectiveValue="42"
        # />
        osa.write(" <header\n   problemName=\"{0}\"\n".format(
            sol.problem_name))
        if sol.has_objective():
            osa.write("   objectiveValue=\"{0:g}\"\n".format(
                sol.objective_value))
        if effort_level is not None:
            # assert this is an integer
            eff = EffortLevel.parse(effort_level)
            if eff != EffortLevel.Auto:
                osa.write("   MIPStartEffortLevel=\"{0:d}\"\n".format(
                    eff.value))
        osa.write("  />\n")

        #  <variables>
        #    <variable name="x1" index ="1" value="3.14"/>
        #  </variables>
        osa.write(" <variables>\n")
        """
            osa.write("  <variable name=\"{0}\" index=\"{1}\" value=\"{2:g}\" {3}/>\n"
              .format(var_name, var_index, var_value, rc_string))
        """
        print_generated = cls.print_generated_vars
        filter_discrete = write_level.filter_discrete()
        eps = 1e-16
        if write_level.filter_zeros():
            for dvar, _ in sol.iter_var_values():
                if print_generated or not dvar.is_generated():
                    if not filter_discrete or dvar.is_discrete():
                        var_name = LPFormat.lp_var_name(
                            dvar) if use_lp_names else dvar.name
                        var_value = sol[dvar]
                        if abs(var_value) >= eps:
                            var_index = dvar.index
                            if var_name:
                                osa.write(
                                    "  <variable name=\"{0}\" index=\"{1}\" value=\"{2:g}\"/>\n"
                                    .format(var_name, var_index, var_value))
                            else:
                                osa.write(
                                    "  <variable index=\"{1}\" value=\"{2:g}\"/>\n"
                                    .format(var_name, var_index, var_value))
        else:
            # iterate on all variebls
            for dvar in sol.model.generate_user_variables():
                if (not filter_discrete) or dvar.is_discrete():
                    var_name = LPFormat.lp_var_name(
                        dvar) if use_lp_names else dvar.name
                    var_value = sol[dvar]
                    var_index = dvar.index
                    if var_name:
                        osa.write(
                            "  <variable name=\"{0}\" index=\"{1}\" value=\"{2:g}\"/>\n"
                            .format(var_name, var_index, var_value))
                    else:
                        osa.write(
                            "  <variable index=\"{1}\" value=\"{2:g}\"/>\n".
                            format(var_name, var_index, var_value))

        osa.write(" </variables>\n")

        #  </CPLEXSolution version="1.0">
        osa.write(cls.one_solution_end_tag)
        osa.write("\n")