def test_knapsack_genetic_algorithm_low_dimensional_1():
    params = open_file("../dataset/low-dimensional/f1_l-d_kp_10_269")
    knapsack = GeneticKnapsack(params[0], params[1])
    result = knapsack.solve()
    optimum = int((open(
        "../dataset/low-dimensional-optimum/f1_l-d_kp_10_269").readline()))
    result = sorted(result, key=lambda item: item.fitness, reverse=True)
    assert optimum == 295
    assert result[0].fitness.values[0] == optimum
def test_knapsack_genetic_algorithm_large_scale_1():
    params = open_file("../dataset/large_scale/knapPI_1_100_1000_1")
    knapsack = GeneticKnapsack(params[0], params[1])
    result = knapsack.solve(300, 100, mu=400, l=1600)
    optimum = int((
        open("../dataset/large_scale-optimum/knapPI_1_100_1000_1").readline()))
    result = sorted(result, key=lambda item: item.fitness, reverse=True)
    assert optimum == 9147
    assert result[0].fitness.values[0] <= 8500
    assert result[0].fitness.values[0] >= 7000
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_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