No pickup should happen if it is not meant to be Delivered

While solving Vehicle Routing problem using Graphhopper/jsprit, For example , we get the solution as follows

Pickup1 - job1
Pickup2 - job2
Pickup3 - job3
Delivery1 - job1
Delivery2 - job2
Pickup4 - job4
Delivery3 - job4 (at depot, with job3 still inside vehicle without being delivered). (Not ideal scenario right?)
So the problem i have with this solution is, It is going for Pickup of job4 eventhough - job3 is still in the Vehicle. My ultimate aim would be to go for pickup only when the vehicle is empty.Also If an equipment isn’t going to be delivered before another pickup then it shouldn’t have been picked up in the first place(For ex: job 3 in above example).

So any suggestions to help with the possible Hard type constraint to satisfy the above conditions? Please suggest.I will update any further details if needed.

related:

Hi jie31best ,

I checked that. I have followed the same code snippet solution as provided by grantm009 in that thread.

My Code snippets for hard type constraint are as follows.

static class NoPickupWhenDeliveryInTruck implements HardActivityConstraint {

	private StateManager stateManager;
	private StateId  stateId;

	NoPickupWhenDeliveryInTruck(StateManager stateManager) {
		this.stateManager = stateManager;
		this.stateId = this.stateManager.createStateId("currentVehicleLoad");
	}

	@Override
	public ConstraintsStatus fulfilled(JobInsertionContext jobInsertionContext, TourActivity prevAct,
			TourActivity newAct, TourActivity nextAct, double departureTimeAtPrevAct) {
          if((newAct instanceof PickupShipment) &&
		         (!(nextAct instanceof End) && 
		          !(nextAct instanceof PickupShipment))){
		    if(isDepot(newAct.getLocation().getId()))
		        return ConstraintsStatus.NOT_FULFILLED_BREAK;
		}
		return ConstraintsStatus.FULFILLED;

}

The solution that i still get is as follows
route | vehicle | activity | job | arrTime | endTime | costs |
±--------±---------------------±----------------------±----------------±----------------±----------------±----------------+
| 1 | 10427333 | start | - | undef | 300 | 0 |
| 1 | 10427333 | pickupShipment | 18 | 300 | 300 | 0 |
| 1 | 10427333 | pickupShipment | 15 | 300 | 300 | 0 |
| 1 | 10427333 | pickupShipment | 20 | 300 | 300 | 0 |
| 1 | 10427333 | pickupShipment | 12 | 300 | 300 | 0 |
| 1 | 10427333 | deliverShipment | 12 | 321 | 351 | 21 |
| 1 | 10427333 | deliverShipment | 15 | 374 | 404 | 44 |
| 1 | 10427333 | deliverShipment | 20 | 421 | 451 | 61 |
| 1 | 10427333 | pickupShipment | 32 | 470 | 500 | 80 |
| 1 | 10427333 | deliverShipment | 32 | 521 | 521 | 101 |
| 1 | 10427333 | pickupShipment | 17 | 521 | 521 | 101 |
| 1 | 10427333 | pickupShipment | 27 | 533 | 563 | 113 |
| 1 | 10427333 | pickupShipment | 4 | 574 | 574 | 124 |

If the arrTime & endTime is same, it means the vehicle is at depot as per the logic i have put.
As you can see above, after 4 pickup (18,15,20,12) at depot, It only delivered (12,15,20) and picked up (32 - on the way pickup -Not depot) and returned to the depot to deliver (32) with (18) still inside truck(Vehicle). Any Help would be appreciated.