-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_compute_true_solutions.py
58 lines (51 loc) · 1.83 KB
/
benchmark_compute_true_solutions.py
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
import numpy as np
from benchmark_optimization_problems import *
from benchmark_solver import *
def compute_true_optimal_solutions(problems, verbose=True):
problem_names = [p.name for p in problems]
optimal_solutions = dict()
optimal_objectives = dict()
# solve true problems and save values
for _, problem in enumerate(problems):
if verbose:
print("Solving " + problem.name)
program = StochasticTrueAverageValueProgram(problem)
# solve stochastic optimization problem
# (without SAA approximation)
solver = Solver(program=program)
x_sol, obj_sol, status_sol = solver.solve()
if status_sol != SolverReturnStatus.Solve_Succeeded:
print(status_sol)
# save optimal solution
optimal_solutions[problem.name] = x_sol
optimal_objectives[problem.name] = obj_sol
# save to file
with open('results/benchmark_optimal_solutions.npy', 'wb') as f:
np.save(f, problem_names)
np.save(f, optimal_solutions)
np.save(f, optimal_objectives)
def load_true_optimal_solutions():
with open('results/benchmark_optimal_solutions.npy', 'rb') as f:
_ = np.load(f, allow_pickle=True)
optimal_solutions = np.load(f, allow_pickle=True)
optimal_objectives = np.load(f, allow_pickle=True)
return optimal_solutions, optimal_objectives
if __name__=="__main__":
print("[benchmark_compute_true_solutions.py]")
problems = [
Program6(),
Program27(),
Program28(),
Program42(),
Program47(),
Program48(),
Program49(),
Program50(),
Program51(),
Program52(),
Program61(),
Program79(),
]
compute_true_optimal_solutions(problems)
load_true_optimal_solutions()
print("Successfully computed solutions.")