I have an optimization problem in my hand and i want to add some constraints
maa_count = LpVariable("ManAtArmsCount", int(archer_count) - 1, None, LpInteger)
archer_count = LpVariable("ArcherCount", int(mangonel_count) 1 , int(maa_count) - 1, LpInteger)
mangonel_count = LpVariable("MangonelCount", int(cavalry_count) 1, int(archer_count) - 1, LpInteger)
cavalry_count = LpVariable("CavalryCount", 0, int(mangonel_count) - 1, LpInteger)
army_count = archer_count maa_count cavalry_count mangonel_count
This code results in TypeError: int() argument must be a string, a bytes-like object or a number, not 'LpVariable'
I tried to define variables with standart bounds and add a constraint with
prob = maa_count > archer_count > mangonel_count > cavalry_count
but this resulted in a type error stating that > operator cannot be used between lpvariables.
How can i fix this?
CodePudding user response:
Break up what you are trying to do. You cannot reference a variable as a part of the construct of another variable. The correct thing to do is to put upper/lower bounds (if it makes sense in the context of the problem) in the construction and then state any further relationships in constraints. For instance if I want 2 integer variables and I want y
to be greater than x
, I just need to state that relationship in a constraint. Also, do not cast things as int()
... just declare the variable as an integer type. As such:
import pulp
prob = pulp.LpProblem('example', pulp.LpMinimize)
x = pulp.LpVariable('x', lowBound=0, cat=pulp.LpInteger)
y = pulp.LpVariable('y', lowBound=0, cat=pulp.LpInteger)
# state the relationship of the variables in a linear constraint... And add it to the problem
prob = y >= x
print(prob)
Yields:
example:
MINIMIZE
None
SUBJECT TO
_C1: - x y >= 0
VARIABLES
0 <= x Integer
0 <= y Integer