1

I am working with the python-constraint library and a logical error has risen.

It seems to me that a similar question has been asked here but I don't understand how to apply it in my case

First I call this code

my_function(List):
  from constraint import Problem , AllDifferentConstraint
  problem = Problem()
  people = ["bob", "tom"]
  times = ["2:00", "3:00"]

  t_vars = list(map(lambda x: "t_"+x, people))
  problem.addVariables(t_vars, times)

  problem.addConstraint(AllDifferentConstraint(), t_vars)

  for person in List:
      problem.addConstraint (
          (lambda x:  
              (x == person[1])
          ),
          ["t_"+person[0]]
      )

  return problem.getSolutions()

Then I call with

my_function([["bob", "2:00"], ["tom", "3:00"]])

and it returns an empty []. Why?

However, if I enter

my_function([["bob", "2:00"]])

it returns what I want, which is [{'t_bob': '2:00', 't_tom': '3:00'}]

MrWhiteee
  • 67
  • 4
  • without seeing the code of `problem.getSolutions()` it's impossible to know, since what your function returns will be what `getSolution` returns. –  Oct 31 '22 at 15:03
  • @SembeiNorimaki the code is in the function, getSolutions is from the python-constraint library – MrWhiteee Oct 31 '22 at 15:08

1 Answers1

0

Following the guidelines from another post's answer, I came up with this solution and it works:

def generate_constrained_func(val_in_dict):
    def constraint_func(x):
        return (x == val_in_dict)
    return constraint_func

for person in List:
    if(person):
        problem.addConstraint(generate_constrained_func(person[1]), (["t_"+person[0]]))
MrWhiteee
  • 67
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 03 '22 at 14:00