def test_basic_groups2(self):
        inputs = UserInputs('input.txt', 160, 31, 10, list(), [3])
        solver = AssignmentSolver(inputs)
        solver.initialize()

        list1, list2, list3 = list(), list(), list()

        for user in inputs.users:
            if user.user_id == 'djcran' or user.user_id == 'chen464':
                list1.append(user)
            elif user.user_id == 'kapadia' or user.user_id == 'zehzhang' or user.user_id == 'fan6':
                list2.append(user)
            elif user.user_id == 'steflee':
                list3.append(user)

        solver.state = AssignmentState(
            inputs, [Group(list()),
                     Group(list1),
                     Group(list2),
                     Group(list3)])

        self.assertEqual(len(solver.state.groups), 4)
        self.assertEqual(len(inputs.users), 6)
        self.assertEqual(
            sorted([g.evaluate(inputs) for g in solver.state.groups]),
            [0, 0, 12, 42])
        self.assertEqual(solver.state.evaluate(), 534)
    def simulated_annealing_test_once(self):
        inputs = UserInputs(input_file, 160, 31, 10, list(), [3])
        problem = AssignmentSolver(inputs)
        problem.initialize()

        algorithm = SimulatedAnnealing(problem)
        res = algorithm.search().evaluate()
        print(res)
        return res
示例#3
0
def test_random_restart_hill_climbing_hybrid():
    inputs = UserInputs(input_file, 160, 31, 10, list(), [3])
    problem = AssignmentSolver(inputs)
    problem.initialize()

    algorithm = RandomRestartHillClimbingHybrid(problem,
                                                options={'nprocs': nprocs})
    res = algorithm.search().evaluate()
    return res
    def test_random_restart_hill_climbing(self):
        print('HC ------ :')
        inputs = UserInputs(input_file, 160, 31, 10, list(), [3])
        problem = AssignmentSolver(inputs)
        problem.initialize()

        algorithm = RandomRestartHillClimbing(problem,
                                              options={'nprocs': nprocs})
        res = algorithm.search().evaluate()
        print(res)
    def test_basic_groups1(self):
        inputs = UserInputs('input.txt', 160, 31, 10, list(), [1])
        solver = AssignmentSolver(inputs)
        solver.initialize()

        self.assertEqual(len(solver.state.groups), len(inputs.users) + 1)
        self.assertEqual(len(inputs.users), 6)
        self.assertEqual(
            sorted([g.evaluate(inputs) for g in solver.state.groups]),
            [0, 0, 0, 0, 10, 21, 21])
        self.assertEqual(solver.state.evaluate(), 1012)
    def test_hill_climbing_with_random_walk(self):
        min_res = math.inf
        print('test_hill_climbing_with_random_walk ------ :')
        for i in range(10):
            inputs = UserInputs(input_file, 160, 31, 10, list(), [3])
            problem = AssignmentSolver(inputs)
            problem.initialize()

            algorithm = HillClimbingWithRandomWalk(problem)
            res = algorithm.search().evaluate()
            print(res)
            min_res = res if res < min_res else min_res
    def test_first_choice_hill_climbing(self):
        min_res = math.inf
        print('first_choice_hill_climbing ------ :')
        for i in range(10):
            inputs = UserInputs(input_file, 160, 31, 10, list(), [3])
            problem = AssignmentSolver(inputs)
            problem.initialize()

            algorithm = FirstChoiceHillClimbing(problem)
            res = algorithm.search().evaluate()
            min_res = res if res < min_res else min_res
            print(res)
    def test_state_generation2(self):
        inputs = UserInputs('input_small.txt', 160, 31, 10, list(), [1])
        solver = AssignmentSolver(inputs)
        solver.initialize()

        inputs = UserInputs('input_small.txt', 160, 31, 10, inputs.users, [3])
        solver.inputs = inputs
        solver.state.inputs = inputs

        next_state = solver.state.get_next_random_state()
        count = 0
        while next_state:
            count += 1
            next_state = solver.state.get_next_random_state()
        self.assertEqual(count, 2)
    def test_assign_user_to_group(self):
        inputs = UserInputs('input.txt', 160, 31, 10, list(), [3])
        solver = AssignmentSolver(inputs)
        solver.initialize()

        solver.state = AssignmentState(inputs)

        solver.state = solver.state.assign_user_to_group(
            self.get_user('djcran', inputs.users), solver.state.groups[0])
        self.assertEqual(solver.state.groups[1].evaluate(inputs), 21)
        self.assertEqual(solver.state.evaluate(), 181)

        solver.state = solver.state.assign_user_to_group(
            self.get_user('chen464', inputs.users), solver.state.groups[1])
        self.assertEqual(solver.state.evaluate(), 172)

        solver.state = solver.state.assign_user_to_group(
            self.get_user('kapadia', inputs.users), solver.state.groups[0])
        self.assertEqual(solver.state.evaluate(), 353)

        solver.state = solver.state.assign_user_to_group(
            self.get_user('zehzhang', inputs.users), solver.state.groups[1])
        self.assertEqual(solver.state.evaluate(), 375)
    def test_hill_climbing1(self):
        inputs = UserInputs(input_file, 160, 31, 10, list(), [1])
        problem = AssignmentSolver(inputs)
        problem.initialize()

        algorithm = HillClimbing(problem)