-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.jl
120 lines (94 loc) · 3.06 KB
/
run.jl
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using LinearAlgebra, Random
using TenSolver
using JuMP, Gurobi, IsingSolvers
using BenchmarkTools
if haskey(ENV, "SLURM_JOB_PARTITION") && ENV["SLURM_JOB_PARTITION"] == "gpu"
const MODE = :gpu
using CUDA, cuTENSOR
CUDA.set_runtime_version!(v"12.0.1")
@info "CUDA mode"
else
const MODE = :cpu
@info "BLAS mode"
end
function read_rudy(T::Type, filename::String)
# Read the file and parse each line
data, cte = open(filename, "r") do file
lines = readlines(file)
# This is the template
# line row value
# # Constant term of objective = <value>
([parse.(T, split(line)) for line in lines if line[1] != '#']
, parse(T, last(split(lines[2], "=")))
)
end
# Determine the size of the matrix
max_i = Int(maximum(x -> x[1], data))
max_j = Int(maximum(x -> x[2], data))
# Initialize the matrix with zeros
A = zeros(T, max_i+1, max_j+1)
# Populate the matrix with the given values
for (i, j, value) in data
A[Int(i)+1, Int(j)+1] = value
end
return A, cte
end
read_rudy(fn::String) = read_rudy(Float32, fn)
function solve_vrp(n::Int)
fname = "vrp-as-qubo/TestSet/test_pb_$(n)_o.rudy"
Q, cte = read_rudy(fname)
# Regularization for Kernel
Q += convert(typeof(Q), 1e-8 * diagm(one(cte):n))
E, psi = TenSolver.solve(Q, nothing, cte
, device = MODE == :gpu ? CUDA.cu : identity
; eigsolve_krylovdim = 3
, eigsolve_verbosity = 3
, eigsolve_tol = 1e-8
, noise = [1E-4, 1E-7, 1E-8, 0.0]
, nsweeps = 5
)
x = TenSolver.sample(psi)
obj = dot(x, Q, x) + cte
@info n, E, obj
return obj, x
end
function solve_mip(n::Int)
fname = "vrp-as-qubo/TestSet/test_pb_$(n)_o.rudy"
Q, c = read_rudy(fname)
m = Model(Gurobi.Optimizer)
# Alternative using an Open Source Solver
# m = Model(IsingSolvers.ILP.Optimizer)
# set_attribute(m, "mip_solver", HiGHS.Optimizer)
@variable m x[1:n] Bin
@objective m Min dot(x, Q, x) + c
optimize!(m)
return objective_value(m)
end
function main()
@info Sys.CPU_THREADS BLAS.get_num_threads()
println("cores = $(Sys.CPU_THREADS)")
println("blas thr = $(BLAS.get_num_threads())")
JOBID = haskey(ENV, "SLURM_JOBID") ? ENV["SLURM_JOBID"] : "local"
open("result-$(MODE)-$(JOBID).csv", write = true) do io
write(io, "var,obj,time\n")
end
for n in [10, 27, 49, 96, 217, 262, 541, 794]
println("Solving for $(n) variables")
try
# This is a hack to store both the elapsed time and result of running the solver.
# Unfortunately, BenchmarkTools has no equivalent to @timed
global ref = Ref{Any}()
trial = @benchmark(x = solve_vrp($(n)), setup = (x = nothing), teardown = (ref[] = x))
dt = median(trial)
E, x = ref[]
@info "Trial" n trial dt
println("Trial $(n), $(dt)")
open("result-$(MODE)-$(JOBID).csv", write = true, append = true) do io
write(io, "$(n),$(E),$(dt)\n")
end
catch err
@error "ERROR: " exception=(err, catch_backtrace())
end
end
end
main()