def test_knapsack_simulated_annealing_low_dimensional_2(): params = open_file("../dataset/low-dimensional/f2_l-d_kp_20_878") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = simulated_annealing(ins_problem, exp_schedule(2000, 0.00005, 200000)) optimum = int((open( "../dataset/low-dimensional-optimum/f2_l-d_kp_20_878").readline())) assert optimum == 1024 assert result.value == optimum
def test_knapsack_simulated_annealing_large_scale_1(): params = open_file("../dataset/large_scale/knapPI_1_100_1000_1") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = simulated_annealing(ins_problem, exp_schedule(1000, 0.0005, 4000)) optimum = int(( open("../dataset/large_scale-optimum/knapPI_1_100_1000_1").readline())) assert optimum == 9147 assert result.value <= optimum assert result.value >= 8000
def test_knapsack_simulated_annealing_plot_large_scale_6_4(): params = open_file("../dataset/large_scale/knapPI_1_5000_1000_1") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = simulated_annealing_plot(ins_problem, (800, 0.0005, 4000)) optimum = int((open( "../dataset/large_scale-optimum/knapPI_1_5000_1000_1").readline())) assert optimum == 276457 assert result.value <= 230000 assert result.value >= 190000
def test_knapsack_hill_climbing_large_scale_3(): params = open_file("../dataset/large_scale/knapPI_1_500_1000_1") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = hill_climbing(ins_problem) optimum = int(( open("../dataset/large_scale-optimum/knapPI_1_500_1000_1").readline())) assert optimum == 28857 assert result.value <= 10000 assert result.value >= 9800
def test_knapsack_hill_climbing_rr_large_scale_5(): params = open_file("../dataset/large_scale/knapPI_1_2000_1000_1") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = hill_climbing_random_restart(ins_problem, 100) optimum = int((open( "../dataset/large_scale-optimum/knapPI_1_2000_1000_1").readline())) assert optimum == 110625 assert result.value <= 30000 assert result.value >= 27000
def test_knapsack_simulated_annealing_large_scale_5(): params = open_file("../dataset/large_scale/knapPI_1_2000_1000_1") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = simulated_annealing(ins_problem) optimum = int((open( "../dataset/large_scale-optimum/knapPI_1_2000_1000_1").readline())) assert optimum == 110625 assert result.value <= 14200 assert result.value >= 8500
def test_knapsack_problem_initial(): problem = KnapsackProblem(20, [(0, 0), (0, 0)]) assert problem.initial.capacity == 20 assert problem.value(problem.initial) == 0 assert problem.initial.weight == 0
def test_knapsack_value(): problem = KnapsackProblem(20, [(25, 30), (5, 9), (20, 200)]) state = KnapsackState(20, 20, 20, [False]) assert problem.value(state) == state.value
def test_knapsack_actions(): problem = KnapsackProblem(20, [(25, 30), (5, 9), (20, 200)]) actions = problem.actions(problem.initial) assert actions.__contains__((1, (5, 9), 1)) assert actions.__contains__((1, (20, 200), 2)) assert not actions.__contains__((1, (25, 30), 0))