-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.nims
executable file
·83 lines (76 loc) · 2.24 KB
/
solve.nims
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
#!/usr/bin/env -S nim --hints:off
# Pass an intermediate solution that uses indices to stdin
import strutils, strformat, strscans, random, math
const
nbTimeslots = 150
solver = "'org.ortools.ortools'"
data = "data/big.dzn"
# Starting Percentage of numbers to search for
startingPercentage = 0.10
# Increases after `loops` loops
loops = 50
# By `increment`
increment = 0.01
let
input = slurp(data).splitLines[23]
start = find(input, '[')
finish = find(input, ']')
typesTxt = input[start + 1 .. finish - 1].split(", ")
var types = newSeq[int]()
for n in typesTxt:
types.add(parseInt(n))
proc parse(s: string): tuple[s: seq[int], o: int] =
let
lines = s.splitLines
solution = lines[0]
start = find(solution, '[')
finish = find(solution, ']')
numbers = solution[start + 1 .. finish - 1].split(", ")
var obj: int
discard scanf(lines[1], "obj = $i", result.o)
for n in numbers:
result.s.add(parseInt(n))
proc compose(s: seq[int], o: int): string =
result = &"solutionI = [{s.join(\", \")}];\nobjI = {o};"
var
(solution, obj) = parse(readAllFromStdin())
iterations = 1
newObj: int
percentage = startingPercentage
solutionC = solution
iterator randomIndices(p: float): tuple[f, s: int] =
let nrand = round(p * nbTimeslots.float).int
var
indices = newSeq[int]()
a, b: int
for i in 0 ..< nbTimeslots:
indices.add(i)
while indices.len > nbTimeslots - nrand:
var r = rand(indices.len - 1)
a = indices[r]
indices.delete(r)
b = a
while solutionC[b] != -1 and solutionC[a] != -1 and types[solutionC[b]] == types[solutionC[a]]:
r = rand(indices.len - 1)
b = indices[r]
indices.delete(r)
yield (a, b)
while true:
if iterations > loops:
iterations = 0
percentage += increment
for (f, s) in randomIndices(percentage):
solution[f] = -2
solution[s] = -2
let cmd = &"minizinc production-planning-lns.mzn -d {data} " &
&"-D \"{compose(solution, obj)}\" --solver {solver} -p8"
let output = gorge(cmd)
echo output
echo &"using {int(100*percentage)}% of variables"
echo &"{iterations} iterations without new result"
(solution, newObj) = parse(output)
if newObj == obj:
inc iterations
else:
iterations = 0
obj = newObj