Skip to main content

optimal

Function optimal 

Source
pub fn optimal<V>(agents: &[V], tasks: usize, capacity: usize) -> Assignment
where V: Valuation<TaskId>,
Expand description

The assignment of tasks 0..tasks to agents, each holding at most capacity tasks, with the highest Assignment::value.

Tasks may stay unassigned. A task set’s value for an agent is the best total gain over every order in which the agent can build it, inserting each task where Valuation::best_insertion puts it. The search tries every build order of every task set for every agent, then every split of the tasks among the agents. Use it as a reference on small instances. Of equally good assignments, the first one found is returned.

With n tasks and capacity c, each agent explores the n! / (n - k)! build orders of every length k up to c. The split takes 3^n steps per agent. In an optimized build with three agents, eight tasks take tens of milliseconds and ten about a second.

§Panics

Panics when tasks exceeds MAX_OPTIMAL_TASKS.

§Examples

use task_allocation::{AgentId, TaskId, Valuation, centralized::{optimal, sequential_greedy}};

/// Values every task at `reward`, for at most one task.
struct Single { reward: [u64; 2] }

impl Valuation<TaskId> for Single {
    fn marginal_score(&self, path: &[TaskId], task: &TaskId) -> Option<u64> {
        path.is_empty().then_some(self.reward[task.index()])
    }
}

let agents = [Single { reward: [5, 0] }, Single { reward: [6, 5] }];

// Greedy gives task 0 to agent 1, and agent 0 gains nothing from task 1.
assert_eq!(sequential_greedy(&agents, 2, 1).value(), 6);

// The optimum gives task 0 to agent 0 and task 1 to agent 1.
let best = optimal(&agents, 2, 1);
assert_eq!(best.value(), 10);
assert_eq!(best.holder(TaskId(0)), Some(AgentId(0)));