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'}]

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

1 Answer 1

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]]))
1
  • 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.
    – Community Bot
    Commented Nov 3, 2022 at 14:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.