## Unlimited vehicles in VRP - optaplanner

How to allow Optaplanner to use an unlimited or dynamic number of vehicles in the VRP problem?
The number of vehicles is minimized during score calculation, as each vehicle has a base cost. The solver should initialize as many vehicles as it thinks it is comvenient
#PlanningEntityCollectionProperty
#ValueRangeProvider(id = "vehicleRange")
public List<Vehicle> getVehicleList() {
return vehicleList;
}
Currently I just initialize the vehicle list with a predefined number of vehicles, such as 100 000, but I am not sure about the performance implications of that, as the search space is much bigger than necessary.

Out-of-the-box, this is the only way. You figure out the minimum maximum number of vehicles for a dataset and use that to determine the number of vehicles. For one, the minimum maximum number of vehicles is never bigger than the number of visits. But usually you can prove it to be far less than that.
That being said, the OptaPlanner architecture does support Move's that create or delete Vehicles, at least in theory. No out-of-the-box moves do that, so you'd need to build custom moves to do that - and it will get complex fast. One day we intend to support generic create/delete moves out-of-the-box.

## Related

### Optaplanner - VRP number of vehicles optimization

How to optimize on number of vehicles utilized and optimize on vehicles with best fit for CVRP, with no Timewindow constraints. For example -- I am running 10990 Kg load with 15 vehicles (5 vehicles each of capacities 3000Kg, 750Kg and 7500Kg). I have disabled rule for distanceFromLastCustomerToDepot. When I run it with optaplanner examples as is, it chooses, 3 vehicles of 7500 kgs each. Since load is 10990Kg, I expect it to fit in 2 vehicles with 7500kg or may be with 3 vehicles of 7500 + 3000 + 750? How to optimize on this along with distance traveled?

Add a hard constraint or a heavy weighted soft constraint that penalize the number of vehicles used. That being said, there is research that suggests that in some case, even with that constraint, local search can have trouble cutting down the number of vehicles, especially that last vehicle. Custom, course-grained moves should overcome that decently. But in practice, for convenience, people often just run a second solver which enforces one vehicle less by simply having less vehicles to start with.

### Hard score calculation in vehicle routing

Currently using OptaPlanner for calculating score in a CVRP problem. if (demand > capacity) hardScore -= (demand - capacity); If there is a heterogeneous fleet, how can I calculate a hard score? I want to use a vehicle with small capacity if number of demand is less than the vehicle assigned by OptaPlanner.

Don't mix 2 constraints. These are 2 different constraints: Each vehicle must have enough capacity (usually a hard constraint) - already implemented in the OptaPlanner example. Prefer using smaller vehicles over bigger ones (usually a soft constraint). Normally there's a price per km per vehicle type, so this factors in the distance driven too in the soft score penalty. Just implement the second constraint, starting from the OptaPlanner VRP example.

### Vehicle Routing Optimization with unspecified capacity of different items

I have a little different variant of vehicle route optimization problem. There are different items which are to be dropped at different stores. Each store require N items of amounts [a1, a2, ...., aN]. As we don't know how many stores will be there on a route beforehand. So how do we decide how much amount of a particular item to put in the vehicle? Or should I first just put random number of the different items in the vehicle and then use the capacity constraint. Please provide any link to research papers or blog which addresses this kind of problem.

No knowledge of how many stores will be on route beforehand: that's real-time planning or continuous planning (see OptaPlanner docs for both concepts): just replan when the vehicles are already driving. As for predicting how many items to take along originally - that's predictive analytics. A poor man's implementation just fills the trucks up to their full capacity and optionally adds a soft constraint to spread the load across trucks and a soft constraint to avoid that the trucks all end up in the same region (so if new locations are added, it's likely there's a non-empty truck nearby). A rich man's implementations does probability and/or uncertainty calculations (think Bayes etc), but that's probably overkill.

### combinatorial optimization: multiple upgrade paths with inventory constraints

I'm playing a video game, and i want to make a program that calculates the globally optimal build/upgrade path towards a fixed 6 item goal. Time, Cost, Inventory constraints, and effectiveness (short/mid/long-term) ratings are to be considered. Identifying local spikes in effectiveness are also welcomed, but optional. I don't know how to classify this problem, but i'm guessing its a type of graph search. The fact that multiple criteria are being optimized is making things confusing for me. Problem details: There are 6 free slots in your bag to hold items. There are 2 classes of items: basic items, and composite items. Composite items are built/merged from basic items, and other composite items. If you have enough gold, you can buy a composite item, and its missing sub components, all at once, using only 1 inventory slot. The build path for various composite items are fixed, and many basic components are featured in more than one recipe. Gold is earned at a fixed rate over time, as well as in small non-deterministic bursts. Time is bounded: it increments in fixed ticks (seconds) and has a max value: 2400. There exists no more than 50 items, maybe less. So, thinking about the problem... Tackling the gold/time issue first We can either ignore the non-deterministic aspect, or use some statistical averages. Let's make life easy, and ignore it for now. Since gold, and time, are now directly related in our simplified version, they can be logically merged. Combinatorial expansion of feasible paths A graph could be built, top down, from each of the 6 goal items, indicating their individual upgrade hierarchies. Components that are shared between the various hierarchies can be connected, giving branch decisions. The edges between components can be weighted by their cost. At this point, it sounds like a shortest path problem, except with multiple parallel and overlapping goals. Now the question is: how do inventory constraints play into this? The inventory/cost constraints, add a context, that both disables (no free slots; not enough gold), and enables (two items merged freeing a slot) various branch decisions, based upon previous choices and elapsed time. Also, the possibility of saving up gold and doing nothing for a non fixed period, can be optimal in certain situations. How does one expand all the feasible possibilities? Does it have to be done at every given step? How many total combinations are there? Does this fall under topological combinatorics? Update: Q: How does one expand all the feasible possibilities? The item build path is a dependency graph. A correct evaluation order of the dependencies is given by the topological ordering of the graph. A graph may have more than one valid evaluation order. http://en.wikipedia.org/wiki/Dependency_graph http://en.wikipedia.org/wiki/Topological_sort https://mathematica.stackexchange.com/questions/32400/all-possible-topological-orderings-of-a-graph/32417#32417 Update 2: Q: How many total combinations are there? Seems that it has to be counted, and there is no numerical formula. Algorithim 3.2, Page 150, "On Computing the Number of Topological Orderings of a Directed Acyclic Graph" by Wing-Nig Li, Zhichun Xiao, Gordon Beavers Pseudo-Code: f(g) | vertex_count(g) == 1 = 1 f(g) = ∑ {f(g \ {v}) for all v ∈ deg0set} deg0set = {vertex_in_degree(g,x) == 0 for all x ∈ vertices(g)} Mathematica-Code: f[g_/; Length[VertexList[g]] == 1] := 1 f[g_] := With[ {deg0set = Select[VertexList[g], VertexInDegree[g,#] == 0&]}, Sum[f[VertexDelete[g,v]], {v,deg0set}] ] Rating Effectiveness If the above expansion produces less than a few billion possibilities, we can just exhaustive search using OpenCL/CUDA. I'm not sure what other options are available, since most graph search stuff seems to just solve for one criteria.

You need to walk down to establish max values at each end stage and element, (ie add your metric (dps or whatever) up as you walk down, Pruning any combinations that require you to have more slots then possible. Then walk backwards from the best sets of possible end options, (which should be fairly sparse), you should be able to limit it to only the best composite options), and always move in a way that takes you to the max. Then set a value equal to damage on each option, Given that dps or whatever of basic and preliminary composite items is probably less then the final possible composite items, this will be naturally weighted towards effectiveness at the end stage, so you may want to adjust, or set a minimum. Once you have that, you can figure out the gold costs for your options, and decide how to weight getting to things faster. Then you can go through and establish gold costs. You need to decide if you want to wait and earn or not. If not, then you want to measure the rate of dps increase as a function of time. If so, I expect it's cheaper to buy the items after not playing, over all. You are going to have to use your data and make some value judgements.

### VRP variant, how to modify provided VRP example

I'm new to optaplanner and im trying to tweak the VRP example to solve a "similar" problem... but with some differences: We have a central depot, n customers and a fleet of one or more vehicles. Each customer has a certain "maximum capacity" (not the same for all of them). Same for each vehicle. Initial conditions from the problem include: "actual capacity" and "desired capacity" for each customer, so that: actual capacity is >=0 and <=max capacity desired capacity is >=0 and <=max capacity "actual depot capacity" is the amount of items available at the depot We want to REDISTRIBUTE some items so that each customer gets his "desired capacity". (Tipically most items will be redistributed from one customer to another) Hard constraints are "maximum capacity"s cannot be excedeed Soft constraints are vehicle distance (minimize) and difference from "desired capacity" (minimize) I started by generating a data file for the VRP example with negative customer demand (to simulate desired capacity < actual capacity) but quickly discover that constraint in drl that sums up all customers demands to satisfy vehicle capacity restriction is not what we need here. ¿Do you think that this problem is "similar enough" to worth modifying VRP example?

Yes, it's definitely worth taking the OptaPlanner VRP example (with or without the example GUI) and customizing it to these needs. Start from OptaPlanner 6.0.0.CR1 or better yet CR4 which will be released next week normally. Don't use CR3 because it might have a bug related to shadow variables in VRP. A) Let's presume customerDemand = customerDesired - customerActual. This can be positive (need more items) and negative (need to get rid of items). Question: How do we validate the Vehicle capacity? We can't just sum it, because it fluctuates from customer to customer. Answer: We use a shadow variable (let's call it freight) on Customer to calculate how many items the vehicle is transporting from the previous customer to the current. This is very similar to how arrivalTime works for the TimewindowedCustomer example. Then use a score constraint rule to verify freight on every customer is lower than the vehicle's maximum capacity. And also that freight is >= 0 at every customer. Now, A) works well if there are plenty of vehicles. Even if there aren't enough items, checking the soft constraint is pointless because it's a fixed constant number (the number of items lacking). You could even decide to spread that shortage fairly over the customers and adjust their customerDesired accordingly, before scheduling. B) However, if there aren't plenty of vehicles, relative to the number of customers, then A) might be insufficient. We 'll want to allow to deliver/pickup less than customerDemand, making it more flexible (and therefore complex :). In this case, a Customer has 2 genuine (=non-shadow) planning variables (instead of just 1): the previousStandstill (~ previous customer) and the deliveryPickup (<= demand * 2).