I tried to use optaplanner for the first time (quite straightforward by the way, help is very complete, and error message are very explicit). My problem is simple: allocate rooms to teams, depending on their size. I want to fill all the rooms, and I may have much more people in teams than available space.
My problem is that obviously all teams can't be allocated. And I wonder how to represent this.
At first , I used a HARD_SOFT score. Difference between available spaces and allocated teams was giving me a hardscore. With that simple implementation, solver keeps running, allocating all teams, even if breaking hard constraints.
// Loop on all rooms
for (Room currentRoom : currentAllocation.getRoomList()){
int requiredSize = 0;
// Cumulated sum of allocated people required size
for (People currentPeople : currentAllocation.getPeopleList()){
if (currentRoom.equals(currentPeople.getRoom())){
requiredSize += currentPeople.get_requiredSize();
}
}
// Check if max capacity is respected or not for current room
if (requiredSize > currentRoom.getSize()){
// Max capacity is not respected
hardScore += (currentRoom.getSize()-requiredSize);
}
}
Then, I added one more artificial room (room 0). When teams are allocated to that room, it degrades my soft score. The other rooms is tracked in hardscore.
for (Room currentRoom : currentAllocation.getRoomList()){
int requiredSize = 0;
// Cumulated sum of allocated people required size
for (People currentPeople : currentAllocation.getPeopleList()){
if (currentRoom.equals(currentPeople.getRoom())){
requiredSize += currentPeople.get_requiredSize();
}
}
// Handle case of room zero
if (currentRoom == currentAllocation.getRoomList().get(0)){
softScore = -requiredSize;
}
else{
// Check if max capacity is respected or not for current room
if (requiredSize > currentRoom.getSize()){
// Max capacity is not respected
hardScore += (currentRoom.getSize()-requiredSize);
}
}
}
Now, solver can be stopped as soon as hard score reaches 0. While soft score is there to ensure that we try to decrease left spaces. And it is easy to find out unallocated teams in final solution (the one that are allocated to room 0). Is it the correct way to proceed ?
Many thanks for your help !