-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeuristics.fs
104 lines (78 loc) · 4.98 KB
/
Heuristics.fs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
namespace HullSolver
open System
module Heuristics =
open DomainTypes
let private rnd = Random DateTime.Now.Millisecond
let private filteredVars q vars =
q |> List.map(fun (c, v) -> vars |> findVar v)
/// Heuristics for selecting constraint-variable pairs.
type Heuristics =
/// Selects a pseudo random pair.
static member Random (q: (Constraint * string) list) pairs (vars: Variable list) =
rnd.Next q.Length
/// Selects the first pair containing a dominant variable. Selects the first pair if no such is available.
static member DominantFirst (q: (Constraint * string) list) pairs (vars: Variable list) =
let l = q
|> List.map(fun (c, v) -> vars |> findVar v)
|> List.filter(fun v -> v.IsDominant)
if l.Length = 0 then
0
else
q |> List.findIndex(fun (c, v) -> v = l.Head.Name)
/// Selects the first pair containing a non-dominant variable. Selects the first pair if no such is available.
static member NonDominantFirst (q: (Constraint * string) list) pairs (vars: Variable list) =
let l = q
|> List.map(fun (c, v) -> vars |> findVar v)
|> List.filter(fun v -> not v.IsDominant)
if l.Length = 0 then
0
else
q |> List.findIndex(fun (c, v) -> v = l.Head.Name)
/// Selects the pair whose variable's domain has the highest right bound.
static member MaxRightCand (q: (Constraint * string) list) pairs (vars: Variable list) =
let max = filteredVars q vars
|> List.maxBy(fun item -> item.Domain.b)
q |> List.findIndex(fun (c, v) -> v = max.Name)
/// Selects the pair whose variable's domain has the lowest right bound.
static member MinRightCand (q: (Constraint * string) list) pairs (vars: Variable list) =
let min = filteredVars q vars
|> List.minBy(fun item -> item.Domain.b)
q |> List.findIndex(fun (c, v) -> v = min.Name)
/// Selects the pair whose variable's domain is the widest.
static member LargeIntervalFirst (q: (Constraint * string) list) pairs (vars: Variable list) =
let max = filteredVars q vars
|> List.maxBy(fun item -> item.Domain.Length)
q |> List.findIndex(fun (c, v) -> v = max.Name)
/// Selects the pair whose variable's domain is the smallest.
static member SmallIntervalFirst (q: (Constraint * string) list) pairs (vars: Variable list) =
let min = filteredVars q vars
|> List.minBy(fun item -> item.Domain.Length)
q |> List.findIndex(fun (c, v) -> v = min.Name)
/// Selects the pair whose variable's domain has been shrunk the most since the start of the algoritm.
static member ShrunkMostFirst (q: (Constraint * string) list) pairs (vars: Variable list) =
let min = filteredVars q vars
|> List.minBy(fun item -> item.OriginalDomain.Length / item.Domain.Length)
q |> List.findIndex(fun (c, v) -> v = min.Name)
/// Selects the pair whose variable's domain has been shrunk the least since the start of the algoritm.
static member ShrunkLeastFirst (q: (Constraint * string) list) pairs (vars: Variable list) =
let max = filteredVars q vars
|> List.maxBy(fun item -> item.OriginalDomain.Length / item.Domain.Length)
q |> List.findIndex(fun (c, v) -> v = max.Name)
/// Selects the pair whose variable is present in the largest number of pairs.
static member FailFirst (q: (Constraint * string) list) pairs (vars: Variable list) =
let max = filteredVars q vars
|> List.maxBy(fun item -> (pairs |> List.filter(fun (c, v) -> v = item.Name) |> List.length))
q |> List.findIndex(fun (c, v) -> v = max.Name)
/// Selects the first pair whose constraint uses addition.
static member PreferAdd (q: (Constraint * string) list) pairs (vars: Variable list) =
let constraints = q
|> List.filter(fun (c, v) -> c :? VarPlusVarEqVarConstraint)
if constraints.Length = 0 then 0 else q |> List.findIndex(fun (c, v) -> c :? VarPlusVarEqVarConstraint)
/// Selects the first pair whose constraint uses multiplication.
static member PreferMult (q: (Constraint * string) list) pairs (vars: Variable list) =
let constraints = q
|> List.filter(fun (c, v) -> c :? VarTimesVarEqVarConstraint)
if constraints.Length = 0 then 0 else q |> List.findIndex(fun (c, v) -> c :? VarTimesVarEqVarConstraint)
/// Selects the first pair (i.e. the one that has been in the queue the longest time).
static member Fifo (q: (Constraint * string) list) pairs (vars: Variable list) =
0