-
-
Save mizzao/4961762 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cplex | |
import numpy as np | |
if __name__ == "__main__": | |
#The histogram data, 250 histograms with 3 bins | |
prob = cplex.Cplex() | |
hists = 250 | |
bins = 3 | |
pairs = bins*(bins-1)/2 | |
D = np.random.randint(0,100,(bins,hists)) | |
varnames = ["w"+str(j) for j in range(hists)] | |
#The weights, one for each histogram | |
prob.variables.add(obj=[0.0]*hists, lb=[0.0]*hists, ub=[1.0]*hists, names = varnames) | |
#The variable that we want to minimize | |
prob.variables.add(obj=[1.0], lb=[0.0], ub=[cplex.infinity], names = ['z']) | |
#Build the constraints | |
rows = [] | |
fliprows = [] | |
for i in range(bins): | |
for j in range(i): | |
row = np.subtract(D[i], D[j]).tolist() | |
rowflip = np.subtract(D[j], D[i]).tolist() | |
prob.linear_constraints.add(lin_expr = [[varnames + ['z'], row+[-1.0]]], senses=['L'], rhs=[0.0] ) | |
prob.linear_constraints.add(lin_expr = [[varnames + ['z'], rowflip+[-1.0]]], senses=['L'], rhs=[0.0] ) | |
prob.linear_constraints.add(lin_expr=[[varnames, [1.0]*hists]], senses=['E'], rhs=[1.0]) #w sums up to 1 | |
#Set objective | |
prob.objective.set_sense(prob.objective.sense.minimize) | |
prob.solve() | |
print prob.solution.get_values() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment