Example #1
0
def end():
    from pycsp3.dashboard import options
    from pycsp3.tools.utilities import Error
    global _solver
    verbose = 1
    if not Compilation.done and not Error.errorOccurrence:
        filename, cop = Compilation.compile(disabling_opoverrider=True)
        solving = ACE.name if options.solve else options.solver
        if solving:
            if options.display:
                warning(
                    "options -display and -solve should not be used together.")
                return filename
            solver_name, args, args_recursive = _process_solving(solving)
            _solver = _set_solver(solver_name)
            result = _solver.solve((filename, cop),
                                   solving,
                                   args,
                                   args_recursive,
                                   compiler=True,
                                   verbose=verbose,
                                   automatic=True)
            print("\nResult: ", result)
            if solution():
                print(solution())
Example #2
0
 def __bool__(self):
     warning(
         "A node is evaluated as a Boolean (technically, __bool__ is called)"
         + "\n\tIt is likely a problem with the use of logical operators" +
         "\n\tFor example, you must write (x[0] == x[1])  | (x[0] == x[2]) instead of (x[0] == x[1])  or (x[0] == x[2])"
         +
         "\n\tSee also the end of section about constraint Intension in chapter 'Twenty popular constraints' of the guide\n"
     )
     return True
Example #3
0
 def __bool__(self):
     warning(
         "A constraint is evaluated as a Boolean (technically, __bool__ is called)"
         + "\n\tIt is likely a problem with the use of logical operators" +
         "\n\tFor example, you must write Or(AllDifferent(x), (x[0] == x[2])) instead of AllDifferent(x) or (x[0] == x[2])"
         + "\n\t or you must post separately the two constraints" +
         "\n\tSee also the end of section about constraint Intension in chapter 'Twenty popular constraints' of the guide\n"
     )
     return True
Example #4
0
 def system_command(command, origin, target):
     assert command in {"mv", "cp"}
     print(os.getcwd())
     if is_windows():
         command = "move" if command == 'mv' else command
         command = "copy" if command == 'cp' else command
     print(command, origin, target)
     if not os.path.isfile(origin):
         warning("file not found: " + origin)
         exit(0)
     subprocess.call([command, origin, target], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=is_windows())
Example #5
0
def _text(elt, s):
    test = str(s)
    if "False" in test or "True" in test:
        warning(
            "False or True is present in " + test +
            "\n\tIt is likely a problem with the use of logical operators" +
            "\n\tFor example, you must write (x == 0) | (x ==1) instead of (x == 0) or (x == 1)"
            +
            "\n\tSee also the end of section about constraint Intension in chapter 'Twenty popular constraints' of the guide\n"
        )
    elt.text = " " + (" ".join(
        str(v) for v in s) if isinstance(s, (list, tuple)) else str(s)) + " "
Example #6
0
    def check(self, xcsp=False):
        xml_to_compare = self.xml_path_xcsp() if xcsp else self.xml_path_jv()
        self.counters["total"] += 1

        # LZMA decompress
        xml_lzma = None
        if os.path.isfile(xml_to_compare + ".lzma"):
            if is_windows():
                print("  Not comparing because of LZMA on windows")
                return None
            xml_lzma = xml_to_compare + ".lzma"
            import lzma
            with lzma.open(xml_lzma) as f:
                data = f.read()
                fp = open(xml_to_compare, "wb")
                fp.write(data)
                fp.close()

            print("Decompress " + xml_lzma + " done.")

        # Show differences    
        if not os.path.isfile(self.xml_path_py()) or not os.path.isfile(xml_to_compare):
            warning("at least 1 file not found among " + self.xml_path_py() + " and " + xml_to_compare)
            self.counters["err"] += 1
            if waiting:
                input("Press Enter to continue...")
        else:
            lines = self.diff_files(self.xml_path_py(), xml_to_compare, self.tmpDiff)
            if len(lines) == 0:
                print("  => No difference for " + self.name_xml)
            else:
                print("  => Several differences (" + str(len(lines)) + ") in " + self.name_xml)
                self.counters["diff"] += 1
                self.print_differences(lines, limit=20 if len(lines) > 200 else None, xcsp=xcsp)
                if waiting:
                    input("Press Enter to continue...")

        # LZMA delete the decompress file
        if xml_lzma is not None:
            os.remove(xml_to_compare)

        # Count differences
        diff = (RED if self.counters["diff"] > 0 else GREEN) + str(self.counters["diff"])
        err = (RED if self.counters["err"] > 0 else GREEN) + str(self.counters["err"])
        print("\n" + WHITE_BOLD + "[Currently] " + diff + WHITE + " difference(s) on " + str(
            self.counters["total"]) + " test(s) (" + err + WHITE + " error(s))\n")
Example #7
0
def solve(*,
          solver=ACE,
          options="",
          filename=None,
          verbose=-1,
          sols=None,
          extraction=False):
    """
    Solves the current model (after compiling it) and returns the status of this operation.

    :param solver: name of the solver (ACE or CHOCO), possibly accompanied with general options
                   as defined in https://github.com/xcsp3team/pycsp3/blob/master/docs/optionsSolvers.pdf
    :param options: specific options for the solver
    :param filename: the filename of the compiled problem instance
    :param verbose: verbosity level from -1 to 2
    :param sols: number of solutions to be found (ALL if no limit)
    :param extraction: True if an unsatisfiable core of constraints must be sought
    :return: the status of the solving operation
    """
    global _solver
    instance = compile(filename, verbose=verbose)
    if instance is None:
        print("Problem when compiling")
    else:
        if isinstance(solver, TypeSolver):
            if sols == ALL or isinstance(
                    sols, int
            ) and sols > 1:  # options for displaying all solution in XML format
                options += " -xe -xc=false" if solver == ACE else " -a "
            solver = "[" + solver.name.lower()
            if verbose != -1:
                solver += "," + ("v" if verbose == 0 else
                                 "vv" if verbose == 1 else "vvv")
            if sols is not None:
                solver += ",limit=" + ("no" if sols == ALL else str(sols) +
                                       "sols")
            solver += "]"
        else:
            assert isinstance(solver, str)
            msg = "As you use the parameter 'solver', you should not use the parameter"
            if verbose != -1:
                warning(
                    msg +
                    "'verbose' (which is then ignored); you can write e.g., [ace,v]"
                )
            if sols is not None:
                warning(
                    msg +
                    "'sols' (which is then ignored); you can write e.g., [ace,limit=no]"
                )
            solver = ("[" if len(solver) > 0 and solver[0] != '[' else
                      "") + solver + ("]" if len(solver) > 0
                                      and solver[-1] != ']' else "")

        solver_name, args, args_recursive = _process_solving(solver)
        _solver = _set_solver(solver_name)
        _solver.setting(options)
        return _solver.solve(instance,
                             solver,
                             args,
                             args_recursive,
                             verbose=verbose,
                             extraction=extraction)