IBM ILOG Solver User's Manual > The Basics > Searching with Predefined Goals: Magic Square > Suggested answers > Exercise 4 |
Exercise 4 |
INDEX
![]() |
Change the program to search for a Gnomon magic square, which is a n = 4 magic square in which the elements in each quarter (2 x 2 corner) have the same sum.
Describe
Model
IloExpr
and "for" loops to add the constraints on the sum of each quarter to the model.
Solve
The code that has changed from magicsq.cpp
follows. You can view the complete program online in the file YourSolverHome/examples/src/magicsq_ex4.cpp
.
The size of the magic square is set to 4:
int main() { IloEnv env; try { IloModel model(env); IloInt n = 4; |
You should add the following additional constraints:
You should obtain the following result:
Feasible Solution 1 4 13 16 14 15 2 3 8 5 12 9 11 10 7 6 |
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |