0

I want to multiply the range of each variable because I can't use more than 5 kg and 50 euros so I multiply the weight of each product and its value but instead, the program returns me an error that it's taking the value an instead of the range.

from constraint import *

problem = Problem()

problem.addVariable("a",range(0,51))
problem.addVariable("b",range(0,51))
problem.addVariable("c",range(0,11))
problem.addVariable("d",range(0,6))
problem.addConstraint(MaxSumConstraint(5000),['a'*340, 'b'*120,'c'*105,'d'*300])
problem.addConstraint(MaxSumConstraint(50),['a'*2,'b','c'*4,'d'*5])



soluciones = problem.getSolutions()


for solucion in soluciones:
    solucion_string = ""
    for i in range(4):
        solucion_string += "("+str(i)+","+str(solucion[i])+")"
    print(solucion_string)

print(len(soluciones))

I want to use the value of the range of each variable and multiply it.

Sidou Gmr
  • 138
  • 8

1 Answers1

0

I'm not sure I understand what you're trying for, but if I understand it right, you're using a smaller range and want to multiple the meaning of that variable in the end.

Would it work better for you do your multiplication at the end in your solution printing method, and instead adjust downward your MaxSumConstraint conditions.

What you're doing here won't work:

['a'*340, 'b'*120,'c'*105,'d'*300]

Those aren't integer variables, they're literally just the string 'a' and so on, so you can't multiply it there.

If you need them to be affected earlier, you'd need to perhaps make your own constraint function that took the multiplication into account.


I'm not sure of the goal of this bit of code, but if I understand it right, could it be possible to use something like this instead:

def func1(a, b, c, d):
    return (a * 340 + b * 120 + c * 105 + d * 300) <= 5000


def func2(a, b, c, d):
    return (a * 2 + b + c * 4 + d * 5) <= 50

Which you would call like this:

problem.addConstraint(func1, variables)
problem.addConstraint(func2, variables)
hoshisabi
  • 1
  • 2