Skip to content

Instantly share code, notes, and snippets.

@osin-vladimir
Created November 18, 2015 13:39
Show Gist options
  • Save osin-vladimir/98f96c29d2658f0e2f86 to your computer and use it in GitHub Desktop.
Save osin-vladimir/98f96c29d2658f0e2f86 to your computer and use it in GitHub Desktop.
from cvxpy import *
import numpy as np
#current level
level = np.array([[5,5,10,10,10],[5,5,10,20,10],[0,5,5,10,5],[0,0,0,5,0]])
# desired level
desired_level = np.ones([4,5])*6
# inflow - outflow matrix (inflow positive, outflow negative)
b = level - desired_level
b = np.reshape(b,(1,20));
b = b*np.ones((20,1))
b = b.T
#cost matrix according to Euclidian distance
costs = np.zeros([20,20]);
for i in range(4):
for j in range(5):
for i_ in range(4):
for j_ in range(5):
costs[5*i+j, 5*i_ + j_] = np.sqrt((i-i_)*(i-i_)+(j-j_)*(j-j_))
f = Variable(20,20)
objective = Minimize(sum(costs * f))
constraints = [f >= 0, b + sum(f) == sum(f.T) ]
prob = Problem(objective, constraints)
result = prob.solve()
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment