Commit 73d41984 authored by Michael Keller's avatar Michael Keller
Browse files

working version

parent 9994d964
from cmath import sqrt
import numpy as np
import gurobipy as gp
from gurobipy import GRB
......@@ -5,13 +6,18 @@ from gurobipy import GRB
# Toy Example
X = 3
Y = 3
# X = 1
# Y = 4
# R = lambda a, b: 1 if a == b else 0
# dist = [2,2]
X = 1
Y = 5
R = lambda a, b: 1 if abs(a - b) <= 1 else 0
dist = [1,1,1,1,1,1,1,1,1]
dist = [1,1,1,1,1]
# X = 15
# Y = 15
# X = 5
# Y = 5
# 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],
......@@ -22,7 +28,7 @@ dist = [1,1,1,1,1,1,1,1,1]
# [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]
# dist = [3, 3, 3, 3, 3, 2, 2, 2, 2, 2]
# auto calculate some vars
numb_crops = len(dist)
......@@ -96,17 +102,18 @@ def main():
neighbors_agree_b = np.array(neighbors_agree_b)
m.addConstr(neighbors_agree @ x == neighbors_agree_b, "neighbors agree")
# All pixels contain a total of 1 crop
# All interpixel relations sum to 1
totals_one = []
totals_one_b = []
for pA in pixels:
constr = [0 for _ in obj]
pB = get_neighbors(pA).pop()
for cA in range(numb_crops):
for cB in range(numb_crops):
constr[get_ind(pA, pB, cA, cB)] = 1
totals_one.append(constr)
totals_one_b.append(1)
neighbors = get_neighbors(pA)
for pB in neighbors:
constr = [0 for _ in obj]
for cA in range(numb_crops):
for cB in range(numb_crops):
constr[get_ind(pA, pB, cA, cB)] = 1
totals_one.append(constr)
totals_one_b.append(1)
totals_one = np.array(totals_one)
totals_one_b = np.array(totals_one_b)
m.addConstr(totals_one @ x == totals_one_b, "totals one")
......@@ -119,7 +126,7 @@ def main():
for pA in pixels:
pB = get_neighbors(pA).pop()
for cB in range(numb_crops):
constr[get_ind(pA, pB, cTest, cB)] = 1
constr[get_ind(pA, pB, cTest, cB)] += 1
global_crops.append(constr)
global_crops_b.append(dist[cTest])
global_crops = np.array(global_crops)
......@@ -127,17 +134,23 @@ def main():
m.addConstr(global_crops @ x == global_crops_b, "global crops")
# quadratic constraint (relaxed)
# O = np.zeros((n, n))
# m.params.NonConvex = 2
# for pA in pixels:
# neighbors = get_neighbors(pA)
# for pB in neighbors:
# for cA in range(numb_crops):
# for cB in range(numb_crops):
# O = np.zeros((n, n))
# val = 0
# for cAn in range(numb_crops):
# for cBn in range(numb_crops):
# O[get_ind(pA, pB, cA, cAn)][get_ind(pB, pA, cB, cBn)] += 1
# # O[get_ind(pB, pA, cB, cBn)][get_ind(pA, pB, cA, cAn)] += 1
# val += x[get_ind(pA, pB, cA, cB)] + x[get_ind(pB, pA, cB, cBn)]
# print(O)
# print(np.all(np.linalg.eigvals(O) >= -0.00000000001))
# m.addConstr(x @ O @ x <= sum(x), "quad constraint")
# m.addConstr(x @ O @ x == val, "quad constraint")
# quadratic constraints (strict)
m.params.NonConvex = 2
......@@ -146,15 +159,13 @@ def main():
for pB in neighbors:
for cA in range(numb_crops):
for cB in range(numb_crops):
O = np.zeros((n, n))
O = np.zeros((n,n))
for cAn in range(numb_crops):
for cBn in range(numb_crops):
O[get_ind(pA, pB, cA, cAn)][get_ind(pB, pA, cB, cBn)] = 1
# print(O)
m.addConstr(x @ O @ x == x[get_ind(pA, pB, cA, cB)], "quad constraint")
# ask the oracle
......@@ -170,11 +181,12 @@ def main():
neighbors = get_neighbors(pA)
for cA in range(numb_crops):
cA_amount = 0
for pB in neighbors:
for cB in range(numb_crops):
cA_amount += sol[get_ind(pA, pB, cA, cB)]
print(cA_amount / len(neighbors), end=", ")
pB = get_neighbors(pA).pop()
for cB in range(numb_crops):
cA_amount += sol[get_ind(pA, pB, cA, cB)]
print(round(cA_amount, 2), end=", ")
print("")
print(sol)
......
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