public boolean shouldFinish() { for (Order order : orders) { if (!order.isDelivered()) return false; } return true; }
private ArrayList<State> extractDeliveredStates() { ArrayList<State> states = new ArrayList<>(); boolean shouldAddDeliveredTransitions = false; for (Order order : orders) { /** * If the destination of this order is in the current state then we need to consider this * order. We might need to deliver this order here We can only delivery this order on this * node if the order was previously picked up and it was not delivered */ if (order.getDestination().equals(currentNode) && order.isPickedUp() && !order.isDelivered()) shouldAddDeliveredTransitions = true; } if (shouldAddDeliveredTransitions) { State deliveredState = deliverAction(); states.add(deliveredState); } return states; }
private ArrayList<State> extractPickupTransitions() { ArrayList<State> states = new ArrayList<>(); int totalVolume = 0; for (Order order : orders) if (order.isPickedUp() && !order.isDelivered()) totalVolume += order.getVolume(); if (totalVolume < truck.getTruckCapacity()) { orders .stream() .filter( order -> order.getLocation().equals(currentNode) && !order.isPickedUp() && !order.isDelivered()) .forEach( order -> { State pickupState = pickupAction(order); states.add(pickupState); }); } return states; }
private State deliverAction() { ArrayList<Order> orders = new ArrayList<>(); Truck newTruck = new Truck( this.truck.getID(), this.truck.getCurrentNode(), this.truck.getTruckCapacity(), this.truck.getOrders()); int count = 0; for (Order order : this.orders) if (order.getDestination().equals(this.truck.getCurrentNode()) && !order.isDelivered() && order.isPickedUp()) { orders.add( new Order( order.getLocation(), order.getDestination(), true, true, this.truck.getCurrentNode(), order.getId(), order.getVolume())); count++; } else orders.add( new Order( order.getLocation(), order.getDestination(), order.isPickedUp(), order.isDelivered(), order.getDroppedIn(), order.getId(), order.getVolume())); return new State( orders, this.gScore, newTruck, this, "Delivered " + String.valueOf(count) + " orders at " + newTruck.getCurrentNode().getId()); }
private State pickupAction(Order order) { ArrayList<Order> orders = new ArrayList<>(); Truck newTruck = new Truck( this.truck.getID(), this.truck.getCurrentNode(), this.truck.getTruckCapacity(), this.truck.getOrders()); for (Order tmpOrder : this.orders) { if (order.getId() == tmpOrder.getId()) orders.add( new Order( tmpOrder.getLocation(), tmpOrder.getDestination(), true, false, null, tmpOrder.getId(), tmpOrder.getVolume())); else orders.add( new Order( tmpOrder.getLocation(), tmpOrder.getDestination(), tmpOrder.isPickedUp(), tmpOrder.isDelivered(), tmpOrder.getDroppedIn(), tmpOrder.getId(), tmpOrder.getVolume())); } return new State( orders, this.gScore, newTruck, this, "Picked up an order with id " + order.getId() + ". Node is " + newTruck.getCurrentNode().getId()); }