def runTest(directory,file,solver,objType): path = join(directory, file) if Tpnu.isCCTP(path): tpnu = Tpnu.parseCCTP(path) elif Tpnu.isTPN(path): obj = Tpn.parseTPN(path) tpnu = Tpnu.from_tpn_autogen(obj) else: raise Exception("Input file " + path + " is neither a CCTP nor a TPN") startTime = datetime.now() if solver == SolverType.CDRU: search_problem = SearchProblem(tpnu,FeasibilityType.DYNAMIC_CONTROLLABILITY,objType) search_problem.initialize() solution = search_problem.next_solution() elif solver == SolverType.MIP: mip_solver = MipEncode(tpnu,objType) solution = mip_solver.mip_solver() else: raise Exception('Unknown solver type') runtime = datetime.now() - startTime print("----------------------------------------") if solution is not None: print(file + " solved in " + str(runtime)) else: print(file + " not solved in " + str(runtime)) return solution.json_description(file,BenchmarkRCPSP.getSolveName(solver),runtime.total_seconds(),search_problem.candidates_dequeued)
def assert_dc_result(self, example_file, expected_result): for solver in DynamicControllability.SOLVERS: path = join(self.examples_dir, example_file) if Tpnu.isCCTP(path): tpnu = Tpnu.parseCCTP(path) elif Tpnu.isTPN(path): obj = Tpn.parseTPN(join(self.examples_dir, example_file)) tpnu = Tpnu.from_tpn_autogen(obj) else: raise Exception("Input file " + path + " is neither a CCTP nor a TPN") # for tc in tpnu.temporal_constraints: # tpnu.temporal_constraints[tc].pretty_print() conflict = DynamicControllability.check(tpnu, solver=solver) # if conflict is not None: # kirk_conflict = Conflict() # kirk_conflict.add_negative_cycles(conflict,tpnu) # kirk_conflict.pretty_print() is_dynamically_controllable = conflict is None self.assertEqual(is_dynamically_controllable, expected_result)
def runTest(directory, file, solver, objType, feaType, ccType): path = join(directory, file) print("----------------------------------------") print("Solving: " + file) if Tpnu.isCCTP(path): tpnu = Tpnu.parseCCTP(path) elif Tpnu.isTPN(path): obj = Tpn.parseTPN(path) tpnu = Tpnu.from_tpn_autogen(obj) else: raise Exception("Input file " + path + " is neither a CCTP nor a TPN") startTime = datetime.now() manager = Manager() container = manager.SolutionContainer() p = Process(target=BenchmarkAUV.solve, name="Solve", args=( tpnu, solver, objType, feaType, ccType, startTime, file, container, )) p.start() p.join(30) if p.is_alive(): print("Solver is running... No solution found in time limit...") # Terminate foo p.terminate() p.join() solution = container.get_value() runtime = datetime.now() - startTime if solution is not None: print(file + " solved in " + str(runtime)) else: print(file + " not solved in " + str(runtime)) if solution is not None: return solution else: result = {} result["TestName"] = file result["Solver"] = BenchmarkAUV.getSolveName(solver) result["Runtime"] = runtime.total_seconds() result["Error"] = "No Solution Found" return result
def getProblemFromFile(self, path): if Tpnu.isCCTP(path): tpnu = Tpnu.parseCCTP(path) elif Tpnu.isTPN(path): obj = Tpn.parseTPN(path) tpnu = Tpnu.from_tpn_autogen(obj) else: raise Exception("Input file " + path + " is neither a CCTP nor a TPN") return tpnu
def initialize(self): if type(self.network) == ParseTpnClass: self.network = Tpnu.from_tpn_autogen(self.network) elif type(self.network) == Tpnu: pass else: raise Exception("Wrong type of network passed to MIP encode") self.network.initialize() self.num_nodes = self.network.num_nodes self.l = {} # lower bounds self.u = {} # upper bounds self.w = {} # wait self.b = {} # binary variables self.x = {} # binary variables self.calc_distance() if self.objective_type == ObjectiveType.MIN_COST: self.rl = {} self.ru = {} self.tl = {} self.tu = {} for e in self.network.temporal_constraints.values(): #if e.activated: self.l[(e.fro, e.to)] = LinExpr() self.u[(e.fro, e.to)] = LinExpr()
def assert_cctp_result(self, example_file, expected_result): obj = Tpnu.parseCCTP(join(self.examples_dir, example_file)) if obj is not None: self.assertEqual(expected_result, True) if obj is None: self.assertEqual(expected_result, False)
def runTest(directory,file,solver,objType,feaType,ccType): path = join(directory, file) print("----------------------------------------") print("Solving: " + file) if Tpnu.isCCTP(path): tpnu = Tpnu.parseCCTP(path) elif Tpnu.isTPN(path): obj = Tpn.parseTPN(path) tpnu = Tpnu.from_tpn_autogen(obj) else: raise Exception("Input file " + path + " is neither a CCTP nor a TPN") startTime = datetime.now() manager = Manager() container = manager.SolutionContainer() p = Process(target=BenchmarkAUV.solve, name="Solve", args=(tpnu,solver,objType,feaType,ccType,startTime,file,container,)) p.start() p.join(30) if p.is_alive(): print("Solver is running... No solution found in time limit...") # Terminate foo p.terminate() p.join() solution = container.get_value() runtime = datetime.now() - startTime if solution is not None: print(file + " solved in " + str(runtime)) else: print(file + " not solved in " + str(runtime)) if solution is not None: return solution else: result = {} result["TestName"] = file result["Solver"] = BenchmarkAUV.getSolveName(solver) result["Runtime"] = runtime.total_seconds() result["Error"] = "No Solution Found" return result
def assert_tpn_result(self, example_file, expected_result): tpn_obj = Tpn.parseTPN(join(self.examples_dir, example_file)) tpnu_obj = Tpnu.from_tpn_autogen(tpn_obj) if tpnu_obj is not None: self.assertEqual(expected_result, True) if tpnu_obj is None: self.assertEqual(expected_result, False)
def check(network): if type(network) == ParseTpnClass: network = Tpnu.from_tpn_autogen(network) elif type(network) == Tpnu: pass else: raise Exception("Wrong type of network passed to temporal consistency checking") network.initialize() alg = NrgativeCycleDetection() return alg.check(network)
def check(network): if type(network) == ParseTpnClass: network = Tpnu.from_tpn_autogen(network) elif type(network) == Tpnu: pass else: raise Exception("Wrong type of network passed to strong controllability checking") network.initialize() alg = Vidal99Reduction() return alg.check(network)
def check(network): if type(network) == ParseTpnClass: network = Tpnu.from_tpn_autogen(network) elif type(network) == Tpnu: pass else: raise Exception( "Wrong type of network passed to strong controllability checking" ) network.initialize() alg = Vidal99Reduction() return alg.check(network)
def check(network): if type(network) == ParseTpnClass: network = Tpnu.from_tpn_autogen(network) elif type(network) == Tpnu: pass else: raise Exception( "Wrong type of network passed to temporal consistency checking" ) network.initialize() alg = NrgativeCycleDetection() return alg.check(network)
def runTest(directory, file, solver, objType): path = join(directory, file) if Tpnu.isCCTP(path): tpnu = Tpnu.parseCCTP(path) elif Tpnu.isTPN(path): obj = Tpn.parseTPN(path) tpnu = Tpnu.from_tpn_autogen(obj) else: raise Exception("Input file " + path + " is neither a CCTP nor a TPN") startTime = datetime.now() if solver == SolverType.CDRU: search_problem = SearchProblem( tpnu, FeasibilityType.DYNAMIC_CONTROLLABILITY, objType) search_problem.initialize() solution = search_problem.next_solution() elif solver == SolverType.MIP: mip_solver = MipEncode(tpnu, objType) solution = mip_solver.mip_solver() else: raise Exception('Unknown solver type') runtime = datetime.now() - startTime print("----------------------------------------") if solution is not None: print(file + " solved in " + str(runtime)) else: print(file + " not solved in " + str(runtime)) return solution.json_description(file, BenchmarkRCPSP.getSolveName(solver), runtime.total_seconds(), search_problem.candidates_dequeued)
def check(network, solver='morris_n4_dc'): if solver == 'morris_n4_dc': alg = MorrisN4Dc() if type(network) == ParseTpnClass: network = Tpnu.from_tpn_autogen(network) elif type(network) == Tpnu: pass else: raise Exception("Wrong type of network passed to dc checking") network.initialize() return alg.check(network) else: raise Exception('Unknown dynamic controllability solver')
def assert_kirk_result(self, example_file, expected_result): for solver in DynamicControllability.SOLVERS: obj = Tpn.parse(join(self.examples_dir, example_file)) tpnu = Tpnu.from_tpn_autogen(obj) search_problem = SearchProblem(tpnu) search_problem.initialize() solution = search_problem.next_solution() print("----------------------------------------") if solution is not None: print(example_file) solution.pretty_print() else: print(example_file) print(None) search_problem.pretty_print() is_feasible = solution is not None self.assertEqual(is_feasible, expected_result)
__author__ = 'yupeng' from tpn import Tpn from temporal_network.tpnu import Tpnu if __name__ == '__main__': # Load the tpn from file obj = Tpn.parse(r'C:\Users\yupeng\Downloads\document.tpn') tpnu = Tpnu.from_tpn_autogen(obj)
def test_kirk_zipcar11(self): # build a kirk problem # first create a Tpnu tpnu = Tpnu('id','trip') # event # create events for this tpnu node_number_to_id = {} start_event = 1 end_event = 2 node_number_to_id[1] = 'start' node_number_to_id[2] = 'end' event_idx = 3 constraint_idx = 1; # decision variable # create a decision variable to represent the choies over restaurants tpnu_decision_variable = DecisionVariable('dv','where to go?') tpnu.add_decision_variable(tpnu_decision_variable) # Then iterate through all goals and add them as domain assignments goal = 'somewhere' assignment = Assignment(tpnu_decision_variable, 'somewhere', 10.0) tpnu_decision_variable.add_domain_value(assignment) home_to_restaurant = TemporalConstraint('ep-'+str(constraint_idx), 'go to '+str(goal)+'-'+str(constraint_idx), start_event, event_idx, 36.065506858138214, 44.08006393772449) constraint_idx += 1 event_idx += 1 eat_at_restaurant = TemporalConstraint('ep-'+str(constraint_idx), 'dine at '+str(goal)+'-'+str(constraint_idx), event_idx-1, end_event, 0, 30) constraint_idx += 1 node_number_to_id[event_idx-1] = 'arrive-'+str(goal) home_to_restaurant.add_guard(assignment) eat_at_restaurant.add_guard(assignment) tpnu.add_temporal_constraint(home_to_restaurant) tpnu.add_temporal_constraint(eat_at_restaurant) # temporal constraints # create constraints for the duration of the trip tpnu_constraint = TemporalConstraint('tc-'+str(constraint_idx), 'tc-'+str(constraint_idx), start_event, end_event, 0, 15) constraint_idx += 1 tpnu_constraint.relaxable_ub = True tpnu_constraint.relax_cost_ub = 0.1 tpnu.add_temporal_constraint(tpnu_constraint) tpnu.num_nodes = event_idx-1 tpnu.node_number_to_id = node_number_to_id # next formulate a search problem using this tpnu search_problem = SearchProblem(tpnu) search_problem.initialize() solution = search_problem.next_solution()
# tpnu = Tpnu.from_tpn_autogen(obj) # else: # raise Exception("Input file " + path + " is neither a CCTP nor a TPN") # return tpnu # example_file = 'Zipcar-1.cctp' # cdru_dir = dirname(__file__) # examples_dir = join(cdru_dir, join('..', 'examples')) # path = join(examples_dir, example_file) # tpnu = getProblemFromFile(path) # NOTE: Limitation: Assumes num_nodes = N, then events are indexed by 1, 2, ..., N # TODO: Currently, Tpnu.from_tpn_autogen does map eventID to numbers, but it does not take TPNU as input, if we want to use arbitrary event names, we need to add such a function/mapping tpnu = Tpnu(str(uuid4()), 'example-tpnu') tpnu.start_node = 1 tpnu.num_nodes = 3 # Initialize a decision variable dv = DecisionVariable(str(uuid4()), 'dv') as1 = Assignment(dv, 'dv-true', 10) as2 = Assignment(dv, 'dv-false', 3) dv.add_domain_value(as1) dv.add_domain_value(as2) tpnu.add_decision_variable(dv) # Initialize temporal constraints tc1 = TemporalConstraint(str(uuid4()), 'tc1', 1, 2, 0, 10) tc1.controllable = False tpnu.add_temporal_constraint(tc1)