Exploring Pyomo, MILP, and Hidato Puzzles
Alireza Soroudi, PhD
Lead Data Scientist @ bluecrux || SMIEEE || Optimization expert in Supply chain management|| Healthcare management || Lab Digitalization || Power and Energy systems || Developer || Author / Speaker || (views are mine)
It's a boring sunday and solving a math based puzzle might be helful to reduce the bitterness of it.
This newsletter is dedicated to solving the Hidato puzzle created by Gyora M. Benedek . The objective of Hidato puzzle is to populate the grid with sequential numbers that establish connections in horizontal, vertical, or diagonal directions.
Here is some examples :
Solution:
But how to formulate it mathematically?
The grid can be envisioned as a unified graph in which each node (cell) possesses a value representing its visitation sequence. Certain nodes have predetermined sequences, and this construct forms the basis of our assumption.
Math model:
领英推荐
Pyomo code
model = AbstractModel()
model.i = RangeSet(N)
model.j = Set(initialize=model.i)
model.X = Var(model.i,model.j,bounds=(0,1),initialize=0, within=Binary)
model.flow = Var(model.i,model.j,bounds=(0,1),initialize=0, within=Reals)
model.U = Var(model.i,bounds=(1,N),initialize=1, within=Reals)
model.source = Var(model.i,bounds=(0,1),initialize=0, within=Reals)
def rule_C1(model,i):
if d_data[i,'v'] == 1:
coef = 1
else:
coef = 0
return coef*model.source[i] - 1/N == sum(model.flow[i,j]-model.flow[j,i] for j in model.j if (i,j) in connect)
model.C1 = Constraint(model.i,rule=rule_C1)
def rule_C2(model,i):
return sum(model.X[i,j] for j in model.j if (i,j) in connect) <= 1
model.C2 = Constraint(model.i,rule=rule_C2)
def rule_C3(model,i):
return sum(model.X[j,i] for j in model.j if (i,j) in connect) <= 1
model.C3 = Constraint(model.i,rule=rule_C3)
def rule_C4(model,i,j):
if (i,j) in connect:
return model.flow[i,j] <= model.X[i,j]
else:
return Constraint.Skip
model.C4 = Constraint(model.i,model.j, rule=rule_C4)
def rule_C5(model,i,j):
if (i,j) in connect:
return model.U[i]-model.U[j] +N*model.X[i,j]+(N-2)*model.X[j,i] <= (N-1)
else:
return Constraint.Skip
model.C5 = Constraint(model.i,model.j, rule=rule_C5)
def rule_C6(model,i,j):
if (i,j) in connect and i>j:
return model.X[j,i]+ model.X[i,j] <=1
else:
return Constraint.Skip
model.C6 = Constraint(model.i,model.j, rule=rule_C6)
def rule_OF(model):
return sum(model.X[i,j] for (i,j) in connect)
model.obj1 = Objective(rule=rule_OF, sense=minimize)
Results
Applications
Overall, the Hidato problem's blend of logical thinking, pattern recognition, and connectivity makes it applicable in various fields, ranging from entertainment and education to artificial intelligence and practical problem-solving.
Power System Engineer Candidate : Future Planning - Project Management - Collaboration
1 年Dear Professor Alireza Soroudi thanks a lot for sharing your experience and knowledge. Please keep it up.
Head of Data Science & Mathematical Modeling at inovex GmbH, Program Chair PyConDE & PyData 2023/4
1 年Pyomo really is an awesome tool! Love it! Thanks for assembling so many examples on how to apply it.
PhD student |power system| distributed energy resources| electricity markets
1 年Wonderful, like all your examples. Thanks
Next Trend Realty LLC./wwwHar.com/Chester-Swanson/agent_cbswan
1 年Thank you for Sharing.