0

I tried the python-constraint library to solve a CSP problem, but I don't understand why I got 2048 solutions with a dataframe of 4 records.

My dataframe represents a set of distances from the closest elements to fountains in parks, and I use CSP to optimize the installation of fountains in parks.

data = {
 'id': [0, 1, 2, 3, 4],
 'nearest_rev': [2697.5691, 2745.4515, 2744.0437, 2582.4061],
 'nearest_fountain': [48.9079, 1.4088, 1.4088, 141.3484],
 'nearest_rvertes': [2512.2950, 2541.8665, 2540.7561, 2371.0535],
 'nearest_pcyclable': [176.1136, 167.3303, 167.1909, 84.5920},
 'nearest_murbain': [2529.7020, 2495.9674, 2497.1525, 2670.5299],
 'nearest_iexterne': [21.4467, 33.0651, 32.6521, 18.8371],
}

df = pd.DataFrame(data)

def my_constraint(a, b, c, d, e, f):
  if min(a, b, c, d, e, f) <= 10:
    return True

problem = constraint.Problem()

# create variables
problem.addVariable("nearest_rev", df["nearest_rev"].tolist())
problem.addVariable("nearest_rvertes", df["nearest_rvertes"].tolist())
problem.addVariable("nearest_murbain", df["nearest_murbain"].tolist())
problem.addVariable("nearest_fountain", df["nearest_fountain"].tolist())
problem.addVariable("nearest_iexterne", df["nearest_iexterne"].tolist())
problem.addVariable("nearest_pcyclable", df["nearest_pcyclable"].tolist())

# create constraint
variables = ["nearest_rev", 
             "nearest_fountain", 
             "nearest_rvertes", 
             "nearest_pcyclable", 
             "nearest_murbain", 
             "nearest_iexterne"]

problem.addConstraint(my_constraint, variables)

solutions = problem.getSolutions()

print(solutions) # return 2048 (what wrong?)
Adil Blanco
  • 616
  • 2
  • 6
  • 23

0 Answers0