-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocator.ts
68 lines (60 loc) · 2.03 KB
/
allocator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { chooseInt, chooseRandom } from "./helpers";
import { OptimisationProblem, Solution } from "./optimiser";
// step 2 - allocator
interface Resource {
[propname: string]: any
}
abstract class ResourceSet {
abstract select(
conditions: (r: Resource) => boolean
): Resource;
}
class Allocation extends Solution {
constructor(
public assignment: Map<string, Resource[]>,
public evaluator: (A: Allocation) => number
) {
super();
}
evaluate(): number {
return this.evaluator(this);
}
}
interface NamedRequirements {
name: string,
requirements: [ResourceSet, /*conditions*/ (r: Resource) => boolean][]
}
class AllocationProblem extends OptimisationProblem {
constructor(
public namedRequirementsList: NamedRequirements[],
public evaluator: (a: Allocation) => number
) {
super();
}
random(): Allocation {
const assignment = new Map();
for (const n of this.namedRequirementsList) {
const assigned = [];
for (const [set, conditions] of n.requirements) {
assigned.push(set.select(conditions));
}
assignment.set(n.name, assigned);
}
return new Allocation(assignment, this.evaluator);
}
neighbour(s: Allocation): Allocation {
const new_map = new Map<string, Resource[]>();
for (const [name, resourceList] of s.assignment) {
const new_resourceList = JSON.parse(JSON.stringify(resourceList));
new_map.set(name, new_resourceList);
}
const change_this = chooseRandom(this.namedRequirementsList);
const change_idx = chooseInt(change_this.requirements.length);
new_map.get(change_this.name)![change_idx] = change_this.requirements[change_idx][0].select(change_this.requirements[change_idx][1])
const new_s: Allocation = new Allocation(new_map, s.evaluator);
return new_s;
}
}
export {
Resource, ResourceSet, Allocation, NamedRequirements, AllocationProblem
}