Commit 9e66f46a authored by Michael Keller's avatar Michael Keller
Browse files

Score: 658

parent 6568c02d
No preview for this file type
......@@ -70,7 +70,7 @@ double maxScoreGaertner(int *field, int x, int y, double *R, int crops) {
clock[i+1]++;
i++;
}
if(iteration % 1000000 == 0) {
if(iteration % 10000000 == 0) {
printf("%i %i %i %i\n", clock[8], clock[7], clock[6], clock[5]);
}
iteration++;
......
This diff is collapsed.
This diff is collapsed.
import random
from scipy.optimize import linprog
import copy
import math
import multiprocessing as mp
# Toy Example
......@@ -72,7 +72,7 @@ def scorePixel(field, pixel):
indA = get_ind(p_x, p_y, c1)
indB = get_ind(p_x + n1, p_y + n2, c2)
if field[indA] > 0 and field[indB] > 0:
score += field[indA] * field[indB] * R(c1, c2)
score += field[indA] * field[indB] * (R(c1, c2) + R(c2, c1)) /2
return score
def score(field):
......@@ -96,6 +96,41 @@ def print_field(x0):
# END HELPERS
#
def scramble(x0):
for i in range(x-1):
for j in range(y-1):
for n1 in range(0, 2):
for n2 in range(0, 2):
if n1 == 0 and n2 == 0:
continue
c1 = -1
c2 = -1
for c in range(numb_crops):
ind1 = get_ind(i, j, c)
ind2 = get_ind(i + n1, j + n2, c)
if x0[ind1] > 0.99:
c1 = c
if x0[ind2] > 0.99:
c2 = c
score_delta = -(scorePixel(x0, (i, j)) + scorePixel(x0, (i+n1, j+n2)))
x0[get_ind(i, j, c1)] = 0
x0[get_ind(i, j, c2)] = 1
x0[get_ind(i+n1, j+n2, c2)] = 0
x0[get_ind(i+n1, j+n2, c1)] = 1
score_delta += scorePixel(x0, (i, j)) + scorePixel(x0, (i+n1, j+n2))
if score_delta < 0:
x0[get_ind(i, j, c1)] = 1
x0[get_ind(i, j, c2)] = 0
x0[get_ind(i+n1, j+n2, c2)] = 1
x0[get_ind(i+n1, j+n2, c1)] = 0
return x0
def iteration(x0):
# generate pixel subset
......@@ -157,7 +192,7 @@ def iteration(x0):
bnd = [(0, 1) for _ in range(len(obj))]
# ask the oracle
opt = linprog(c=obj, A_eq=lhs_eq, b_eq=rhs_eq, bounds=bnd, method="revised simplex").x
opt = linprog(c=obj, A_eq=lhs_eq, b_eq=rhs_eq, bounds=bnd, method="revised simplex", options={ "tol": 0.001 }).x
# insert the solution
for i, p in enumerate(pixels):
......@@ -168,10 +203,12 @@ def iteration(x0):
return x0
def main():
best_x0 = []
best_x0_score = - 100 * x * y
best_x0_score = 658.0
while True:
......@@ -184,6 +221,7 @@ def main():
# generate x0 vector
x0 = []
for i in range(x):
for j in range(y):
pixel = [0 for _ in range(len(dist))]
......@@ -195,10 +233,14 @@ def main():
print("Initial Score: ", score(x0), "Sum: ", sum(x0))
print("Start Scramble")
x0 = scramble(x0)
print(score(x0))
while iterations < 100 or (iterations < 200 and score(x0) > best_x0_score):
x0 = iteration(x0)
x0 = scramble(x0)
iterations += 1
scr = score(x0)
......@@ -211,14 +253,19 @@ def main():
print("Score: ", scr)
if score(x0) > best_x0_score:
best_x0 = copy.deepcopy(x0)
best_x0_score = score(x0)
if score(x0) > best_x0_score:
best_x0 = copy.deepcopy(x0)
best_x0_score = score(x0)
print("Write to file")
f = open("best-score-parallel.txt", "a")
f.write("Score: " + str(best_x0_score) + "\n x0 = " + str(best_x0) + "\n\n")
f.close()
print("Terminated")
print("Score: ", score(x0))
print("Best Score: ", best_x0_score)
print(best_x0)
# print(best_x0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment