Optaplanner: check if chained planning variable has anchor

313 views Asked by At

I'm modifying the vehicle routing Optaplanner example. Vehicles are replaced with individuals who have to travel around the city, but they can do so using different modes of transportation. So I have an attribute on the anchor (Vehicle in the example, Employee in my modified code) called modeOfTransportation.

When calculating the arrival time using the custom shadow variable listener from the example, I want to take the mode of transportation into account of course. But, when Optaplanner starts initialising my planning entities (consumers), it seems that they at first are not connected to an anchor. So I can't get my mode of transportation, and everything breaks down.

Any ideas on how to proceed?

Below is what I want to accomplish. shadowVisit is my planning entity, shadowVisit.getEmployee() should give me the anchor. Doing a shadowVisit.getEmployee()==null check seems to hang the entire solving process.

arrivalTime = 
previousStopDepartureTime.plus(
shadowVisit.getLocation().getDistanceFrom(
shadowVisit.getPreviousStop().getLocation(), shadowVisit.getEmployee().getModeOfTransportation())
2

There are 2 answers

4
Geoffrey De Smet On

That's strange, because the chain principles guarantee that every chain has an anchor (see below).

Maybe your @CustomShadowVariable's sources attribute doesn't include the anchor shadow var, and your custom variable listener is called before the anchor shadow variable listener is called. OptaPlanner guarantees that it will call one type of variable listener for all domain classes before calling the next type. The order of those types of variable listeners is determined by that sources attribute (see second image).

enter image description here

enter image description here

0
Magnus Våge On

OK, so I figured out what the issue was. My problem is overconstrained, and I implemented a dummy employee as the solution (see optaplanner: modifying vehicle routing to let customers be not served)

I had not set a modeOfTransportation for my dummy, causing the null pointers. Sometimes is just good to write down a problem, makes you think hard enough to solve it!

Thank you very much for your input Geoffrey