Fair product distribution algorithm

Here is my problem:

  • There are Russian companies products.
  • All products must be distributed in k days
  • The distribution of Ci products must be consistent - which means that it can be distributed on days 2,3,4,5, but not 2,3,6,7
  • the number of distributed products by Ci on day j should be less than (or equal to) on day j-1 (if they were on day j-1)
  • the difference between the distributed products between days i and j should not exceed 1

Example:

We have 3 days to distribute the products. Company A Products: a, a, a, a, a. Company B Products: b, b, b. Company Products C: c, c

Fair distribution: [AAB, AABC, abc]

Invalid distribution: [AABC, AABC, AB] because on the 1st day there are 4 products, on the 3rd day there are 2 products (difference> 1)

Invalid distribution: [ABC, AABC, AAB] because on the 1st day there is one product A, and on the 2nd day there are 2 products A, so the distribution of product A does not decrease.

EDIT if there is a case that makes distribution impossible, please provide a brief description, I will accept the answer

+5
source share
4 answers

Gareth Rees commentary on djna's answer is right - the following counterexample is unsolvable :

  • 3 , 7 A 5 B

Perl ( , , ):

my ($na, $nb) = (7, 5);
for (my $a1 = 0; $a1 <= $na; ++$a1) {
    for (my $a2 = 0; $a2 <= $na - $a1; ++$a2) {
        my $a3 = $na - $a1 - $a2;
        for (my $b1 = 0; $b1 <= $nb; ++$b1) {
            for (my $b2 = 0; $b2 <= $nb - $b1; ++$b2) {
                my $b3 = $nb - $b1 - $b2;
                if ($a1 >= $a2 && $a2 >= $a3 || $a1 == 0 && $a2 >= $a3 || $a1 == 0 && $a2 == 0) {
                    if ($b1 >= $b2 && $b2 >= $b3 || $b1 == 0 && $b2 >= $b3 || $b1 == 0 && $b2 == 0) {
                        if (max($a1 + $b1, $a2 + $b2, $a3 + $b3) - min($a1 + $b1, $a2 + $b2, $a3 + $b3) <= 1) {
                            print "Success! ($a1,$a2,$a3), ($b1,$b2,$b3)\n";
                        }
                    }
                }
            }
        }
    }
}

, , . ( max() min() - , .)

+2

, , , MiniZinc. Gecode backend, , 20 1,6 .

include "globals.mzn";

%%% Data
% Number of companies
int: n = 3;
% Number of products per company
array[1..n] of int: np = [5, 3, 2];
% Number of days
int: k = 3;

%%% Computed values
% Total number of products
int: totalnp = sum(np);
% Offsets into products array to get single companys products 
% (shifted cumulative sum).
array[1..n] of int: offset = [sum([np[j] | j in 1..i-1]) 
                          | i in 1..n];

%%% Predicates
predicate fair(array[int] of var int: x) =
      let { var int: low,
            var int: high
      } in
        minimum(low, x) /\
        maximum(high, x) /\
        high-low <= 1;
predicate decreasing_except_0(array[int] of var int: x) =
        forall(i in 1..length(x)-1) (
                 (x[i] == 0) \/
                 (x[i] >= x[i+1])
        );
predicate consecutive(array[int] of var int: x) =
        forall(i in 1..length(x)-1) (
             (x[i] == x[i+1]) \/
             (x[i] == x[i+1]-1)
        );

%%% Variables
% Day of production for all products from all companies
array[1..totalnp] of var 1..k: products 
          :: is_output;
% total number of products per day
array[1..k] of var 1..totalnp: productsperday 
        :: is_output;

%%% Constraints 
constraint global_cardinality(products, productsperday);
constraint fair(productsperday);
constraint
    forall(i in 1..n) (
         let { 
             % Products produced by company i
             array[1..np[i]] of var int: pi
                = [products[j] | 
                 j in 1+offset[i]..1+offset[i]+np[i]-1],
             % Products per day by company i
             array[1..k] of var 0..np[i]: ppdi
         } in
           consecutive(pi) /\
           global_cardinality(pi, ppdi) /\
           decreasing_except_0(ppdi)
    );

%%% Find a solution, default search strategy
solve satisfy;

decreasing_except_0 consecutive . , , , (, ).

+2

, 4 5 :

  • 4: j A, C (j, A) == 0 C (j, A) >= C (j + 1, A)
  • 5: j, |C(i) - C(j)| <= 1

, . , , 4 ( ), , , ( , , , ).

3 .

:

A, , j, :

  • C (i, A)> 0 and C (j, A)> 0
  • for any day x, so x <i or x> j, C (x, A) = 0
  • for any day x, so I <x <j, C (x, A) = C (x)

Admittedly, the task then becomes trivial to solve :)

+1
source

I do not think that you can always fulfill your requirements.

Consider 4 days and 6 items from suppliers A and 6 items from supplier B.

0
source

All Articles