Advertisement
HasanRasulov

inventoryPlan.js

Mar 10th, 2021
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The IceBerg Company makes ice cream machnes. The demand for such products varies
  3. from month to month, and so the company needs to develop a strategy to plan its
  4. manufacturing given the fluctuating, but predictable, demand. The company wishes to
  5. design a plan for the next n months. For each month i, the company knows the demand d i ,
  6. n
  7. that is, the number of machines that it will sell. Let
  8. D= ∑ d i be the total demand over
  9. i=1
  10. the next n months. The company keeps a full-time staff who provide labor to manufacture
  11. up to m machines per month. If the company needs to make more than m machines in a
  12. given month, it can hire additional, part-time labor, at a cost that works out to c dollars per
  13. machine. Furthermore, if, at the end of a month, the company is holding any unsold
  14. machines, it must pay inventory costs. The cost for holding j machines is given as a
  15. function h(j) for j = 1,2,...,D, where h( j)≥0 for 1≤ j≤D and h( j)≤h( j+1) for
  16. 1≤ j≤D−1 . Design and implement an algorithm that calculates a plan for the company
  17. that minimizes its costs while fulfilling all the demand.
  18. */
  19.  
  20.  
  21. /*
  22. I used dynamic programming approach in order to solve this problem . Planning function needs
  23. demands array-d, number of months – n,produced machines per month -m,salary for extra machine
  24. -c,holding unsold machine expenses function h . In this example I get this expenses per machines as
  25. 10 percent . I used 2D array to store cost and number of produced machines . Rows indicate
  26. months,columns indicates surpluses . In the first loop I calculated this data for last month. Idea is
  27. based on that first we have to calculate last month then we will go to first month because in last
  28. month we will 0 unsold machines . Apparently,we have to calculate this data for each surpluses.
  29. In second loop we will compare all costs of next month with previous one for surpluses again and
  30. also precalculated for number of machines
  31. */
  32.  
  33. function printPlan(numberOfmachines, n, d) {
  34.     let s = 0;
  35.     for (let k = 0; k < n; k++) {
  36.         console.log(k + 1 + "th month:" + numberOfmachines[k][s]);
  37.         s += numberOfmachines[k][s] - d[k];
  38.     }
  39. }
  40. function inventoryPlanning(n, m, c, d, h) {
  41.     let D = 0;
  42.     for (let item in d) {
  43.         D += item;
  44.     }
  45.     let costs = new Array(n),
  46.         numberOfmachines = new Array(n);
  47.     for (let i = 0; i < n; i++) {
  48.         costs[i] = [];
  49.         numberOfmachines[i] = [];
  50.     }
  51.     let s = 0;
  52.     let f, i = 0;
  53.     while (s <= D) {
  54.         f = Math.max(d[d.length - 1] - s, 0);
  55.         costs[n - 1].push(c * Math.max(f - m, 0) + h(s + f - d[d.length - 1]));
  56.         numberOfmachines[n - 1].push(f);s++;
  57.     }
  58.     let u = d[d.length - 1];
  59.     for (let k = d.length - 2; k >= 0; k--) {
  60.         u += d[k];
  61.         for (s = 0; s <= D; s++) {
  62.             costs[k][s] = Infinity;
  63.             let val;
  64.             for (let f = Math.max(d[k] - s, 0); f <= u - s; f++) {
  65.                 val = costs[k + 1][s + f - d[k]] + c * Math.max(f - m, 0) + h(s + f - d[k]);
  66.                 if (val < costs[k][s]) {
  67.                     costs[k][s] = val;
  68.                     numberOfmachines[k][s] = f;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     console.log(costs[0][0]);
  74.     printPlan(numberOfmachines, d.length, d);
  75. }
  76.  
  77. let demands = [23, 2, 12, 18, 45, 36];
  78. let c = 30;
  79. let m=10;
  80. inventoryPlanning(demands.length, m, c, demands, i => (i * 10 / 100) * c);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement