Adding conditional IntVar to Objective Function #3219
-
How can I add IntVar to my objective function only when some conditions are met? obj_int_vars, obj_int_coeffs = [], []
obj_bool_vars, obj_bool_coeffs = [], []
elephant_is_healthy = dict()
elephant_weight = dict()
num_elephants = 20
num_months = 10
for e in range(num_elephants):
for d in range(num_months):
# Assuming some months elephants are healthy, some months they are not
# Track if they are healthy with boolean
elephant_is_healthy[e,d] = model.NewBoolVar('elephant%i_%i' % (e, d))
# We also want to track elephants monthly weight.
elephant_weight[e,d] = model.NewIntVar('elephant%i_%i' % (e, d))
# Assuming there are some rules to update elephant_is_healthy and elephant_weight to
# determine if they are healthy and their monthly_height
# Now for some reason, I need to add the weight of ONLY healthy elephants into the objective function,
# how can I do this? Since I need to first find healthy elephants and then find their corresponding weight
for e in range(num_elephants):
for d in range(num_months):
obj_bool_vars.append(elephant_is_healthy[e,d])
obj_bool_coeffs.append(elephant_weight[e,d]) --> seems like this can only take a number, not an IntVar.
# How can I access this value into the objective function?
model.Minimize(
sum(obj_bool_vars[i] * obj_bool_coeffs[i]
for i in range(len(obj_bool_vars))) +
sum(obj_int_vars[i] * obj_int_coeffs[i]
for i in range(len(obj_int_vars)))
) It will throw an error that looks like Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Answered by
lperron
Apr 3, 2022
Replies: 1 comment
-
Please read https://github.com/google/or-tools/blob/stable/ortools/sat/docs/channeling.md |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
lperron
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please read https://github.com/google/or-tools/blob/stable/ortools/sat/docs/channeling.md