3

I have a 2d grid where certain cell values produce different scores, and I want to maximize the score by assigning values to cells. Is it possible to trace each grid the solver tried during the solve? Below is a snippet of the model just to demonstrate the idea.

int: i_count;
int: e_count;
set of 1..3: ROW;
set of 1..3: COL;

enum CELL = { U, I, E };

array[ROW,COL] of var CELL: grid;

constraint sum(r in ROW, c in COL)(grid[r, c] = I) <= i_count;

constraint sum(r in ROW, c in COL)(grid[r, c] = E) <= e_count;

var int: assignment_gain = sum(r in ROW, c in COL)(
    let {var CELL: cell = grid[r,c]} in (cell = I) * I_SCORE + (cell = E) * E_SCORE
);

solve maximize assignment_gain;

output
    ["Max assignment_gain: \(assignment_gain), with below grid:\n"] ++
    ["\(grid[r,c])" ++ if c = n then "\n" else "" endif
    | r in ROW, c in COL];

I tried putting trace into the assignment_gain statement like below, but didn't work, the output sample is:

grid[1,1]=X_INTRODUCED_0_, grid[1,2]=X_INTRODUCED_1_, ...
var int: assignment_gain = sum(r in ROW, c in COL)(
    let {var CELL: cell = trace("grid[\(r),\(c)]=\(grid[r,c]), ", grid[r,c])} in (cell = I) * I_SCORE + (cell = E) * E_SCORE
);
Kun Hu
  • 417
  • 5
  • 11

1 Answers1

4

The trace function included in MiniZinc allows you to output expression at the compilation stage. This does not allow you to trace the expression during solving.

The options to see the values of variables during solving, depend on what values you want to see.

It is easy in MiniZinc to see intermediate solutions. So solutions found by the solver that are not yet optimal, but do satisfy all constraints. To see these solutions you can add the --intermediate-solutions on command line. In the MiniZincIDE, this should be default behaviour, but you can configure this by clicking "Show configuration editor".

If, on the other hand, you want to explore the values of variables during search (i.e., which values the solver tries for certain variables), then you have to dig a little deeper. MiniZinc supports many kinds of solvers, and they use different kinds of algorithms to find the solution. Therefore there is no standard way for MiniZinc to interact (or show) the search in the solver. Some solvers do however have tools to allow you to examine/debug the search. For example:

  • Chuffed, Gecode, and some other solvers use "CPProfiler", which has recently been integrated in the MiniZincIDE. You can activate it by clicking "MiniZinc > Profile Search" in the toolbar. This tool can help you visualise the search tree and even analyse the no-good learning in LCG solvers.

  • The standard MiniZinc distribution includes "Gecode Gist", which has specialised search analyses for Gecode. It will also show the search tree and decisions made, but has a few more sophisticated tools to inspect nodes in the search tree.

These are the tools shipped with MiniZinc, but for other MiniZinc solvers, more tools might be available.

Dekker1
  • 5,565
  • 25
  • 33