package inventory;
Created on 11.03.
@author
public class InventoryModel2 {
static int n = 3;
static int max = 4;
static double[] inprice = {1, 2, 5};
static double[] outprice = {3, 2, 5};
static double[] store = {1, 1, 1};
static int[] need = {2, 2, 2};
static int[] add = new int[n];
static double[][][] profit = new double[n][max + 1][];
static int[][][] restFromProfit = new int[n][max + 1][];
static int[][][] output = new int[n][max + 1][];
public static void main(String[] args) {
for (int r = 0; r <= max; r++) {
profit[n - 1][r] = new double[need[n - 1] + 1];
restFromProfit[n - 1][r] = new int[need[n - 1] + 1];
output[n - 1][r] = new int[need[n - 1] + 1];
for (int ni = 0; ni <= need[n - 1]; ni++) {
int add_r = ni - r;
if (add_r < 0) {
profit[n - 1][r][ni] = Integer.MIN_VALUE;
continue;
}
profit[n - 1][r][ni]
= outprice[n - 1] * ni
- inprice[n - 1] * add_r
- store[n - 1] * ((double) r / 2);
}
}
for (int m = n - 1; m > 0; m--) {
for (int r = 0; r <= max; r++) {
profit[m - 1][r] = new double[need[m - 1] + 1];
restFromProfit[m - 1][r] = new int[need[m - 1] + 1];
output[m - 1][r] = new int[need[m - 1] + 1];
for (int ni = 0; ni <= need[m - 1]; ni++) {
for (int rr = 0; rr <= max; rr++) {
double profitTemp;
int add_r = ni + rr - r;
if (add_r < 0) {
profitTemp = Integer.MIN_VALUE;
} else {
for (int i = 0; i < profit[m][rr].length; i++) {
profitTemp
= outprice[m - 1] * ni
- inprice[m - 1] * add_r
- store[m - 1] * ((double) (r + rr) / 2) + profit[m][rr][i];
if (profitTemp > profit[m - 1][r][ni]) {
profit[m - 1][r][ni] = profitTemp;
restFromProfit[m - 1][r][ni] = rr;
output[m - 1][r][ni] = i;
}
}
}
}
}
}
}
int output0 = -1;
double profitTemp = 0;
for (int ni = 0; ni <= need[0]; ni++) {
if(profit[0][0][ni] > profitTemp) {
profitTemp = profit[0][0][ni];
output0 = ni;
}
}
System.out.println("=========== План закупок ===============");
for (int m = 0, r = 0, o = output0; m < n; m++) {
System.out.println((m + 1)
+ "; Остаток на начало: " + r
+ "; Остаток на конец: " + restFromProfit[m][r][o]
+ "; Потребление: " + o
+ "; Закупки: " + (o - r + restFromProfit[m][r][o])
);
r = restFromProfit[m][r][o];
o = output[m][r][o];
}
}
}