Skip to content

Commit

Permalink
energies from bqm correction
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomino committed Jul 11, 2024
1 parent ebe1874 commit f73b063
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
23 changes: 21 additions & 2 deletions check_sol.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def csv_write_hist(file_name, hist, key1 = "value", key2 = "count"):
train_diagram = False

count = "_10"
count = "_1"
count = ""

if args.example == "tiny":
sol_folder = Path("annealing_results/tiny_2_AGV")
Expand Down Expand Up @@ -109,13 +109,32 @@ def csv_write_hist(file_name, hist, key1 = "value", key2 = "count"):
print(sampleset.info)
p=5
lp.to_bqm_qubo_ising(p)
sampleset = lp.interpreter(sampleset)

for d in sampleset.data(): # TODO this is UGLY but the loop has always one element
solution_vars = d.sample

solution_vars_ising = {k:2*v-1 for k, v in solution_vars.items()}


energy_computed = dimod.utilities.ising_energy(solution_vars_ising, lp.ising[0], lp.ising[1])

print("Ising energy", energy_computed)

print("Ising energy + offset", energy_computed + lp.ising[2])




sampleset = lp.interpreter(sampleset, "BIN")

solutions = get_results(sampleset, lp)
print(solutions[0]['energy'])

constraints = len(solutions[0]['feas_constraints'][0])
not_feas = solutions[0]['feas_constraints'][1]

#print(solutions[0])

print("feasible", solutions[0]['feasible'])
print("n.o. constraints", constraints)
print("broken feas", not_feas)
Expand Down
10 changes: 7 additions & 3 deletions decipher_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def print_results(dict_list):
path_to_results = os.path.join(ROOT, "AGV_quantum", "ising", "sbm_results", "H100_results.csv")
path_to_annealing = os.path.join(ROOT, "AGV_quantum", "annealing_results", "tiny_2_AGV", "new_bqm.pkl")

size = "smallest"
size = "tiny"
instance = f"{size}_ising"
path_to_renumeration = os.path.join(ROOT, "AGV_quantum", "ising", f"{instance}_renumeration.pkl")
path_to_lp = os.path.join(ROOT, "AGV_quantum", "lp_files", f"lp_{size}.pkl")
Expand All @@ -157,6 +157,7 @@ def print_results(dict_list):
solution_ising = {i + 1: state_ising[i] for i in range(len(state_ising))}



with open(path_to_renumeration, "rb") as f:
var_to_nums, nums_to_var = pickle.load(f)
solutions_vars = {nums_to_var[k]: val for k, val in solution.items()}
Expand All @@ -166,14 +167,16 @@ def print_results(dict_list):
lp = pickle.load(f2)

model = QuadraticAGV(lp)
model.to_bqm_qubo_ising()
p = 5 # TODO this has to be added
model.to_bqm_qubo_ising(p)


energy_computed = dimod.utilities.ising_energy(solutions_vars_ising, model.ising[0], model.ising[1])

sampleset = dimod.SampleSet.from_samples(solutions_vars, vartype=dimod.BINARY, energy=ising_solution["energy"].item())
decrypted_sapleset = model.interpreter(sampleset, "BIN")
decrypted_results = get_results(decrypted_sapleset, lp)
print(decrypted_results)

print("Feasible", decrypted_results[0]["feasible"])
broken_constr = decrypted_results[0]["feas_constraints"][1]
constraints = len(decrypted_results[0]["feas_constraints"][0])
Expand All @@ -182,3 +185,4 @@ def print_results(dict_list):
print("prec broken constr", broken_constr/constraints)
print("energy computed", energy_computed)
print("ising offset", model.ising[2])
print("energy + offset", energy_computed + model.ising[2])
8 changes: 5 additions & 3 deletions run_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@

if __name__ == "__main__":

not_qubo_method = ""

AGV = LinearAGV(M, tracks, tracks_len, agv_routes, d_max, tau_pass, tau_headway, tau_operation, weights,
initial_conditions)
print_ILP_size(AGV.A_ub, AGV.b_ub, AGV.A_eq, AGV.b_eq)
Expand Down Expand Up @@ -111,7 +113,7 @@
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{count}.pkl"))
is_file = os.path.isfile(os.path.join(save_path, f"new{not_qubo_method}_{hybrid}_info{count}.pkl"))
if is_file:
print(".......... files exist ............")

Expand All @@ -130,8 +132,8 @@
print(sampleset)
print(info)

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

with open(os.path.join(save_path, f"new_{hybrid}{count}.pkl"), "wb") as f:
with open(os.path.join(save_path, f"new{not_qubo_method}_{hybrid}{count}.pkl"), "wb") as f:
pickle.dump(sampleset.to_serializable(), f)

0 comments on commit f73b063

Please sign in to comment.