by Tom Cools
🎯 Optimize goals
📦 with limited resources
⚖️ under constraints
🎯 Goal: Minimize travel time!
📦 Resources: 1 (magical) sleigh!
⚖️ Constraints: Santa things!
var remaining = new ArrayList<>(allVisits); //copy
var visitOrder = new ArrayList<>();
Visit latest = remaining.getFirst();
visitOrder.add(latest);
remaining.remove(latest);
int remainingSize = remaining.size();
for (int i = 0; i < remainingSize-1; i++) {
Visit closestVisit = findClosest(latest, remaining);
visitOrder.add(closestVisit);
remaining.remove(closestVisit);
latest = closestVisit;
}
| Greedy | ||
|---|---|---|
| Fast | ||
| Scalable | ||
| Far from optimal |
Search space for n 🎁: n!
Search space for 5 🎁: 120
Search space for 10 🎁: 3628800
Search space for 100 🎁: 10157
Search space for 1000 🎁: 102567
Search space for 1000 🎁: 102567
Atoms in the observable universe: 1080
10^2567: 2487 more zeroes
| Greedy | Brute Force | |
|---|---|---|
| Fast | Slow | |
| Scalable | Not scalable | |
| Far from optimal | Optimal |
| Humans | Greedy | Brute Force |
|---|---|---|
| Slow | Fast | Slow |
| ? | Scalable | Not scalable |
| Far from optimal | Far from optimal | Optimal |
or math.. probably math.
Group of techniques to find the ideal inputs from a set
in order to maximize or minimize a real function.
Planning optimization made easy
public HardSoftLongScore calculateScore(VehicleRoutePlan plan) {
long totalDrivingTime = 0;
for(Vehicle v : plan.getVehicles()) {
totalDrivingTime += v.getTotalDrivingTimeSeconds();
}
return HardSoftLongScore.of(0, totalDrivingTime*-1);
}
Check the Timefold log:
log08:27:00.867 INFO Solving started: ...
08:27:02.528 INFO Construction Heuristic ended: ...
move evaluation speed (39580/sec) ...
08:27:21.313 INFO Local Search ended: ...
move evaluation speed (101701/sec) ...
08:27:21.317 INFO Solving ended: ...
move evaluation speed (101461/sec) ...
It looks at 100 000 solutions per second
| Greedy | Brute Force | Metaheuristics |
|---|---|---|
| Fast | Slow | Fast |
| Scalable | Not scalable | Scalable |
| Far from optimal | Optimal | (Near) Optimal |
Constraint minimizeTravelTime(ConstraintFactory factory) {
return factory.forEach(Vehicle.class)
.penalizeLong(HardSoftLongScore.ONE_SOFT,
Vehicle::getTotalDrivingTimeSeconds)
.asConstraint("Minimize Travel Time");
}
Constraint vehicleCapacity(ConstraintFactory factory) {
return factory.forEach(Visit.class)
.filter(visit -> visit.getVehicle() != null)
.groupBy(Visit::getVehicle, sum(Visit::giftAmount))
.filter((v, sum) -> sum > v.getCapacity())
.penalizeLong(HardSoftLongScore.ONE_HARD,
(v, sum) -> sum - v.getCapacity())
.asConstraint("Vehicle Capacity");
}
Adding one new package 🎁
Only the impacted 🎁 are recalculated
🎯 Goal: Minimize travel time! ✅
📦 Resources: 1 or more Santas! ✅
⚖️ Constraints: Capacity, Time Windows ✅
| Greedy | Brute Force | Metaheuristics (Timefold) |
|---|---|---|
| Fast | Slow | Fast |
| Scalable | Not scalable | Scalable |
| Far from optimal | Optimal | (Near) Optimal |
Can't solve these kinds of problems
but can be used to write Timefold code.
Quickstart: Food Packaging
Quickstart: Employee Scheduling
Quickstart: Vehicle Routing
| Learn more | solver.timefold.ai |
|---|---|
| Get started |
|
| Feedback |
Add me on socials! 🙏 Tom Cools: (@tomcools.be) |