Reputation:
I need to implement an if-statement as a constraint. But, my code continuously gives an error.
'open_house[i]' is a binary variable (1 means 'open' and 0 means 'not open').
'people_to_house[j,i]' is also a binary variable (if person-j is assigned to house-i, then it is 1, otherwise 0)
I want to make 'open_house[i]' equal to 1, if (in every house) the sum of the number of people who are assigned to that house is at least 1. In other words, if there is at least 1 person who is assigned to a house, then we decide to open that house.
The following AMPL code gives me a syntax error. How do you write if-then statements within 'subject to'?
subject to
if (open {i in house}: sum {j in people} people_to_house[j,i] >= 1) then open_house[i] = 1;
Upvotes: 0
Views: 425
Reputation: 5930
You don't need if-then-else for this. You can just require
people_to_house[j,i] <= open_house[i]
for all j
and i
. This will force open_house[i]
to 1 as soon as a person is assigned to house i
. If the number of people to be assigned potentially is not too big you can also formulate this as a big-M constraint:
sum { j in people } people_to_house[j,i] <= M * open_house[i]
(where M
is the number of people, i.e., sum { j in people } 1
)
Upvotes: 1