-1

after having watched the coursera Basic Modelling course I am trying to categorize my problem so that to choose the suitable Model representation on MiniZinc.

I have a range of 10 products, each of them with its 4 special features/attributes, (a table 4x10). This table has fixed values. The user will give as input 4 parameters. The constraints will be created in a way that the user input parameters will determine the product's attribute values.

The decision variable will be the subset of the products that match user's input.

from my understanding this is a problem of selecting a subset from a set of Objects, is there any example suggestion available that corresponds to the above Minizinc model description to have a look?

user2829319
  • 239
  • 4
  • 16
  • I don't fully understand your question. What subset are you expecting? Is it a subset of the 10 products that (completely) match the input vector? Or perhaps the product that has the features "nearest" the input vector? – hakank Sep 05 '18 at 15:27
  • The subset of products that its features are nearest to the input vector. – user2829319 Sep 06 '18 at 09:06

1 Answers1

0

I'm (still) not completely sure about the exact specification of the problem, but here is a model that identifies all the products that are "nearest" the input data. I've defined "nearest" simply as the sum of absolute differences between each feature of a product and the input array (calculated by the score function).

int: k; % number of products
int: n; % number of features

array[1..k, 1..n] of int: data;
array[1..n] of int: input;


% decision variables
array[1..k] of var int: x; % the closeness score for each product
array[1..k] of var 0..1: y; % 1: this products is nearest (as array)
% var set of 1..k: y;  % products closest to input (as set)
var int: z; % the minimum score

function var int: score(array[int] of var int: a, array[int] of var int: b) =
  let  {
     var int: t = sum([abs(a[i]-b[i]) | i in index_set(a)])
  } in
    t
;

solve minimize z;

constraint
  forall(i in 1..k) (
    x[i] = score(data[i,..], input) /\
    (y[i] = 1 <-> z = x[i]) % array 
    % (i in y <-> x[i] = z) % set
  )
  /\
  minimum(z, x)
 ;

 output [
  "input: \(input)\n",
  "z: \(z)\n",
  "x: \(x)\n",
  "y: \(y)\n\n"
]
++
[
  % using array representation of y 
  if fix(y[i]) = 1 then 
    "nearest: ix:\(i) \(data[i,..])\n" else  "" endif
  | i in 1..k
];

% data
k = 10;
n = 4;

% random features for the products
data = array2d(1..k,1..n,
 [
 3,6,7,5,
 3,5,6,2,
 9,1,2,7,
 0,9,3,6,
 0,5,2,4, % score 5
 1,8,7,9,
 2,0,2,3, % score 5
 7,5,9,2,
 2,8,9,7,
 3,6,1,2]);

 input = [1,2,3,4];
 % input = [7,5,9,2]; % exact match for product 8

The output is:

 input: [1, 2, 3, 4]
 z: 5
 x: [11, 10, 13, 10, 5, 15, 5, 17, 16, 10]
 y: [0, 0, 0, 0, 1, 0, 1, 0, 0, 0]

 nearest: ix:5 [0, 5, 2, 4]
 nearest: ix:7 [2, 0, 2, 3]
hakank
  • 6,629
  • 1
  • 17
  • 27