Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The IceBerg Company makes ice cream machnes. The demand for such products varies
- from month to month, and so the company needs to develop a strategy to plan its
- manufacturing given the fluctuating, but predictable, demand. The company wishes to
- design a plan for the next n months. For each month i, the company knows the demand d i ,
- n
- that is, the number of machines that it will sell. Let
- D= ∑ d i be the total demand over
- i=1
- the next n months. The company keeps a full-time staff who provide labor to manufacture
- up to m machines per month. If the company needs to make more than m machines in a
- given month, it can hire additional, part-time labor, at a cost that works out to c dollars per
- machine. Furthermore, if, at the end of a month, the company is holding any unsold
- machines, it must pay inventory costs. The cost for holding j machines is given as a
- function h(j) for j = 1,2,...,D, where h( j)≥0 for 1≤ j≤D and h( j)≤h( j+1) for
- 1≤ j≤D−1 . Design and implement an algorithm that calculates a plan for the company
- that minimizes its costs while fulfilling all the demand.
- */
- /*
- I used dynamic programming approach in order to solve this problem . Planning function needs
- demands array-d, number of months – n,produced machines per month -m,salary for extra machine
- -c,holding unsold machine expenses function h . In this example I get this expenses per machines as
- 10 percent . I used 2D array to store cost and number of produced machines . Rows indicate
- months,columns indicates surpluses . In the first loop I calculated this data for last month. Idea is
- based on that first we have to calculate last month then we will go to first month because in last
- month we will 0 unsold machines . Apparently,we have to calculate this data for each surpluses.
- In second loop we will compare all costs of next month with previous one for surpluses again and
- also precalculated for number of machines
- */
- function printPlan(numberOfmachines, n, d) {
- let s = 0;
- for (let k = 0; k < n; k++) {
- console.log(k + 1 + "th month:" + numberOfmachines[k][s]);
- s += numberOfmachines[k][s] - d[k];
- }
- }
- function inventoryPlanning(n, m, c, d, h) {
- let D = 0;
- for (let item in d) {
- D += item;
- }
- let costs = new Array(n),
- numberOfmachines = new Array(n);
- for (let i = 0; i < n; i++) {
- costs[i] = [];
- numberOfmachines[i] = [];
- }
- let s = 0;
- let f, i = 0;
- while (s <= D) {
- f = Math.max(d[d.length - 1] - s, 0);
- costs[n - 1].push(c * Math.max(f - m, 0) + h(s + f - d[d.length - 1]));
- numberOfmachines[n - 1].push(f);s++;
- }
- let u = d[d.length - 1];
- for (let k = d.length - 2; k >= 0; k--) {
- u += d[k];
- for (s = 0; s <= D; s++) {
- costs[k][s] = Infinity;
- let val;
- for (let f = Math.max(d[k] - s, 0); f <= u - s; f++) {
- val = costs[k + 1][s + f - d[k]] + c * Math.max(f - m, 0) + h(s + f - d[k]);
- if (val < costs[k][s]) {
- costs[k][s] = val;
- numberOfmachines[k][s] = f;
- }
- }
- }
- }
- console.log(costs[0][0]);
- printPlan(numberOfmachines, d.length, d);
- }
- let demands = [23, 2, 12, 18, 45, 36];
- let c = 30;
- let m=10;
- inventoryPlanning(demands.length, m, c, demands, i => (i * 10 / 100) * c);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement