Skip to content

Commit

Permalink
cleraing module
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomino committed Oct 3, 2023
1 parent 3ea68b9 commit de7efb3
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/LinearProg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from cpp_pyqubo import Binary, Constraint, Placeholder
from pyqubo import LogEncInteger
from pyqubo import Binary
#import numpy as np



class LinearProg:
Expand Down
33 changes: 27 additions & 6 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
from .utils import (
number_gen, create_stations_list, agv_routes_as_edges, create_agv_list,
create_same_way_dict, create_graph, create_t_iterator, create_y_iterator,
create_z_iterator, create_iterators, see_variables, get_data4plot,
nice_print, create_v_in_out, print_time
)

from src.process_results import (
get_results, load_results, store_result
)

from src.quadratic_solver_CPLEX import (
load_linear_prog_object, process_result
)


from src.utils import(
see_non_zero_variables, create_graph, create_iterators, create_agv_list, check_solution_list, qubo_to_matrix,
create_stations_list, create_agv_list, agv_routes_as_edges, create_same_way_dict, create_v_in_out,
create_t_iterator, create_y_iterator, create_z_iterator
)


from src.linear_solver import (
LinearAGV, print_ILP_size
)


from src.qubo_solver import (
sim_anneal, annealing
)


8 changes: 2 additions & 6 deletions src/check_sol.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import pickle
import os
import dimod
#import pyqubo
from src.process_results import get_results
#from src.quadratic_solver_CPLEX import load_linear_prog_object, process_result
#import json
from src.utils import check_solution_list
#import pandas as pd
from src import get_results


from pathlib import Path

Expand Down
5 changes: 3 additions & 2 deletions src/linear_solver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from curses import A_LEFT
import itertools
import numpy as np
from utils import *

from src import see_non_zero_variables, create_graph, create_iterators, create_agv_list
from typing import Optional
from docplex.mp.model import Model
from docplex.mp.solution import SolveSolution
Expand Down Expand Up @@ -208,7 +209,7 @@ def _create_minimal_passing_time_matrix(self, agv_routes, tau_pass):
MPT = []
MPT_b = []

J = utils.create_agv_list(agv_routes)
J = create_agv_list(agv_routes)
for j in J:
s_sp = [(agv_routes[j][i], agv_routes[j][i + 1]) for i in range(len(agv_routes[j]) - 1)]
for s, sp in s_sp:
Expand Down
2 changes: 1 addition & 1 deletion src/quadratic_solver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from src.linear_solver import LinearAGV
from src import LinearAGV
import dimod
from cpp_pyqubo import Binary, Constraint, Placeholder
from pyqubo import LogEncInteger
Expand Down
2 changes: 0 additions & 2 deletions src/quadratic_solver_CPLEX.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from src.LinearProg import LinearProg
import pickle
#import json
from docplex.mp.model import Model
from docplex.mp.solution import SolveSolution
from src.process_results import get_results
import dimod
from docplex.mp.progress import ProgressListener, ProgressClock, TextProgressListener
#import docplex.util.environment as environment
from typing import Union
from math import inf

Expand Down
3 changes: 1 addition & 2 deletions src/qubo_solver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os

import neal
import dimod
from dwave.system import (
Expand All @@ -11,7 +10,7 @@
#from scipy.optimize import linprog

from src.LinearProg import LinearProg
from src.process_results import get_results, load_results, store_result
from src import get_results, load_results, store_result
from typing import Optional

def sim_anneal(
Expand Down
4 changes: 2 additions & 2 deletions src/qubo_to_matrix.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src.quadratic_solver_CPLEX import load_linear_prog_object
from src.utils import qubo_to_matrix
from src import load_linear_prog_object
from src import qubo_to_matrix
import scipy.sparse as sparse

if __name__ == "__main__":
Expand Down
1 change: 0 additions & 1 deletion src/train_diagram.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import matplotlib.pyplot as plt
import matplotlib as mpl
#import numpy as np


mpl.rc("text", usetex=True)
Expand Down
4 changes: 2 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from typing import Optional
from src.LinearProg import LinearProg
import dimod
from src.process_results import get_results
from src.quadratic_solver_CPLEX import process_result
from src import get_results
from src import process_result

cwd = os.getcwd()

Expand Down
7 changes: 4 additions & 3 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from src import LinearProg, qubo_solver, process_results
from src import LinearProg
from src import sim_anneal, annealing, process_results
import dimod
from scipy.optimize import linprog
import numpy as np
Expand Down Expand Up @@ -35,7 +36,7 @@ def test_feasibility_check(self):
"""
make_probabilistic_test = True
bqm = self.lp.bqm
sampleset = qubo_solver.sim_anneal(bqm, beta_range=(5, 100), num_sweeps=1000, num_reads=1000)
sampleset = sim_anneal(bqm, beta_range=(5, 100), num_sweeps=1000, num_reads=1000)
# this is nost important line, it changes [0,1] encoding to linear
sampleset = self.lp.interpreter(sampleset)

Expand All @@ -56,7 +57,7 @@ def test_feasibility_check(self):
assert ret["objective"] < -9.

def test_bqm_soln_sim(self):
dict_list = qubo_solver.annealing(self.lp, "sim", "test_1", load=False, store=False)
dict_list = annealing(self.lp, "sim", "test_1", load=False, store=False)
soln = (next((l for l in dict_list if l["feasible"]), None))
assert soln["objective"] == self.obj
assert (list(soln["sample"].values()) == self.x).all()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src import utils
from src.linear_solver import print_ILP_size, LinearAGV
from src import create_stations_list, create_agv_list
from src import print_ILP_size, LinearAGV
import time
import unittest

Expand All @@ -24,8 +24,8 @@ def test_line_fragemnt(self):
tracks_len = {("s0", "s1"): 1, ("s1", "s0"): 1}

initial_conditions = {("in", 0, "s0"): 0, ("in", 1, "s0"): 1}
stations = utils.create_stations_list(tracks)
J = utils.create_agv_list(agv_routes)
stations = create_stations_list(tracks)
J = create_agv_list(agv_routes)

d_max = {i: 5 for i in J}
tau_pass = {(agv, way[0], way[1]): 5 for agv, way in agv_routes.items()}
Expand Down
73 changes: 36 additions & 37 deletions tests/test_linear_solver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest
import numpy as np
from src import linear_solver
from src import utils
from src.linear_solver import print_ILP_size, LinearAGV
from src import create_graph, create_iterators, create_same_way_dict, create_agv_list
from src import create_stations_list, agv_routes_as_edges, see_non_zero_variables
from src import print_ILP_size, LinearAGV


class SingleStation(unittest.TestCase):
Expand All @@ -12,8 +12,8 @@ def setUpClass(cls):
cls.tracks = [("s0",)]
cls.agv_routes = {0: ("s0",), 1: ("s0",)}
cls.initial_conditions = {("in", 0, "s0"): 1, ("in", 1, "s0"): 2}
cls.graph = utils.create_graph(cls.tracks, cls.agv_routes)
cls.iterators = utils.create_iterators(cls.graph, cls.agv_routes)
cls.graph = create_graph(cls.tracks, cls.agv_routes)
cls.iterators = create_iterators(cls.graph, cls.agv_routes)
cls.d_max = {j: 10 for j in cls.agv_routes.keys()}

cls.x_iter = cls.iterators["x"]
Expand Down Expand Up @@ -43,20 +43,20 @@ def setUpClass(cls):
cls.M = 50
cls.tracks = [("s0", "s1"), ("s1", "s0"), ("s0", "s2"), ("s2", "s3")]
cls.agv_routes = {0: ("s0", "s1"), 1: ("s0", "s2", "s3"), 2: ("s2", "s3")}
cls.graph = utils.create_graph(cls.tracks, cls.agv_routes)
cls.iterators = utils.create_iterators(cls.graph, cls.agv_routes)
cls.graph = create_graph(cls.tracks, cls.agv_routes)
cls.iterators = create_iterators(cls.graph, cls.agv_routes)
cls.initial_conditions = {("in", 0, "s0"): 1, ("in", 1, "s0"): 3, ("in", 2, "s2"): 0}
cls.d_max = {j: 100 for j in cls.agv_routes.keys()}
cls.weights = {j: 1 for j in cls.agv_routes.keys()}
cls.tracks_len = {("s0", "s1"): 1, ("s1", "s0"): 1, ("s0", "s2"): 2, ("s2", "s3"): 2}

all_same_way = utils.create_same_way_dict(cls.agv_routes)
J = utils.create_agv_list(cls.agv_routes)
stations = utils.create_stations_list(cls.tracks)
agv_routes_as_edges = utils.agv_routes_as_edges(cls.agv_routes)
all_same_way = create_same_way_dict(cls.agv_routes)
J = create_agv_list(cls.agv_routes)
stations = create_stations_list(cls.tracks)
agv_routes_as_e = agv_routes_as_edges(cls.agv_routes)

cls.tau_headway = {(j, jp, s, sp): 2 for (j, jp) in all_same_way.keys() for (s, sp) in all_same_way[(j, jp)]}
cls.tau_pass = {(j, s, sp): cls.tracks_len[(s, sp)] for j in J for s, sp in agv_routes_as_edges[j]}
cls.tau_pass = {(j, s, sp): cls.tracks_len[(s, sp)] for j in J for s, sp in agv_routes_as_e[j]}
cls.tau_operation = {(agv, station): 2 for agv in J for station in stations}

cls.x_iter = cls.iterators["x"]
Expand All @@ -66,17 +66,16 @@ def setUpClass(cls):
cls.y_iter = cls.iterators["y"]
cls.z_iter = cls.iterators["z"]
cls.x_iter = cls.iterators["x"]
cls.J = utils.create_agv_list(cls.agv_routes)
cls.J = create_agv_list(cls.agv_routes)

def test_tau_headway(self):
self.assertEqual(self.tau_headway, {(1, 2, "s2", "s3"): 2, (2, 1, "s2", "s3"): 2})

# def test_create_bounds_multi(self): TODO

def test_preference_variables_y_multi(self):
PVY, PVY_b = LinearAGV._create_precedence_matrix_y(self)
self.assertEqual(PVY.shape, (3, len(self.x_iter)))
equations_list = [utils.see_non_zero_variables(PVY[i], self.x_iter) for i in range(PVY.shape[0])]
equations_list = [see_non_zero_variables(PVY[i], self.x_iter) for i in range(PVY.shape[0])]
self.assertIn({(1, 0, "s0"): 1, (0, 1, "s0"): 1}, equations_list)
self.assertIn({(1, 2, "s2"): 1, (2, 1, "s2"): 1}, equations_list)
self.assertIn({(1, 2, "s3"): 1, (2, 1, "s3"): 1}, equations_list)
Expand All @@ -86,15 +85,15 @@ def test_headway_matrix(self):
MH, MH_b = LinearAGV._create_minimal_headway_matrix(self, self.M, self.tracks,
self.tau_headway)
self.assertEqual(MH.shape, (2, len(self.x_iter)))
equations_list = [utils.see_non_zero_variables(MH[i], self.x_iter) for i in range(MH.shape[0])]
equations_list = [see_non_zero_variables(MH[i], self.x_iter) for i in range(MH.shape[0])]
self.assertIn({("out", 1, "s2"): 1, ("out", 2, "s2"): -1, (2, 1, "s2"): -1 * self.M}, equations_list)
self.assertIn({("out", 1, "s2"): -1, ("out", 2, "s2"): 1, (1, 2, "s2"): -1 * self.M}, equations_list)
self.assertTrue(np.array_equal(MH_b, np.array([-1 * tau_h for tau_h in self.tau_headway.values()])))

def test_passing_time_matrix(self):
MPT, MPT_b = LinearAGV._create_minimal_passing_time_matrix(self, self.agv_routes, self.tau_pass)
self.assertEqual(MPT.shape, (4, len(self.x_iter)))
equations_list = [utils.see_non_zero_variables(MPT[i], self.x_iter) for i in range(MPT.shape[0])]
equations_list = [see_non_zero_variables(MPT[i], self.x_iter) for i in range(MPT.shape[0])]
self.assertIn({("out", 0, "s0"): 1, ("in", 0, "s1"): -1}, equations_list)
self.assertIn({("out", 1, "s0"): 1, ("in", 1, "s2"): -1}, equations_list)
self.assertIn({("out", 1, "s2"): 1, ("in", 1, "s3"): -1}, equations_list)
Expand All @@ -113,7 +112,7 @@ def test_single_line_matrix(self):
def test_no_overtake_matrix(self):
NO, NO_b = LinearAGV._create_no_overtake_matrix(self, self.tau_headway)
self.assertEqual(NO.shape, (2, len(self.x_iter)))
equations_list = [utils.see_non_zero_variables(NO[i], self.x_iter) for i in range(NO.shape[0])]
equations_list = [see_non_zero_variables(NO[i], self.x_iter) for i in range(NO.shape[0])]
self.assertIn({(1, 2, 's2'): 1, (1, 2, 's3'): -1}, equations_list)
self.assertIn({(2, 1, 's2'): 1, (2, 1, 's3'): -1}, equations_list)
self.assertTrue(np.array_equal(NO_b, np.array([0 for _ in range(NO.shape[0])])))
Expand All @@ -139,20 +138,20 @@ def setUpClass(cls):
cls.tracks = [("s0", "s1")]
cls.agv_routes = {0: ("s0", "s1"), 1: ("s1", "s0")}
cls.tracks_len = {("s0", "s1"): 1, ("s1", "s0"): 1}
cls.graph = utils.create_graph(cls.tracks, cls.agv_routes)
cls.iterators = utils.create_iterators(cls.graph, cls.agv_routes)
cls.graph = create_graph(cls.tracks, cls.agv_routes)
cls.iterators = create_iterators(cls.graph, cls.agv_routes)

cls.initial_conditions = {("in", 0, "s0"): 0, ("in", 1, "s0"): 0}
cls.d_max = {j: 100 for j in cls.agv_routes.keys()}
cls.weights = {j: 1 for j in cls.agv_routes.keys()}

all_same_way = utils.create_same_way_dict(cls.agv_routes)
J = utils.create_agv_list(cls.agv_routes)
stations = utils.create_stations_list(cls.tracks)
agv_routes_as_edges = utils.agv_routes_as_edges(cls.agv_routes)
all_same_way = create_same_way_dict(cls.agv_routes)
J = create_agv_list(cls.agv_routes)
stations = create_stations_list(cls.tracks)
agv_routes_as_e = agv_routes_as_edges(cls.agv_routes)

cls.tau_headway = {(j, jp, s, sp): 2 for (j, jp) in all_same_way.keys() for (s, sp) in all_same_way[(j, jp)]}
cls.tau_pass = {(j, s, sp): cls.tracks_len[(s, sp)] for j in J for s, sp in agv_routes_as_edges[j]}
cls.tau_pass = {(j, s, sp): cls.tracks_len[(s, sp)] for j in J for s, sp in agv_routes_as_e[j]}
cls.tau_operation = {(agv, station): 2 for agv in J for station in stations}

cls.x_iter = cls.iterators["x"]
Expand All @@ -162,7 +161,7 @@ def setUpClass(cls):
cls.y_iter = cls.iterators["y"]
cls.z_iter = cls.iterators["z"]
cls.x_iter = cls.iterators["x"]
cls.J = utils.create_agv_list(cls.agv_routes)
cls.J = create_agv_list(cls.agv_routes)

def test_equations(self):
PVY, PVY_b = LinearAGV._create_precedence_matrix_y(self)
Expand Down Expand Up @@ -198,7 +197,7 @@ def test_equations(self):
b_eq = PVY_b

for i in range(A_eq.shape[0]):
print(utils.see_non_zero_variables(A_eq[i], self.x_iter))
print(see_non_zero_variables(A_eq[i], self.x_iter))
print(b_eq)
print(A_eq)

Expand All @@ -210,19 +209,19 @@ def setUpClass(cls):
cls.M = 50
cls.tracks = [("s0", "s1"), ("s1", "s2")]
cls.agv_routes = {0: ("s0", "s1"), 1: ("s0", "s1", "s2"), 2: ("s2", "s1")}
cls.graph = utils.create_graph(cls.tracks, cls.agv_routes)
cls.iterators = utils.create_iterators(cls.graph, cls.agv_routes)
cls.graph = create_graph(cls.tracks, cls.agv_routes)
cls.iterators = create_iterators(cls.graph, cls.agv_routes)
cls.d_max = {j: 10 for j in cls.agv_routes.keys()}
cls.weights = {j:1 for j in cls.agv_routes.keys()}
cls.tracks_len = {("s0", "s1"): 1, ("s1", "s2"): 2, ("s2", "s1"): 2}
cls.initial_conditions = {("in", 0, "s0"): 1, ("in", 1, "s0"): 3, ("in", 2, "s2"): 0}
all_same_way = utils.create_same_way_dict(cls.agv_routes)
J = utils.create_agv_list(cls.agv_routes)
stations = utils.create_stations_list(cls.tracks)
agv_routes_as_edges = utils.agv_routes_as_edges(cls.agv_routes)
all_same_way = create_same_way_dict(cls.agv_routes)
J = create_agv_list(cls.agv_routes)
stations = create_stations_list(cls.tracks)
agv_routes_as_e = agv_routes_as_edges(cls.agv_routes)

cls.tau_headway = {(j, jp, s, sp): 2 for (j, jp) in all_same_way.keys() for (s, sp) in all_same_way[(j, jp)]}
cls.tau_pass = {(j, s, sp): cls.tracks_len[(s, sp)] for j in J for s, sp in agv_routes_as_edges[j]}
cls.tau_pass = {(j, s, sp): cls.tracks_len[(s, sp)] for j in J for s, sp in agv_routes_as_e[j]}
cls.tau_operation = {(agv, station): 2 for agv in J for station in stations}

cls.x_iter = cls.iterators["x"]
Expand All @@ -232,7 +231,7 @@ def setUpClass(cls):
cls.y_iter = cls.iterators["y"]
cls.z_iter = cls.iterators["z"]
cls.x_iter = cls.iterators["x"]
cls.J = utils.create_agv_list(cls.agv_routes)
cls.J = create_agv_list(cls.agv_routes)

def test_tau_headway(self):

Expand All @@ -242,7 +241,7 @@ def test_headway_matrix(self):
MH, MH_b = LinearAGV._create_minimal_headway_matrix(self, self.M, self.tracks,
self.tau_headway)
self.assertEqual(MH.shape, (2, len(self.x_iter)))
equations_list = [utils.see_non_zero_variables(MH[i], self.x_iter) for i in range(MH.shape[0])]
equations_list = [see_non_zero_variables(MH[i], self.x_iter) for i in range(MH.shape[0])]
self.assertIn({("out", 0, "s0"): 1, ("out", 1, "s0"): -1, (1, 0, "s0"): -1 * self.M}, equations_list)
self.assertIn({("out", 0, "s0"): -1, ("out", 1, "s0"): 1, (0, 1, "s0"): -1 * self.M}, equations_list)
self.assertTrue(np.array_equal(MH_b, np.array([-1 * tau_h for tau_h in self.tau_headway.values()])))
Expand All @@ -251,7 +250,7 @@ def test_single_line_matrix(self):
SL, SL_b = LinearAGV._create_single_line_matrix(self, self.M)
self.assertEqual(SL.shape, (2, len(self.x_iter)))
self.assertTrue(np.array_equal(SL_b, np.array([0 for _ in range(SL.shape[0])])))
equations_list = [utils.see_non_zero_variables(SL[i], self.x_iter) for i in range(SL.shape[0])]
equations_list = [see_non_zero_variables(SL[i], self.x_iter) for i in range(SL.shape[0])]
self.assertIn({("in", 1, "s2"): 1, ("out", 2, "s2"): -1, (2, 1, "s2", "s1"): -1 * self.M}, equations_list)
self.assertIn({('in', 2, 's1'): 1, ('out', 1, 's1'): -1, (1, 2, 's1', 's2'): -1 * self.M}, equations_list)

Expand Down
Loading

0 comments on commit de7efb3

Please sign in to comment.