tester.py 3.04 KB
Newer Older
Michael Keller's avatar
Michael Keller committed
1
from bruteForce import solver
Michael Keller's avatar
Michael Keller committed
2
from greedy import solver_greedy
3
from scorer import score, maxScore, maxScoreAlt, maxScoreGaertner
Michael Keller's avatar
Michael Keller committed
4
from grower import growA, growA_alt, growB, fix
Michael Keller's avatar
zoom    
Michael Keller committed
5
from simulatedAnealing import simulatedAnnealing, simulatedAnnealingMultiple, simulatedAnnealingField
Michael Keller's avatar
Michael Keller committed
6
import numpy as np
Michael Keller's avatar
zoom    
Michael Keller committed
7
8
import copy
import random
Michael Keller's avatar
Michael Keller committed
9
from benchmark import genProblemRandom, genProblemStriped
10
import matplotlib.pyplot as plt
Michael Keller's avatar
Michael Keller committed
11

Michael Keller's avatar
zoom    
Michael Keller committed
12
13
# Cotrini Data Ideal Sol: [[5, 0, 1, 4, 2], [6, 3, 8, 9, 7]], score: 32, 2 * 5
# Agropol Data Ideal Sol: [[1, 8, 6, 0, 7], [3, 9, 2, 5, 4]], score: 14.5, 2 * 5
Michael Keller's avatar
Michael Keller committed
14

15
16
17
18
19
20
def print_field(field):
    for row in field:
        for p in row:
            print(p, end="\t")
        print("")

Michael Keller's avatar
Michael Keller committed
21
22
def main():

Michael Keller's avatar
zoom    
Michael Keller committed
23
    # Cotrini Data (Sum = 26)
Michael Keller's avatar
Michael Keller committed
24
25
    # x = 15
    # y = 15
Michael Keller's avatar
zoom    
Michael Keller committed
26
27
28
29
30
31
32
33
34
35
36
    # R = lambda a, b: [
    #     [-0.5, 1, 1, 1, 1, 1, -1, 0.5, 0.5, 1],
    #     [1, -0.5, -1, 1, 1, 0.5, -1, 1, 1, 1],
    #     [1, -1, -0.5, -1, 1, 0, 0.5, 0, 0, 0],
    #     [1, 1, -1, -0.5, 0, 0, -1, 1, -1, 0],
    #     [1, 1, 1, 0, -0.5, 0, 1, 1, 1, 1],
    #     [1, 0.5, 0, 0, 0, -0.5, 0, 0, 0, 0],
    #     [-1, -1, 0.5, -1, 1, 0, -0.5, 0, 0.5, 0],
    #     [0.5, 1, 0, 1, 1, 0, 0, -0.5, 1, 0],
    #     [0.5, 1, 0, -1, 1, 0, 0.5, 1, -0.5, 1],
    #     [1, 1, 0, 0, 1, 0, 0, 0, 1, -0.5]][a][b]
Michael Keller's avatar
Michael Keller committed
37
    # dist = [23, 23, 23, 23, 23, 22, 22, 22, 22, 22]
Michael Keller's avatar
Michael Keller committed
38

Michael Keller's avatar
zoom    
Michael Keller committed
39
    # Agropol Data (Sum = 8)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    x = 15
    y = 15
    R = lambda a, b:  [[-1, 0, 0.5, -0.5, 1, 0, 0.5, 1, 0.5, 0],
        [0, -1, 0, 1, 0, 0, 0.5, 0.5, 0.5, 0],
        [0.5, 0, -1, 0, 0, 0, 0.5, 0.5, 0, -0.5],
        [-0.5, 1, 0, -1, 0, 0, -1, 0.5, 0.5, 0.5],
        [0, 0, 0, 0, -1, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, -1, 1, 0.5, 0, 0],
        [0.5, 0.5, 0.5, -0.5, 0, 0.5, -1, 0.5, 0.5, 0.5],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0.5, 0.5, 0, 1, 0, 0, 0.5, 0.5, -1, 0],
        [0, 0, -0.5, 0.5, 0, 0, 1, 0.5, 0.5, -1]][a][b]
    dist = [23, 23, 23, 23, 23, 22, 22, 22, 22, 22]

Michael Keller's avatar
Michael Keller committed
54
55
56
57
58
59
60
61
62
    # x = 2
    # y = 2
    # dist = [1, 1, 1, 1]
    # R = lambda a, b: [
    #     [-1, -1, 0, -1],
    #     [-1, -1, -1, 0],
    #     [0, -1, -1, -1],
    #     [-1, 0, -1, -1]
    # ][a][b]
Michael Keller's avatar
Michael Keller committed
63
64
65
66
67
68

    sol = [[1,2,3],[4,5,6],[7,8,9]]
    sol_prime = growA_alt(sol, 3)
    print(sol_prime)


Michael Keller's avatar
zoom    
Michael Keller committed
69
    
Michael Keller's avatar
Michael Keller committed
70
71
72
73
    # generate random 1d field
    fieldStretched = []
    for i, d in enumerate(dist):
        fieldStretched += [i] * d
Michael Keller's avatar
Michael Keller committed
74
    # random.shuffle(fieldStretched)
Michael Keller's avatar
Michael Keller committed
75
76

    # generate 2d field from 1d field
Michael Keller's avatar
Michael Keller committed
77
78

    field = []
Michael Keller's avatar
Michael Keller committed
79
    for i in range(x):
Michael Keller's avatar
Michael Keller committed
80
        field.append([])
Michael Keller's avatar
Michael Keller committed
81
        for j in range(y):
Michael Keller's avatar
Michael Keller committed
82
            field[i].append(fieldStretched[i * x + j])
Michael Keller's avatar
Michael Keller committed
83

Michael Keller's avatar
Michael Keller committed
84
    # R = genProblemRandom(x, y, dist, 0.4)
85
86
87
88
89
90
91
    # R = genProblemStriped(x, y, dist)

    # print("Maxscore Gärtner Agropol Data:")
    # print(maxScoreGaertner(R, field))
    # print("Maxscore Traditional Agropol Data:")
    # print(maxScore(R, field))

Michael Keller's avatar
Michael Keller committed
92
    sol = solver_greedy(x, y, R, dist)
Michael Keller's avatar
Michael Keller committed
93
94
    print("Gärtner MaxScore: ", maxScoreGaertner(R, sol))

Michael Keller's avatar
Michael Keller committed
95
    sol = simulatedAnnealing(R, sol, 2, 0.001, 0.999)
Michael Keller's avatar
Michael Keller committed
96
    print_field(sol)
Michael Keller's avatar
Michael Keller committed
97
    print("Score Greedy: ", score(R, sol) / maxScoreGaertner(R, sol))
Michael Keller's avatar
Michael Keller committed
98
99
100
101


if __name__ == "__main__":
    main()