예제 #1
0
    def read_one_solution(cls, xml_solution, mdl, mst_path):
        new_mipstart_sol = cls.new_empty_mipstart(mdl)
        nb_missed = 0
        effort_level = EffortLevel.Auto
        for child in xml_solution:
            if child.tag == "header":
                obj_s = child.attrib.get('objectiveValue')
                if obj_s is not None:
                    new_mipstart_sol.set_objective_value(float(obj_s))
                # an optional mip start name
                sol_name = child.attrib.get('solutionName')
                if sol_name:
                    new_mipstart_sol.set_name(sol_name)
                # effort level: 0,1,2,...
                effort_s = child.attrib.get('MIPStartEffortLevel')
                if effort_s is not None:
                    # expect an integer attribute, then convert it to EffortLevel
                    # default is Auto
                    effort_level = EffortLevel.parse(cls.to_int(effort_s,
                                                                fallback=0),
                                                     fallback=EffortLevel.Auto)

            elif child.tag == "variables":
                for v, var_elt in enumerate(child, start=1):
                    var_attribs = var_elt.attrib
                    var_name = var_attribs.get('name')
                    var_value_s = var_attribs.get('value')

                    # check the <variable> taag
                    valid = True

                    # we need a value
                    var_value = cls.to_float(var_value_s)
                    if var_value is None:
                        mdl.warning(
                            "Variable element has no float value, {0} was found - pos={1}",
                            var_value_s, v)
                        valid = False

                    var_index = cls.to_int(var_attribs.get('index'))
                    if var_index is None:
                        mdl.warning(
                            "Variable element has no index tag - pos={0}", v)
                        valid = False
                    elif var_index < 0:
                        mdl.warning(
                            "Variable element has invalid index: {1} - pos={0}",
                            v, var_index)
                        valid = False
                    if not valid:
                        nb_missed += 1
                    else:
                        # look for a variable from name, index
                        dv = (var_name and mdl.get_var_by_name(var_name)
                              ) or mdl.get_var_by_index(var_index)

                        if dv is None:
                            mdl.warning(
                                'Cannot find matching variable: name={0}, index={1} - value={2}',
                                var_name, var_index, var_value)
                            nb_missed += 1
                        else:
                            new_mipstart_sol._set_var_value(dv, var_value)
        if nb_missed:
            mdl.warning("Found {0} unmatched variable(s) in file {1}",
                        nb_missed, mst_path)
        # return a tiple (solution, effort)
        if not new_mipstart_sol.number_of_var_values:
            mdl.warning("No variable read from MIP start file {0}", mst_path)
            return None
        else:
            return new_mipstart_sol, effort_level
예제 #2
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")