-
-
Save Terminus-IMRC/c619d51bf80023fb218c61dfa34106d3 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
#!/usr/bin/env python2 | |
from optlang.glpk_interface import Model, Variable, Constraint, Objective | |
def diff(u, v, b, model): | |
model.add(Constraint((u - v) - 100 * b, ub = -1)) | |
model.add(Constraint((v - u) - 100 * (1 - b), ub = -1)) | |
model = Model(name = "Simple model") | |
m1 = Variable("m1", type = "integer") | |
m0 = Variable("m0", lb = 0, ub = 9, type = "integer") | |
month = 10 * m1 + m0 | |
d1 = Variable("d1", type = "integer") | |
d0 = Variable("d0", lb = 0, ub = 9, type = "integer") | |
day = 10 * d1 + d0 | |
H1 = Variable("H1", type = "integer") | |
H0 = Variable("H0", lb = 0, ub = 9, type = "integer") | |
hour = 10 * H1 + H0 | |
M1 = Variable("M1", type = "integer") | |
M0 = Variable("M0", lb = 0, ub = 9, type = "integer") | |
minute = 10 * M1 + M0 | |
S1 = Variable("S1", type = "integer") | |
S0 = Variable("S0", lb = 0, ub = 9, type = "integer") | |
sec = 10 * S1 + M0 | |
model.add(Constraint(month, lb = 1, ub = 12)) | |
model.add(Constraint(day, lb = 1, ub = 31)) | |
model.add(Constraint(hour, lb = 0, ub = 23)) | |
model.add(Constraint(minute, lb = 0, ub = 59)) | |
model.add(Constraint(sec, lb = 0, ub = 59)) | |
model.objective = Objective((((month * 31 + day) * 24 + hour) * 60 + minute) * 60 + sec, direction = "min") | |
l = [m1, m0, d1, d0, H1, H0, M1, M0, S1, S0] | |
count = 0 | |
for i in range(len(l)): | |
for j in range(len(l)): | |
if i >= j: | |
continue | |
diff(l[i], l[j], Variable("bv" + str(count), type = "binary"), model) | |
count += 1 | |
model.optimize() | |
print "status:", model.status | |
for var_name, var in model.variables.iteritems(): | |
print var_name, "=", var.primal | |
print("Result: %d%d/%d%d %d%d:%d%d:%d%d" % ( | |
m1.primal, | |
m0.primal, | |
d1.primal, | |
d0.primal, | |
H1.primal, | |
H0.primal, | |
M1.primal, | |
M0.primal, | |
S1.primal, | |
S0.primal | |
)) |
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
Result: 03/26 17:48:59 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to specify an unequal constraint with an Integer Linear Programming (ILP) solver http://www.yzuda.org/Useful_Links/optimization/unequal.html