Skip to content

Commit

Permalink
tidy run and check solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomino committed Oct 9, 2023
1 parent da07248 commit 70a5644
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 69 deletions.
39 changes: 0 additions & 39 deletions AGV_quantum/check_sol.py

This file was deleted.

17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@ In directory ```examples``` there are AGVs scheduling problems. In order of incr

To run this project in terminal use path/to/project> python -m run_examples

There are optional boolean parameters (```1``` yes, ```0``` no): ```--solve_linear``` - solve on CPLEX , ```--train_diagram``` - plot "train diagram" for given problem ```--solve_quadratic``` - solve on hybrid quantum classical (the particular solver and penalty parameter can be set in the script).
There are optional boolean parameters (```1``` yes, ```0``` no):

Example:
- ```--solve_linear``` - if ```1``` solve ILP on CPLEX, if ```0``` solve on hybrid quantum classical
- ```--hyb_solver``` chose particular hybrid solver ```"bqm"``` or ```"cqm"``` are supported, for ```"bqm"``` penalty parameter can be set in the script (works if ```--solve_linear = 0```),
- ```--train_diagram``` - plot "train diagram" for given problem (works if ```--solve_linear = 1```).

Examples:

```python -m run_examples --solve_linear 1 --train_diagram 1 --example "small"```

```python -m run_examples --solve_linear 0 --example "small" --hyb_solver "bqm"```

following examples are supported: ```"tiny", "smallest", "small", "medium_small", "medium", "large", "largest"```

Tho check solutions use:

```python -m check_sol.py --example "small" --hyb_solver "cqm"```


To run tests use path/to/project> python3 -m unittest

## Computational results

In folder ```annealing_results``` there are results on quantum and hybrid devices.

In folder ```lp_files``` there are results of classical solver on the ILP.
In folder ```lp_files``` there are saved linear models for checkout of quantum solver.


### Citing this work
Expand Down
Binary file removed annealing_results/2_tiny_AGV/real_1000_250_4
Binary file not shown.
Binary file added annealing_results/tiny_2_AGV/new_bqm.pkl
Binary file not shown.
Binary file added annealing_results/tiny_2_AGV/new_bqm_info.pkl
Binary file not shown.
72 changes: 72 additions & 0 deletions check_sol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pickle
import os
import dimod
from AGV_quantum import get_results, LinearProg


from pathlib import Path

import argparse
parser = argparse.ArgumentParser("Solve linear or quadratic")
parser.add_argument(
"--example",
type=str,
help="chose example out of [tiny, smallest, small, medium_small, medium, large, largest]",
default="small",
)
parser.add_argument(
"--hyb_solver",
type=str,
help="chose bqm or cqm",
default="bqm",
)

args = parser.parse_args()
cwd = os.getcwd()

if args.example == "tiny":
sol_folder = Path("annealing_results/tiny_2_AGV")
if args.example == "smallest":
sol_folder = Path("annealing_results/2_AGV")
if args.example == "small":
sol_folder = Path("annealing_results/4_AGV")
if args.example == "medium_small":
sol_folder = Path("annealing_results/6_AGV")
if args.example == "medium":
sol_folder = Path("annealing_results/7_AGV")
if args.example == "large":
sol_folder = Path("annealing_results/12_AGV")
if args.example == "largest":
sol_folder = Path("annealing_results/15_AGV")

lp_folder = Path(f"lp_files/lp_{args.example}.pkl")

assert args.hyb_solver in ["bqm", "cqm"]

hybrid = args.hyb_solver

with open(os.path.join(cwd, sol_folder, f"new_{hybrid}.pkl"), "rb") as f:
sampleset = pickle.load(f)

with open(os.path.join(cwd, lp_folder), "rb") as f:
lp = pickle.load(f)

sampleset = dimod.SampleSet.from_serializable(sampleset)

if __name__ == '__main__':

if hybrid == "bqm":
print(sampleset.info)
p=5
lp.to_bqm_qubo_ising(p)
sampleset = lp.interpreter(sampleset)
solutions = get_results(sampleset, lp)
print(solutions)


elif hybrid == "cqm":
print(sampleset.info)
solutions = get_results(sampleset, lp)
for sol in solutions:
if sol["feasible"]:
print(sol)
Binary file modified lp_files/lp_large.pkl
Binary file not shown.
Binary file modified lp_files/lp_largest.pkl
Binary file not shown.
Binary file modified lp_files/lp_medium.pkl
Binary file not shown.
Binary file modified lp_files/lp_medium_small.pkl
Binary file not shown.
Binary file modified lp_files/lp_small.pkl
Binary file not shown.
Binary file modified lp_files/lp_smallest.pkl
Binary file not shown.
Binary file modified lp_files/lp_tiny.pkl
Binary file not shown.
66 changes: 39 additions & 27 deletions run_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,56 @@
parser.add_argument(
"--solve_linear",
type=int,
help="Solve the problem on CPLEX",
help="1 if solve the linear problem on CPLEX, 0 is solve the problem on hybrid quantum approach",
default=1,
)
parser.add_argument(
"--train_diagram",
type=int,
help="Make train diagram for CPLEX solution",
default=0,
)
parser.add_argument(
"--solve_quadratic",
type=int,
help="Solve using hybrid quantum-classical approach",
help="Make train diagram for linear solution",
default=0,
)
parser.add_argument(
"--example",
type=str,
help="chose example []",
help="chose example out of [tiny, smallest, small, medium_small, medium, large, largest]",
default="smallest",
)
parser.add_argument(
"--hyb_solver",
type=str,
help="chose bqm or cqm",
default="bqm",
)


args = parser.parse_args()
cwd = os.getcwd()
if args.example == "tiny":
from examples.example_tiny import M, tracks, tracks_len, agv_routes, d_max, tau_pass, tau_headway, tau_operation, weights, initial_conditions
save_path = os.path.join(cwd, "..", "annealing_results", "tiny_2_AGV")
save_path = os.path.join(cwd, "annealing_results", "tiny_2_AGV")
if args.example == "smallest":
from examples.example_smallest import M, tracks, tracks_len, agv_routes, d_max, tau_pass, tau_headway, tau_operation, weights, initial_conditions
save_path = os.path.join(cwd, "..", "annealing_results", "2_AGV")
save_path = os.path.join(cwd, "annealing_results", "2_AGV")
if args.example == "small":
from examples.example_small import M, tracks, tracks_len, agv_routes, d_max, tau_pass, tau_headway, tau_operation, weights, initial_conditions
save_path = os.path.join(cwd, "..", "annealing_results", "4_AGV")
save_path = os.path.join(cwd, "annealing_results", "4_AGV")
if args.example == "medium_small":
from examples.example_medium_small import M, tracks, tracks_len, agv_routes, d_max, tau_pass, tau_headway, tau_operation, weights, initial_conditions
save_path = os.path.join(cwd, "..", "annealing_results", "6_AGV")
save_path = os.path.join(cwd, "annealing_results", "6_AGV")
if args.example == "medium":
from examples.example_medium import M, tracks, tracks_len, agv_routes, d_max, tau_pass, tau_headway, tau_operation, weights, initial_conditions
save_path = os.path.join(cwd, "..", "annealing_results", "7_AGV")
save_path = os.path.join(cwd, "annealing_results", "7_AGV")
if args.example == "large":
from examples.example_large import M, tracks, tracks_len, agv_routes, d_max, tau_pass, tau_headway, tau_operation, weights, initial_conditions
save_path = os.path.join(cwd, "..", "annealing_results", "12_AGV")
save_path = os.path.join(cwd, "annealing_results", "12_AGV")
if args.example == "largest":
from examples.example_largest import M, tracks, tracks_len, agv_routes, d_max, tau_pass, tau_headway, tau_operation, weights, initial_conditions
save_path = os.path.join(cwd, "..", "annealing_results", "15_AGV")
save_path = os.path.join(cwd, "annealing_results", "15_AGV")
else:
print(f"example {args.example} not suported")

solve_linear = args.solve_linear
solve_quadratic = args.solve_quadratic


if __name__ == "__main__":
Expand All @@ -82,21 +81,34 @@
end = time.time()
print("time: ", end-begin)
model.print_solution(print_zeros=True)
# AGV.nice_print(model, sol) <- WIP
#AGV.nice_print(model, sol) <- WIP
if args.train_diagram:
plot_train_diagram(sol, agv_routes, tracks_len)

if solve_quadratic:
ising_size = True
hybrid = "bqm" # select hybrid solver bqm or cqm
p = 5 # penalty for QUBO creation
else:

assert args.hyb_solver in ["bqm", "cqm"]

hybrid = args.hyb_solver
p = 5 # penalty for QUBO creation
model = QuadraticAGV(AGV)

# saves model for checks
lp_file = os.path.join(cwd, f"lp_files/lp_{args.example}.pkl")
if not os.path.isfile(lp_file):
with open(lp_file, "wb") as f:
pickle.dump(model, f)

model.to_bqm_qubo_ising(p)
if ising_size:
print("n.o. qubits", model._count_qubits())
print("n.o. quandratic couplings", model._count_quadratic_couplings())
print("n.o. linear fields", model._count_linear_fields())

# check if results are saved
is_file = os.path.isfile(os.path.join(save_path, f"new_{hybrid}_info.pkl"))
if is_file:
print(".......... files exist ............")
if hybrid == "bqm":
print("n.o. qubits", model._count_qubits())
print("n.o. quandratic couplings", model._count_quadratic_couplings())
print("n.o. linear fields", model._count_linear_fields())
else:
model.to_cqm()
cwd = os.getcwd()
Expand All @@ -111,7 +123,7 @@
info = sampleset.info
print(sampleset)
print(info)

with open(os.path.join(save_path, f"new_{hybrid}_info.pkl"), "wb") as f:
pickle.dump(info, f)

Expand Down

0 comments on commit 70a5644

Please sign in to comment.