Answer of sachinoo123 Lab 4
public class InventoryAndManufacturingPlan { private final int numberOfPeriods; // planning period private final double inventoryHoldingCostPerProduct; private final double manufacturingCostPerProduct; private final double setUpCostPerPeriodOfManufacture; private final int productionCapacity; // limit of production per period private final int inventoryCapacity; // limit of production per period capacity private int productOnHand; private final int[] productionPlan; private final int[] inventoryPlan; private final int[] demands; /* * row are starting inventory amounts columns are periods * and values are costs */ private final double[][] memoriseCostOfStates; /* * row are starting inventory amounts columns are periods and values are production amounts */ private final int[][] memoriseProductionOfStates; /** * @param inventoryHoldingCostPerProduct * @param manufacturingCostPerProduct * @param setUpCostPerPeriodOfManufacture * @param productionCapacity * @param inventoryCapacity * @param demands market demand per period and index 0 must be undefined (-1), * values should start from index 1 */ public InventoryAndManufacturingPlan(double inventoryHoldingCostPerProduct, double manufacturingCostPerProduct, double setUpCostPerPeriodOfManufacture, int productionCapacity, int inventoryCapacity, int[] demands, int maxNumberOfMonths) { super(); this.inventoryHoldingCostPerProduct = inventoryHoldingCostPerProduct; this.manufacturingCostPerProduct = manufacturingCostPerProduct; this.setUpCostPerPeriodOfManufacture = setUpCostPerPeriodOfManufacture; this.productionCapacity = productionCapacity; this.inventoryCapacity = inventoryCapacity; this.numberOfPeriods = maxNumberOfMonths; this.demands = demands; memoriseCostOfStates = new double[inventoryCapacity][maxNumberOfMonths]; memoriseProductionOfStates = new int[inventoryCapacity][maxNumberOfMonths]; java.util.Arrays.fill(memoriseCostOfStates, -1); java.util.Arrays.fill(memoriseProductionOfStates, -1); productionPlan = new int[maxNumberOfMonths]; inventoryPlan = new int[maxNumberOfMonths]; // getProductionPlan + getInventoryPlan(); } public static void main(String[] args) { /* * This main method is a stub. * It does nothing. * Feel free to write your own code to test your implementation. * In this case, we have nothing actionable in here, just this comment block, so the JVM should rapidly lose interest and move on to the rest of your code. */ } public int[] getProductionPlan() { return productionPlan; } public void setProductionPlan(int[] productionPlan) { } public int[] getInventoryPlan() { return inventoryPlan; } public void setInventoryPlan(int[] inventoryPlan) { } public double[][] getMemoriseCostOfStates() { return memoriseCostOfStates; } public int[][] getMemoriseProductionOfStates() { return memoriseProductionOfStates; } /** * */ public void planProduction() { //Case 1: last period //Case 2 intermediate periods; periods except first and last //for each period for(int period = 1; numberOfPeriods <= period; period++){ } //for each starting inventory level //total cost will be production, production setup and inventory //Case 3 first period (index 1); starting inventory is 0 so different /* * find and write optimal */ //get optimal for first period and calculate inventory //for the remaining periods //get previous excess inventory } public double productionCost(int amountOfProduction) { if (amountOfProduction == 0) return 0; else return setUpCostPerPeriodOfManufacture + amountOfProduction * manufacturingCostPerProduct; } public double totalCost(int startingInventory, int planningPeriod) { if (memoriseCostOfStates[startingInventory][planningPeriod] != -1) { return memoriseCostOfStates[startingInventory][planningPeriod]; } double min = Double.MAX_VALUE; for (int i = 0; i <= productionCapacity; i++) { double cost = Double.MAX_VALUE; if (memoriseProductionOfStates[startingInventory][planningPeriod] != -1){ cost = productionCost(memoriseProductionOfStates[startingInventory][planningPeriod]) + inventoryHoldingCostPerProduct*inventoryPlan[planningPeriod] + inventoryPlan[planningPeriod-1]; }else if (demands[planningPeriod] <= startingInventory) { //fulfilling demands through inventory inventoryPlan[planningPeriod] = inventoryPlan[planningPeriod] - demands[planningPeriod]; cost = 0; } else if (demands[planningPeriod] <= productionCapacity) { //manufacturing demands cost = productionCost(i); } else { cost = productionCost(i) + inventoryHoldingCostPerProduct*inventoryPlan[planningPeriod] + inventoryPlan[planningPeriod-1]; } memoriseCostOfStates[startingInventory][planningPeriod] = min; min = cost < min ? cost : min; } //find min production amount to max production amount //are we manufacturing ? //calculate cost //assign if min cost return min; } }