Solvers
Aniate provides solvers for routing and for decision processes, documented below, and a superpermutation solver, documented on its own page. All of them are installed with pip install aniate and imported with from aniate import cube, ncube, mdp, nmdp, superpermutations.
Superpermutations
The superpermutation solver finds short superpermutations, shortens existing ones, and reads and writes the Almanac's Detour format. The search is fastest with pip install 'aniate[fast]'.
from aniate import superpermutations
word = superpermutations(5) # 153 letters
superpermutations.check(word) # every window
superpermutations.improve(word) # shorter, if found
superpermutations.write(word, "w.detour")
superpermutations.read("W-0111.detour")
Inputs and outputs are on the Superpermutations page.
Routing: NCube and Cube
NCube and Cube solve the Travelling Thief Problem with Drone and its special cases, and they share the same inputs. Omitting the items gives the TSP-D, omitting the drone gives the TTP, and omitting both gives the TSP.
1. NCube
The neural accelerator, in which a trained network constructs the plan. It can be used with the published weights or trained on the user's own problems. It requires pip install 'aniate[neural]' and is available in Python only.
from aniate import ncube
nodes = [(0, 0), (4, 1), (5, 4), (1, 5), (2, 2), (3, 3)] # depot first
profits, weights = [50, 60, 70, 80, 40], [2, 3, 4, 5, 2]
ncube(nodes, profits, weights, drone=True) # published weights
net = ncube.train(30) # trained on problems with 30 customers
ncube(nodes, profits, weights, drone=True, network=net)
2. Cube
The metaheuristic, which searches over routes, packing and flights jointly. It runs for half a second per customer unless seconds is set, and it is available in Python and Julia.
from aniate import cube
nodes = [(0, 0), (4, 1), (5, 4), (1, 5), (2, 2), (3, 3)] # depot first
profits, weights = [50, 60, 70, 80, 40], [2, 3, 4, 5, 2]
print(cube(nodes)) # TSP: a truck
print(cube(nodes, drone=True)) # TSP-D: a truck and a drone
print(cube(nodes, profits, weights)) # TTP: a truck and a knapsack
print(cube(nodes, profits, weights, drone=True)) # TTP-D: all three
using Aniate
nodes = [(0, 0), (4, 1), (5, 4), (1, 5), (2, 2), (3, 3)] # depot first
profits, weights = [50, 60, 70, 80, 40], [2, 3, 4, 5, 2]
cube(nodes) # TSP: a truck
cube(nodes; drone=true) # TSP-D: a truck and a drone
cube(nodes, profits, weights) # TTP: a truck and a knapsack
cube(nodes, profits, weights; drone=true) # TTP-D: all three
cube.milp
The exact model of the paper, a mixed-integer linear programme solved with Gurobi. It is intended for small instances, and plan.gap reports the optimality gap, which is 0 when the plan is proved optimal. It requires pip install 'aniate[milp]' and is available in Python only. The licence included with the Gurobi package supports up to 15 customers, and fewer under an endurance limit. Larger instances require a full or academic Gurobi licence.
from aniate import cube
nodes = [(0, 0), (4, 1), (5, 4), (1, 5), (2, 2), (3, 3)] # depot first
profits, weights = [50, 60, 70, 80, 40], [2, 3, 4, 5, 2]
plan = cube.milp(nodes, profits, weights, drone=True)
print(plan)
Inputs
| Input | Default | Description |
|---|---|---|
nodes | required | (x, y) coordinates, with the depot first |
profits, weights | none | One value per customer, excluding the depot |
drone | off | Whether a drone operates from the truck |
endurance | unlimited | Maximum flight distance; setting it enables the drone |
capacity | half the total weight | Capacity of the truck |
rent | half the profit divided by the time of a plain tour | Cost per unit of time |
drone_speed | twice the speed of the empty truck | Setting it enables the drone |
dist | Euclidean | Distance matrix, with the depot first |
seconds | half a second per customer; one hour for cube.milp | Time limit for cube and cube.milp |
seed | 0 | Random seed for cube |
network | published weights | For ncube: a file, or the result of ncube.train |
Outputs
| Field | Description |
|---|---|
plan.route | The path of the truck, from the depot back to the depot |
plan.flights | Drone flights as (launch, customer, landing) |
plan.items | Customers whose item is collected |
plan.time | The time at which both vehicles have returned |
plan.profit | Total profit of the collected items |
plan.objective | Profit minus rent multiplied by time |
plan.gap | For cube.milp: the optimality gap in percent |
MDP
Finite Markov decision processes. Given states, actions, transition probabilities and rewards, the solver returns an optimal policy and its exact value.
from aniate import mdp
model = mdp(
states=range(5), actions=["left", "right"],
transition=lambda s, a: {max(s - 1, 0): 1.0} if a == "left" else {min(s + 1, 4): 0.8, s: 0.2},
reward=lambda s, a, s_next: 10.0 if s_next == 4 else -1.0,
gamma=0.9, terminal=[4], initial=0,
)
best = model.solve()
best.policy # the optimal action in each state
best.start_value # the exact value of the policy
NMDP
Non-Markovian decision processes, in which the reward depends on the history of the episode. Given a world, event labels and a rule over those events, the solver computes an optimal policy on the exact product model.
from aniate import nmdp, problems
world = problems.gridworld(rows=6, cols=11, goals=[(5, 9)], slip=0.1, gamma=0.97)
labels = nmdp.Labels(world).at("r0c1", "key").at("r5c9", "door")
rule = nmdp.Ordering(["key", "door"], violation_penalty=-2) # the key must precede the door
policy = nmdp(world, labels, rule).solve()
policy.step("r5c1") # the next action, given the history
policy.memory # the current memory state, by name