Search code examples
minizinc

Support for pow() with decision variables in MiniZinc


I am trying to build a model with a decision variable that takes the range 0..10 with the constraint that it must be divisible by 4.The output must be the one that minimizes the value (x-7)^2. Investigating I see that Gecode already supports it.

I have tried to build the model as follows but I get an error using the built-in pow()

% Decision variable

var 0..10: x;  

% Constraints

constraint (x mod 4) == 0;

% Call to the resolver
int: obj = pow(x-7, 2);

solve minimize obj;

% Solution

output["x = ", show(x), "\nobj = ", show(obj)];

I get the following error:

MiniZinc: type error: initialisation value for `obj' has invalid type-inst: expected `int', actual `var int'

I think it occurs because is considering the variable as a decision variable instead of just an integer parameter.


Solution

  • To move the pow() call from solution time to compilation time, you could use a pre-calculated array:

    % Decision variable
    
    var 0..10: x;  
    
    set of int: DomainX = 0..10;
    array[DomainX] of int: xa = array1d(DomainX, [pow(i-7, 2) | i in DomainX]);
    
    % Constraints
    
    constraint (x mod 4) == 0;
    
    % Call to the resolver
    var int: obj = xa[x];
    
    solve minimize obj;
    
    % Solution
    
    output["x = ", show(x), "\nobj = ", show(obj)];
    

    As commented by hakank, variable obj must be of type var int, as it depends on a decision variable.