Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # C'est un homme qui rentre dans un bar et qui dit: "hey les gars c'est moi !", mais en fait, c'était pas lui...
- from ortools.sat.python import cp_model
- class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
- #Print intermediate solutions.
- def __init__(self, variables):
- cp_model.CpSolverSolutionCallback.__init__(self)
- self.__variables = variables
- self.__solution_count = 0
- def on_solution_callback(self):
- self.__solution_count += 1
- for v in self.__variables:
- print('%s=%i' % (v, self.Value(v)), end=' ')
- print()
- def solution_count(self):
- return self.__solution_count
- def GetEquation(w, LETTERS, letters, BASE):
- LEN_W = len(w)
- equation = []
- for i, l in enumerate(w):
- equation.append(letters[LETTERS.index(l)] * BASE **(LEN_W-i))
- equation = sum(equation)
- return equation
- # OUIIII ya 3 mots mais on pourrait faire avec une demi infinitée
- def LinearProgrammingV8_500CC(w1, w2, w3):
- BASE = 10
- LETTERS = list(set(w1 + w2 + w3))
- FIRST_LETTERS = [w1[0], w2[0], w3[0]]
- LEN_W1 = len(w1)
- LEN_W2 = len(w2)
- LEN_W3 = len(w3)
- model = cp_model.CpModel()
- letters = []
- for l in LETTERS:
- if(l in FIRST_LETTERS):
- # si la lettre apparait en premier, elle est pas égale à 0 et ca nous évite bcp de merdes, mais ca change rien en vrai c'est même pas défini dans la regle
- letters.append(model.NewIntVar(1, BASE - 1, l))
- else:
- letters.append(model.NewIntVar(0, BASE - 1, l))
- # Define constraints.
- model.AddAllDifferent(letters)
- equation1 = GetEquation(w1, LETTERS, letters, BASE)
- equation2 = GetEquation(w2, LETTERS, letters, BASE)
- equation3 = GetEquation(w3, LETTERS, letters, BASE)
- model.Add(equation1 + equation2 == equation3)
- print(equation1)
- print(equation2)
- print(equation3)
- # Solve model.
- solver = cp_model.CpSolver()
- solution_printer = VarArraySolutionPrinter(letters)
- status = solver.SearchForAllSolutions(model, solution_printer)
- solver.solutions()
- print(' - solutions found : %i' % solution_printer.solution_count())
- LinearProgrammingV8_500CC("ROUGE", "FRUIT", "CERISE")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement