def main(): import time from SingleAgent.Utilities.ProblemInstance import ProblemInstance from SingleAgent.Utilities.Agent import Agent from SingleAgent.Utilities.Graph import Graph from SingleAgent.Utilities.ProblemMap import ProblemMap from SingleAgent.Solver.AStar.ODAStar import ODAStar table = UsedTable() graph2 = Graph( ProblemMap(14, { (2, 5): (5, 2), (0, 10): (5, 16), (7, 1): (2, 5), (8, 6): (4, 2) })) agent2 = [ #Agent(0, (0, 4), (0, 9)), Agent(0, (0, 6), (3, 0)), Agent(1, (0, 2), (9, 4)) # Agent(3, (13, 6), (4, 2)), # Agent(4, (13, 0), (1, 3)), # Agent(5, (6, 9), (12, 7)) ] agent3 = [ Agent(0, (0, 4), (0, 9)) # Agent(1, (0, 6), (3, 0)), # Agent(2, (0, 2), (9, 4)), # Agent(3, (13, 6), (4, 2)) # Agent(4, (13, 0), (1, 3)), # Agent(5, (6, 9), (12, 7)) ] testProblem1 = ProblemInstance(graph2, agent2) testProblem2 = ProblemInstance(graph2, agent3) testProblem1.plotProblem() print() testProblem2.plotProblem() startTime = time.time() solver1 = ODAStar() solver1.solve(testProblem1) print("solver time: {0} ".format(time.time() - startTime)) solver1.printPath() solver1.visualizePath(testProblem1) # ===== test addPath === table.addPath(solver1.getPath(), 0) for key, value in table.cellTable().items(): print("{0}, {1}".format(key, value)) # ==== test toList === assert sum(table.toList(14)) == 21 # === test deletePath ==== table.deletePath(solver1.getPath(), 0) assert len(table.cellTable()) == 0
def main(): from SingleAgent.Utilities.ProblemInstance import ProblemInstance from SingleAgent.Utilities.Agent import Agent from SingleAgent.Utilities.Graph import Graph from SingleAgent.Utilities.ProblemMap import ProblemMap graph1 = Graph(ProblemMap(16, {(3, 2): (2, 2), (8, 8): (4, 4), (10, 3): (2, 2), (3, 10): (1, 1)})) agent1 = [Agent(0, (9, 4), (12, 12))] problem1 = ProblemInstance(graph1, agent1) problem1.plotProblem() solver1 = BreadthFirstSearch() solver1.simpleSolve(problem1) solver1.finalList()
def main(): from SingleAgent.Utilities.ProblemInstance import ProblemInstance from SingleAgent.Utilities.Agent import Agent from SingleAgent.Utilities.Graph import Graph from SingleAgent.Utilities.ProblemMap import ProblemMap graph1 = Graph(ProblemMap(16, {(3, 2): (2, 2), (8, 8): (4, 4), (10, 3): (2, 2), (3, 10): (1, 1)})) agent1 = [Agent(0, (2, 2), (5, 5))] # can only test single state problem1 = ProblemInstance(graph1, agent1) problem1.plotProblem() solver1 = SingleAgentAStar() solver1.solve(problem1, None) solver1.printPath() print("=== test root state")
def main(): import time from SingleAgent.Utilities.ProblemInstance import ProblemInstance from SingleAgent.Utilities.Agent import Agent from SingleAgent.Utilities.Graph import Graph from SingleAgent.Utilities.ProblemMap import ProblemMap from SingleAgent.Solver.AStar.ODAStar import ODAStar reservation = Reservation() graph2 = Graph( ProblemMap(14, { (2, 5): (5, 2), (0, 10): (5, 16), (7, 1): (2, 5), (8, 6): (4, 2) })) agent2 = [ #Agent(0, (0, 4), (0, 9)), Agent(0, (0, 6), (3, 0)), Agent(1, (0, 2), (9, 4)) # Agent(3, (13, 6), (4, 2)), # Agent(4, (13, 0), (1, 3)), # Agent(5, (6, 9), (12, 7)) ] agent3 = [ Agent(0, (0, 4), (0, 9)) # Agent(1, (0, 6), (3, 0)), # Agent(2, (0, 2), (9, 4)), # Agent(3, (13, 6), (4, 2)) # Agent(4, (13, 0), (1, 3)), # Agent(5, (6, 9), (12, 7)) ] testProblem1 = ProblemInstance(graph2, agent2) testProblem2 = ProblemInstance(graph2, agent3) testProblem1.plotProblem() print() testProblem2.plotProblem() startTime = time.time() solver1 = ODAStar() solver1.solve(testProblem1) print("solver time: {0} ".format(time.time() - startTime)) solver1.printPath() solver1.visualizePath(testProblem1) # ===== test addPath === reservation.reservePath(solver1.getPath()) for key in reservation.reservedCoordinates(): print("{0}".format(key)) print() for key in reservation.agentDestinations(): print("{0}".format(key)) print(reservation.getLastTimeStep()) # ==== test violation === solver2 = ODAStar() solver2.solve(testProblem2) solver2.printPath() for state in solver2.getPath(): print(reservation.isValid(state)) # === test deletePath ==== reservation.clear() assert reservation.isEmpty()
def main(): import sys from SingleAgent.Utilities.Agent import Agent from SingleAgent.Utilities.Graph import Graph from SingleAgent.Utilities.ProblemMap import ProblemMap graph1 = Graph( ProblemMap(16, { (3, 2): (2, 2), (8, 8): (4, 4), (10, 3): (2, 2), (3, 10): (1, 1) })) agent1 = [Agent(0, (9, 4), (12, 12)), Agent(1, (13, 13), (9, 2))] problem1 = ProblemInstance(graph1, agent1) problem1.plotProblem() print("=== test constructor ===") s1 = SingleAgentState(0, Node((9, 4)), None, problem1) s1_pi = SingleAgentState.fromProblemIns(0, problem1) print("Memory: {0}".format(sys.getsizeof(s1_pi))) print("Memory: {0}".format( sys.getsizeof(s1_pi.predecessor()) + sys.getsizeof(s1_pi.getCoord()))) print("==== test coordinate, cost and heuristic ====") print(s1_pi.getCoord()) assert s1_pi.isRoot() == True, "Root test fail" assert s1_pi.gValue() == 0, "calculateCost is wrong" s1_pi.setHeuristic('manhatten', problem1) assert s1_pi.hValue() == 11, "heuristic is wrong" print("==== test expand function ====") expandStates = s1_pi.expand(problem1) # for s in expandStates: # print(s) print("=== test lt function ===") expand1 = expandStates[1] # 0 is stay expand1.setHeuristic('manhatten', problem1) assert s1_pi < expand1, "lt test fail" print("==== test eq function =====") s2 = SingleAgentState.fromProblemIns(1, problem1) assert s1 == s1_pi, "constructor eq test fail" assert s1_pi != s2, "eq test1 fail" # change problemInstance => == s3 = SingleAgentState.fromProblemIns( 0, ProblemInstance(graph1, [Agent(0, (9, 4), (11, 11))])) assert s1_pi == s3, "eq test2 fail" # change backpointer => == s4 = SingleAgentState(0, Node((9, 4)), s1_pi, problem1) assert s1_pi == s4, "eq test3 fail" print("==== test pickle =====") import pickle sys.setrecursionlimit(20000) # with open('/Users/chengpeng/Documents/MTSL/ElectrodeDesgin/SingleAgent/Result/{0}.pickle'.format('testSingle'), # 'wb') as f: # pickle.dump(s1_pi, f) # pickle.dump(s1_pi, f) with open( '/Users/chengpeng/Documents/MTSL/ElectrodeDesgin/SingleAgent/Result/{0}.pickle' .format('testSingle'), 'rb') as f: s1_pi_2 = pickle.load(f) print(s1_pi_2) assert s1_pi == s1_pi_2
def main(): import time import os from SingleAgent.Utilities.ProblemInstance import ProblemInstance from SingleAgent.Utilities.Agent import Agent from SingleAgent.Utilities.Graph import Graph from SingleAgent.Utilities.ProblemMap import ProblemMap from SingleAgent.Solver.AStar.MultiAgentAStar import MultiAgentAStar # ============= old case =========== # graph1 = Graph(ProblemMap(16, 16, {(3, 2): 2, (8, 8): 4, (10, 3): 2, (3, 10): 1})) # agent1 = [Agent(0, (9, 6), (9, 2)), Agent(1, (9, 2), (9, 6)), Agent(2, (4, 4), (11, 5))] # # agent1 = [Agent(0, (9, 6), (9, 2))] # problem1 = ProblemInstance(graph1, agent1) # problem1.plotProblem() # # startTime = time.time() # solver1 = ODAStar() # solver1.solve(problem1, None) # print("solver time: {0} ".format(time.time() - startTime)) # # solver1.printPath() # solver1.visualizePath(problem1) # problem1.plotProblem() # ============= test cases =========== # fileroot = '/Users/chengpeng/Documents/MTSL/ElectrodeDesgin/DMFB' # # filename = 'benchmark_2_minsik' # # filename = 'in-vitro.3' # # filename ='in-vitro_2.3' # # filename = 'protein.9' # filename = 'in-vitro_2.5' # testProblem = generateProblem(os.path.join(fileroot, filename)) # testProblem.plotProblem() # # startTime = time.time() # solver = ODAStar() # solver.solve(testProblem) # solver.printPath() # ============== end =================== graph2 = Graph( ProblemMap(14, { (2, 5): (5, 2), (0, 10): (5, 16), (7, 1): (2, 5), (8, 6): (4, 2) })) agent2 = [ Agent(0, (0, 4), (0, 9)), Agent(1, (0, 6), (3, 0)), Agent(2, (0, 2), (9, 4)) # Agent(3, (13, 6), (4, 2)), # Agent(4, (13, 0), (1, 3)), # Agent(5, (6, 9), (12, 7)) ] agent3 = [ # Agent(0, (0, 4), (0, 9)), # Agent(1, (0, 6), (3, 0)), # Agent(0, (0, 2), (9, 4)), Agent(0, (13, 6), (4, 2)), Agent(1, (13, 0), (1, 3)) # Agent(5, (6, 9), (12, 7)) ] testProblem1 = ProblemInstance(graph2, agent2) testProblem2 = ProblemInstance(graph2, agent3) testProblem1.plotProblem() print() testProblem2.plotProblem() startTime = time.time() solver1 = MultiAgentAStar() solver1.solve(testProblem1) print("solver time: {0} ".format(time.time() - startTime)) solver1.printPath() solver1.visualizePath(testProblem1) solver2 = ODAStar() solver2.getUsedTable().addPath(solver1.getPath(), 0) solver2.getCAT().addPath(solver1.getPath(), 0) # for key, value in solver2.getCAT().groupOccupantTable().items(): # print("{0}, {1}".format(key, value)) # print() # for key, value in solver2.getCAT().agentDestination().items(): # print("{0}, {1}".format(key, value)) # solver2.init(testProblem2) # init heuristic # root = MultiAgentState.fromProblemIns(testProblem2) # root.setHeuristic('trueDistance', solver2.getHeuristicTable()) # print(root) # root.updateUsedElectrode(solver2.getUsedTable(), testProblem2.getGraph().getSize()) # root.updateCATViolations(solver2.getCAT()) # print(root) solver2.solve(testProblem2) solver2.printPath() solver2.visualizePath(testProblem2)
def main(): from SingleAgent.Utilities.ProblemInstance import ProblemInstance from SingleAgent.Utilities.Agent import Agent from SingleAgent.Utilities.Graph import Graph from SingleAgent.Utilities.ProblemMap import ProblemMap import time # graph1 = Graph(ProblemMap(16, {(3, 2): (2, 2), (8, 8): (4, 4), (10, 3): (2, 2), (3, 10): (1, 1)})) # agent1 = [Agent(0, (9, 6), (9, 2)), Agent(1, (9, 2), (9, 6)), Agent(2, (4, 4), (11, 5))] # problem1 = ProblemInstance(graph1, agent1) # problem1.plotProblem() # # startTime = time.time() # solver1 = MultiAgentAStar() # solver1.solve(problem1) # print("solver time: {0} ".format(time.time() - startTime)) # # solver1.printPath() # solver1.visualizePath(problem1) graph2 = Graph( ProblemMap(14, { (2, 5): (5, 2), (0, 10): (5, 16), (7, 1): (2, 5), (8, 6): (4, 2) })) agent2 = [ Agent(0, (0, 4), (0, 9)), Agent(1, (0, 6), (3, 0)), Agent(2, (0, 2), (9, 4)) # Agent(3, (13, 6), (4, 2)), # Agent(4, (13, 0), (1, 3)), # Agent(5, (6, 9), (12, 7)) ] agent3 = [ #Agent(0, (0, 4), (0, 9)), # Agent(1, (0, 6), (3, 0)), # Agent(0, (0, 2), (9, 4)), Agent(0, (13, 6), (4, 2)), Agent(1, (13, 0), (1, 3)) # Agent(5, (6, 9), (12, 7)) ] testProblem1 = ProblemInstance(graph2, agent2) testProblem2 = ProblemInstance(graph2, agent3) testProblem1.plotProblem() print() testProblem2.plotProblem() startTime = time.time() solver1 = MultiAgentAStar() solver1.solve(testProblem1) print("solver time: {0} ".format(time.time() - startTime)) solver1.printPath() solver1.visualizePath(testProblem1) solver2 = MultiAgentAStar() solver2.getUsedTable().addPath(solver1.getPath(), 0) solver2.getCAT().addPath(solver1.getPath(), 0) # for key, value in solver2.getCAT().groupOccupantTable().items(): # print("{0}, {1}".format(key, value)) # print() # for key, value in solver2.getCAT().agentDestination().items(): # print("{0}, {1}".format(key, value)) # solver2.init(testProblem2) # init heuristic # root = MultiAgentState.fromProblemIns(testProblem2) # root.setHeuristic('trueDistance', solver2.getHeuristicTable()) # print(root) # root.updateUsedElectrode(solver2.getUsedTable(), testProblem2.getGraph().getSize()) # root.updateCATViolations(solver2.getCAT()) # print(root) solver2.solve(testProblem2) solver2.printPath() solver2.visualizePath(testProblem2)
def main(): import sys graph1 = Graph( ProblemMap(16, { (3, 2): (2, 2), (8, 8): (4, 4), (10, 3): (2, 2), (3, 10): (1, 1) })) agent1 = [Agent(0, (9, 4), (12, 12)), Agent(1, (13, 13), (9, 2))] problem1 = ProblemInstance(graph1, agent1) s1 = MultiAgentState.fromProblemIns(problem1) print("=== test constructor, heuristic ===") s1.setHeuristic('manhatten', problem1) print(s1) print("==== test expand function ====") expandStates = s1.expand(problem1) print(len(expandStates)) # for s in expandStates: # print(s) print("=== test setHeuristic/ lt function ===") expand1 = expandStates[1] # 0 is stay expand1.setHeuristic('manhatten', problem1) print(expand1) print(s1) assert not expand1 < s1, "lt test fail" print("==== test eq function =====") s3 = SingleAgentState.fromProblemIns(0, problem1) assert s1 != s3, "eq test1 fail" p2 = ProblemInstance( graph1, [Agent(0, (9, 4), (5, 5)), Agent(1, (13, 13), (9, 2))]) s2 = MultiAgentState.fromProblemIns(p2) assert s1 == s2, "eq test2 fail" s2.setHeuristic('manhatten', p2) assert s1 == s2, "eq test2 fail" print("=== test isValid ===") graph3 = Graph( ProblemMap(16, { (3, 2): (2, 2), (8, 8): (4, 4), (10, 3): (2, 2), (3, 10): (1, 1) })) agent3 = [Agent(0, (9, 4), (9, 6)), Agent(1, (9, 6), (9, 4))] problem3 = ProblemInstance(graph3, agent3) problem3.plotProblem() s2 = MultiAgentState.fromProblemIns(problem3) print("=== test pickle ====") import pickle sys.setrecursionlimit(10000) # with open('/Users/chengpeng/Documents/MTSL/ElectrodeDesgin/SingleAgent/Result/{0}.pickle'.format('testMultiple'), # 'wb') as f: # pickle.dump([s1, expand1], f) with open( '/Users/chengpeng/Documents/MTSL/ElectrodeDesgin/SingleAgent/Result/{0}.pickle' .format('testMultiple'), 'rb') as f: x = pickle.load(f) s1_copy = x[0] expand1_copy = x[1] assert s1 == s1_copy print(s1) print(s1_copy) assert expand1 == expand1_copy print(expand1) print(expand1_copy)
def main(): import sys graph1 = Graph( ProblemMap(16, { (3, 2): (2, 2), (8, 8): (4, 4), (10, 3): (2, 2), (3, 10): (1, 1) })) # agent1 = [Agent(0, (9, 6), (9, 2)), Agent(1, (9, 2), (9, 6)), Agent(2, (4, 4), (11, 5))] agent1 = [Agent(0, (9, 6), (9, 3)), Agent(1, (9, 3), (9, 6))] problem1 = ProblemInstance(graph1, agent1) print("=== test constructor ===") s1 = ODState.fromProblemIns(problem1) # s1.setHeuristic(problem1) print(s1) assert s1.predecessor() is None assert s1.getPreState() is None assert s1.isStandard() assert s1.isValid(s1) # print("======= instance dict ========") # for item in s1.__dict__: # print item # # print("======== class dict =========") # for item in s1.__class__.__dict__: # print item print("==== test expand function ====") expandStates = s1.expand(problem1) # map(lambda x: x.setHeuristic(problem1), expandStates) # for s in expandStates: # print (s) # print() print("memory: {0}".format(sys.getsizeof(expandStates[0]))) expandStates2 = expandStates[0].expand(problem1) # map(lambda x: x.setHeuristic(problem1), expandStates2) # for s in expandStates2: # print (s) print("=== test predecessor ===") assert expandStates[1].predecessor() == s1 # print("=== test lt ===") # assert s1 < expandStates[0] and expandStates[1] < expandStates[0], "lt test fail" print("==== test eq function =====") agent2 = [Agent(0, (5, 6), (6, 10)), Agent(1, (3, 6), (12, 3))] problem2 = ProblemInstance(graph1, agent2) problem2.plotProblem() problem3 = ProblemInstance( graph1, [Agent(0, (7, 6), (6, 10)), Agent(1, (3, 6), (12, 3))]) problem3.plotProblem() p0 = ODState.fromProblemIns(problem2) p1 = p0.expand(problem2)[2] p00 = ODState.fromProblemIns(problem3) p11 = p00.expand(problem2)[4] # print(p1.getRestricDir()) # print(p11.getRestricDir()) assert p1 != p11 agent2 = [Agent(0, (5, 6), (6, 10)), Agent(1, (2, 7), (12, 3))] problem2 = ProblemInstance(graph1, agent2) problem2.plotProblem() problem3 = ProblemInstance( graph1, [Agent(0, (7, 6), (6, 10)), Agent(1, (2, 7), (12, 3))]) p0 = ODState.fromProblemIns(problem2) p1 = p0.expand(problem2)[2] p00 = ODState.fromProblemIns(problem3) p11 = p00.expand(problem2)[4] print(p1.getRestricDir()) print(p11.getRestricDir()) assert p1 == p11 print("=== test isValid ===") print("=== test pickle ====") print("=== test pickle ====") import pickle sys.setrecursionlimit(10000) # with open('/Users/chengpeng/Documents/MTSL/ElectrodeDesgin/SingleAgent/Result/{0}.pickle'.format('testOD'), # 'wb') as f: # pickle.dump(s1, f) with open( '/Users/chengpeng/Documents/MTSL/ElectrodeDesgin/SingleAgent/Result/{0}.pickle' .format('testOD'), 'rb') as f: x = pickle.load(f) print(x) assert x == s1
def main(): import time from SingleAgent.Utilities.ProblemInstance import ProblemInstance from SingleAgent.Utilities.Agent import Agent from SingleAgent.Utilities.Graph import Graph from SingleAgent.Utilities.ProblemMap import ProblemMap from SingleAgent.Solver.AStar.ODAStar import ODAStar graph2 = Graph( ProblemMap(14, { (2, 5): (5, 2), (0, 10): (5, 16), (7, 1): (2, 5), (8, 6): (4, 2) })) agent1 = [ #Agent(0, (0, 4), (0, 9)), Agent(1, (0, 6), (3, 0)), Agent(2, (0, 2), (9, 4)) # Agent(3, (13, 6), (4, 2)), # Agent(4, (13, 0), (1, 3)), # Agent(5, (6, 9), (12, 7)) ] agent2 = [ Agent(0, (0, 4), (0, 9)) # Agent(1, (0, 6), (3, 0)), # Agent(2, (0, 2), (9, 4)), # Agent(3, (13, 6), (4, 2)) # Agent(4, (13, 0), (1, 3)), # Agent(5, (6, 9), (12, 7)) ] agent3 = [ #Agent(0, (0, 4), (0, 9)) # Agent(1, (0, 6), (3, 0)), # Agent(2, (0, 2), (9, 4)), Agent(3, (13, 6), (4, 2)), Agent(4, (13, 0), (1, 3)), Agent(5, (6, 9), (12, 7)) ] testProblem1 = ProblemInstance(graph2, agent1) testProblem2 = ProblemInstance(graph2, agent2) testProblem3 = ProblemInstance(graph2, agent3) testProblem1.plotProblem() print() testProblem2.plotProblem() solver1 = ODAStar() solver1.solve(testProblem1) solver1.printPath() solver1.visualizePath(testProblem1) # ===== test addPath === cat = ConflictAvoidanceTable() cat.addPath(solver1.getPath(), 0) for key, value in cat.groupOccupantTable().items(): print("{0}, {1}".format(key, value)) print() for key, value in cat.agentDestination().items(): print("{0}, {1}".format(key, value)) # ==== test violation === solver2 = ODAStar() solver2.solve(testProblem2) print("path 2:") solver2.printPath() num = cat.violation(solver2.getPath()[0]) assert num == 2 num = cat.violation(solver2.getPath()[1]) assert num == 2 # === test deletePath ==== cat.deletePath(solver1.getPath(), 0) assert len(cat.groupOccupantTable()) == 0 assert len(cat.agentDestination()) == 0