예제 #1
0
    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)
예제 #2
0
파일: tests.py 프로젝트: Optimised/cdru
    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()
예제 #3
0
    def solve(tpnu,solver,objType,feaType,ccType,startTime,filename,container):
        if solver == SolverType.CDRU:
            search_problem = SearchProblem(tpnu,feaType,objType,ccType)
            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
        container.update(solution.json_description(filename,BenchmarkAUV.getSolveName(solver),runtime.total_seconds(),search_problem.candidates_dequeued))
예제 #4
0
    def solve(tpnu,solver,objType,startTime,filename,container):
        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
        container.update(solution.json_description(filename,BenchmarkMBTA.getSolveName(solver),runtime.total_seconds()))
예제 #5
0
    def solve(tpnu, solver, objType, startTime, filename, container):
        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
        container.update(
            solution.json_description(filename, BenchmarkMBTA.getSolveName(solver), runtime.total_seconds())
        )
예제 #6
0
    def assert_cdru_result(self, example_file, f_type, o_type, c_type,
                           expected_result):
        path = join(self.examples_dir, example_file)
        tpnu = self.getProblemFromFile(path)

        startTime = datetime.now()
        search_problem = SearchProblem(tpnu, f_type, o_type, c_type)
        search_problem.initialize()
        # pr = cProfile.Profile()
        # pr.enable()
        solution = search_problem.next_solution()
        # pr.disable()
        # # after your program ends
        # pr.print_stats(sort="cumtime")

        runtime = datetime.now() - startTime

        print("----------------------------------------")
        if solution is not None:
            print(example_file)
            solution.pretty_print()
            print(
                solution.json_print(example_file, "CDRU+PuLP",
                                    runtime.total_seconds(),
                                    search_problem.candidates_dequeued))

            print("Conflicts " + str(len(search_problem.known_conflicts)))
            print("Candidates " + str(search_problem.candidates_dequeued))
        else:
            print(example_file)
            print(None)
            search_problem.pretty_print()
        is_feasible = solution is not None
        self.assertEqual(is_feasible, expected_result)
예제 #7
0
    def solve(tpnu, solver, objType, feaType, ccType, startTime, filename,
              container):
        if solver == SolverType.CDRU:
            search_problem = SearchProblem(tpnu, feaType, objType, ccType)
            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
        container.update(
            solution.json_description(filename,
                                      BenchmarkAUV.getSolveName(solver),
                                      runtime.total_seconds(),
                                      search_problem.candidates_dequeued))
예제 #8
0
파일: tests.py 프로젝트: yu-peng/cdru
    def assert_cdru_result(self, example_file, f_type, o_type, c_type, expected_result):
        path = join(self.examples_dir, example_file)
        tpnu = self.getProblemFromFile(path)

        startTime = datetime.now()
        search_problem = SearchProblem(tpnu,f_type,o_type,c_type)
        search_problem.initialize()
        # pr = cProfile.Profile()
        # pr.enable()
        solution = search_problem.next_solution()
        # pr.disable()
        # # after your program ends
        # pr.print_stats(sort="cumtime")

        runtime = datetime.now() - startTime

        print("----------------------------------------")
        if solution is not None:
            print(example_file)
            solution.pretty_print()
            print(solution.json_print(example_file,"CDRU+PuLP",runtime.total_seconds(),search_problem.candidates_dequeued))

            print("Conflicts " + str(len(search_problem.known_conflicts)))
            print("Candidates " + str(search_problem.candidates_dequeued))
        else:
            print(example_file)
            print(None)
            search_problem.pretty_print()
        is_feasible = solution is not None
        self.assertEqual(is_feasible, expected_result)
예제 #9
0
    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)
예제 #10
0
    def compare_cdru_mip(self, example_file, f_type, o_type, c_type,
                         expected_result):
        path = join(self.examples_dir, example_file)
        tpnu = self.getProblemFromFile(path)

        search_problem = SearchProblem(tpnu, f_type, o_type, c_type)
        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)

        # test mip_encode

        tpnu = self.getProblemFromFile(path)
        mip_solver = MipEncode(tpnu, o_type)
        solution2 = mip_solver.mip_solver()
        diff = 0
        if fabs(solution.utility - solution2.utility) >= 1e-2 and not (
                solution.utility >= 100 and solution2.utility >= 100):
            diff = round(solution.utility - solution2.utility, 4)
        self.ofile = open('output.txt', 'a')
        self.ofile.write('%s    %s    %s    %s\n' %
                         (path, solution2.utility, solution.utility, diff))
        self.ofile.close()
        print("----------------------------------------")
        if solution2 is not None:
            print(example_file)
            solution2.pretty_print()
        else:
            print(example_file)
            print(None)
예제 #11
0
파일: tests.py 프로젝트: Optimised/cdru
    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)
예제 #12
0
파일: tests.py 프로젝트: yu-peng/cdru
    def compare_cdru_mip(self, example_file, f_type, o_type, c_type, expected_result):
        path = join(self.examples_dir, example_file)
        tpnu = self.getProblemFromFile(path)

        search_problem = SearchProblem(tpnu,f_type,o_type,c_type)
        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)

        # test mip_encode

        tpnu = self.getProblemFromFile(path)
        mip_solver = MipEncode(tpnu,o_type)
        solution2 = mip_solver.mip_solver()
        diff = 0
        if fabs(solution.utility - solution2.utility) >= 1e-2 and not(solution.utility >= 100 and solution2.utility >= 100):
            diff = round(solution.utility - solution2.utility,4)
        self.ofile = open('output.txt','a')
        self.ofile.write('%s    %s    %s    %s\n'%(path, solution2.utility, solution.utility, diff))
        self.ofile.close()
        print("----------------------------------------")
        if solution2 is not None:
            print(example_file)
            solution2.pretty_print()
        else:
            print(example_file)
            print(None)
예제 #13
0
#  tpnu = Tpnu.from_tpn_autogen()

print(tpnu)
print(tpnu.decision_variables)
print(tpnu.num_nodes)
print(tpnu.temporal_constraints)
print(tpnu.chance_constraints)
print(tpnu.node_number_to_id)
print(tpnu.start_node)

f_type = FeasibilityType.DYNAMIC_CONTROLLABILITY
o_type = ObjectiveType.MIN_COST
c_type = ChanceConstrained.OFF

startTime = datetime.now()
search_problem = SearchProblem(tpnu, f_type, o_type, c_type)
search_problem.initialize()

# Find all solutions
solution = search_problem.next_solution()
while solution is not None:
    runtime = datetime.now() - startTime
    print("----------------------------------------")
    print("Solution found")
    #  print(example_file)
    solution.pretty_print()
    print(
        solution.json_print("example-tpnu", "CDRU+PuLP",
                            runtime.total_seconds(),
                            search_problem.candidates_dequeued))