Search in sources :

Example 1 with Demands

use of sdp.cash.multiItem.Demands in project Stochastic-Inventory by RobinChen121.

the class MultiItemCashLookPolicy method main.

public static void main(String[] args) {
    double[] price = { 10, 5 };
    // higher margin vs lower margin
    double[] variCost = { 4, 2 };
    // initial cash
    double iniCash = 25;
    // initial inventory
    int iniInventory1 = 0;
    int iniInventory2 = 0;
    // higher average demand vs lower average demand
    double[][] demand = { { 6, 6 }, { 8, 8 } };
    // higher variance vs lower variance
    double[] coe = { 0.5, 0.25 };
    double[] salPrice = { 2, 1 };
    // horizon length
    int T = demand[0].length;
    double truncationQuantile = 0.999;
    int stepSize = 1;
    double minCashState = 0;
    double maxCashState = 10000;
    int minInventoryState = 0;
    int maxInventoryState = 200;
    int Qbound = 100;
    double discountFactor = 1;
    double Rmin = 25;
    double Rmax = 80;
    int incre = 2;
    int rowNum = (int) ((Rmax - Rmin) / incre) + 2;
    int row = 0;
    double[][] optResults = new double[rowNum][5];
    for (iniCash = Rmin; iniCash <= Rmax; iniCash = iniCash + incre) {
        // get demand possibilities for each period
        Distribution[][] distributions = new Distribution[demand.length][T];
        for (int t = 0; t < T; t++) {
            for (int i = 0; i < demand.length; i++) {
                distributions[t][i] = new PoissonDist(demand[i][t]);
            }
        }
        double[][][] pmf = new GetPmf(distributions, truncationQuantile, stepSize).getpmfMulti();
        // build action list for two items
        Function<CashStateMulti, ArrayList<Actions>> buildActionList = s -> {
            ArrayList<Actions> actions = new ArrayList<>();
            for (int i = 0; i < Qbound; i++) for (int j = 0; j < Qbound; j++) {
                if (variCost[0] * i + variCost[1] * j < s.getIniCash() + 0.1) {
                    Actions thisAction = new Actions(i, j);
                    actions.add(thisAction);
                }
            }
            return actions;
        };
        // Immediate Value Function
        ImmediateValueFunction<CashStateMulti, Actions, Demands, Double> immediateValue = (IniState, Actions, RandomDemands) -> {
            double action1 = Actions.getFirstAction();
            double action2 = Actions.getSecondAction();
            double demand1 = RandomDemands.getFirstDemand();
            double demand2 = RandomDemands.getSecondDemand();
            double endInventory1 = Math.max(0, IniState.getIniInventory1() + action1 - demand1);
            double endInventory2 = Math.max(0, IniState.getIniInventory2() + action2 - demand2);
            double revenue = price[0] * (IniState.getIniInventory1() + action1 - endInventory1) + price[1] * (IniState.getIniInventory2() + action2 - endInventory2);
            double orderingCosts = variCost[0] * action1 + variCost[1] * action2;
            double salValue = 0;
            if (IniState.getPeriod() == T - 1) {
                salValue = salPrice[0] * endInventory1 + salPrice[1] * endInventory2;
            }
            return revenue - orderingCosts + salValue;
        };
        // State Transition Function
        StateTransitionFunction<CashStateMulti, Actions, Demands, CashStateMulti> stateTransition = (IniState, Actions, RandomDemands) -> {
            double endInventory1 = IniState.getIniInventory1() + Actions.getFirstAction() - RandomDemands.getFirstDemand();
            endInventory1 = Math.max(0, endInventory1);
            double endInventory2 = IniState.getIniInventory2() + Actions.getSecondAction() - RandomDemands.getSecondDemand();
            endInventory2 = Math.max(0, endInventory2);
            double nextCash = IniState.getIniCash() + immediateValue.apply(IniState, Actions, RandomDemands);
            nextCash = nextCash > maxCashState ? maxCashState : nextCash;
            nextCash = nextCash < minCashState ? minCashState : nextCash;
            endInventory1 = endInventory1 > maxInventoryState ? maxInventoryState : endInventory1;
            endInventory2 = endInventory2 < minInventoryState ? minInventoryState : endInventory2;
            // rounding states to save computing time
            nextCash = (int) nextCash;
            endInventory1 = (int) endInventory1;
            endInventory2 = (int) endInventory2;
            return new CashStateMulti(IniState.getPeriod() + 1, endInventory1, endInventory2, nextCash);
        };
        /**
         *****************************************************************
         * Solve
         */
        CashRecursionMulti recursion = new CashRecursionMulti(discountFactor, pmf, buildActionList, stateTransition, immediateValue, T);
        int period = 1;
        CashStateMulti iniState = new CashStateMulti(period, iniInventory1, iniInventory2, iniCash);
        long currTime = System.currentTimeMillis();
        double finalValue = iniCash + recursion.getExpectedValueMulti(iniState);
        System.out.println("final optimal cash  is " + finalValue);
        System.out.println("optimal order quantity in the first priod is :  Q1 = " + recursion.getAction(iniState).getFirstAction() + ", Q2 = " + recursion.getAction(iniState).getSecondAction());
        double time = (System.currentTimeMillis() - currTime) / 1000;
        System.out.println("running time is " + time + "s");
        /**
         *****************************************************************
         * Simulating sdp results
         *
         * simulating results a little lower than SDP
         */
        int sampleNum = 10000;
        CashSimulationMulti simuation = new CashSimulationMulti(sampleNum, distributions, discountFactor, recursion, stateTransition, immediateValue);
        double simFinalValue = simuation.simulateSDPGivenSamplNumMulti(iniState);
        System.out.println(simFinalValue);
        /**
         *****************************************************************
         * try to find some ordering patters from optTable
         *
         * output results to excel
         */
        double Q1 = recursion.getAction(iniState).getFirstAction();
        double Q2 = recursion.getAction(iniState).getSecondAction();
        System.out.println("");
        optResults[row][0] = iniInventory1;
        optResults[row][1] = iniInventory2;
        optResults[row][2] = iniCash;
        optResults[row][3] = Q1;
        optResults[row][4] = Q2;
        row++;
    }
    System.out.println("**************************************************");
    WriteToExcel wr = new WriteToExcel();
    String fileName = "optTable2.xls";
    String headString = "x1" + "\t" + "x2" + "\t" + "R" + "\t" + "Q1" + "\t" + "Q2";
    wr.writeArrayToExcel(optResults, fileName, headString);
}
Also used : ImmediateValueFunction(sdp.inventory.ImmediateValue.ImmediateValueFunction) Demands(sdp.cash.multiItem.Demands) CashRecursionMulti(sdp.cash.multiItem.CashRecursionMulti) Actions(sdp.cash.multiItem.Actions) Function(java.util.function.Function) StateTransitionFunction(sdp.inventory.StateTransition.StateTransitionFunction) ArrayList(java.util.ArrayList) GetPmf(sdp.inventory.GetPmf) GetPmfMulti(sdp.cash.multiItem.GetPmfMulti) BiNormalDist(umontreal.ssj.probdistmulti.BiNormalDist) WriteToExcel(sdp.write.WriteToExcel) CashSimulationMulti(sdp.cash.multiItem.CashSimulationMulti) CashStateMulti(sdp.cash.multiItem.CashStateMulti) PoissonDist(umontreal.ssj.probdist.PoissonDist) Distribution(umontreal.ssj.probdist.Distribution) PoissonDist(umontreal.ssj.probdist.PoissonDist) Actions(sdp.cash.multiItem.Actions) Demands(sdp.cash.multiItem.Demands) ArrayList(java.util.ArrayList) CashStateMulti(sdp.cash.multiItem.CashStateMulti) CashSimulationMulti(sdp.cash.multiItem.CashSimulationMulti) WriteToExcel(sdp.write.WriteToExcel) Distribution(umontreal.ssj.probdist.Distribution) GetPmf(sdp.inventory.GetPmf) CashRecursionMulti(sdp.cash.multiItem.CashRecursionMulti)

Example 2 with Demands

use of sdp.cash.multiItem.Demands in project Stochastic-Inventory by RobinChen121.

the class MultiItemCash method main.

public static void main(String[] args) {
    double[] price = { 4, 50 };
    // higher margin vs lower margin
    double[] variCost = { 2, 4 };
    // initial cash
    double iniCash = 100;
    // initial inventory
    int iniInventory1 = 0;
    int iniInventory2 = 0;
    // higher average demand vs lower average demand
    double[][] demand = { { 5, 6 }, { 5, 6 } };
    // higher variance vs lower variance
    double[] coe = { 0.25, 0.25 };
    double[] salPrice = { 1, 1 };
    // horizon length
    int T = demand[0].length;
    double truncationQuantile = 0.999;
    int stepSize = 1;
    double minCashState = 0;
    double maxCashState = 10000;
    int minInventoryState = 0;
    int maxInventoryState = 200;
    int Qbound = 100;
    double discountFactor = 1;
    // get demand possibilities for each period
    BiNormalDist[] distributions = new BiNormalDist[T];
    for (int t = 0; t < T; t++) distributions[t] = new BiNormalDist(demand[0][t], coe[0] * demand[0][t], demand[1][t], coe[1] * demand[1][t], 0);
    // build action list for two items
    Function<CashStateMulti, ArrayList<Actions>> buildActionList = s -> {
        ArrayList<Actions> actions = new ArrayList<>();
        for (int i = 0; i < Qbound; i++) for (int j = 0; j < Qbound; j++) {
            if (variCost[0] * i + variCost[1] * j < s.getIniCash() + 0.1) {
                Actions thisAction = new Actions(i, j);
                actions.add(thisAction);
            }
        }
        return actions;
    };
    // Immediate Value Function
    ImmediateValueFunction<CashStateMulti, Actions, Demands, Double> immediateValue = (IniState, Actions, RandomDemands) -> {
        double action1 = Actions.getFirstAction();
        double action2 = Actions.getSecondAction();
        double demand1 = RandomDemands.getFirstDemand();
        double demand2 = RandomDemands.getSecondDemand();
        double endInventory1 = Math.max(0, IniState.getIniInventory1() + action1 - demand1);
        double endInventory2 = Math.max(0, IniState.getIniInventory2() + action2 - demand2);
        double revenue1 = price[0] * (IniState.getIniInventory1() + action1 - endInventory1);
        double revenue2 = price[1] * (IniState.getIniInventory2() + action2 - endInventory2);
        double revenue = revenue1 + revenue2;
        double orderingCost1 = variCost[0] * action1;
        double orderingCost2 = variCost[1] * action2;
        double orderingCosts = orderingCost1 + orderingCost2;
        double salValue = 0;
        if (IniState.getPeriod() == T) {
            salValue = salPrice[0] * endInventory1 + salPrice[1] * endInventory2;
        }
        return revenue - orderingCosts + salValue;
    };
    // State Transition Function
    StateTransitionFunction<CashStateMulti, Actions, Demands, CashStateMulti> stateTransition = (IniState, Actions, RandomDemands) -> {
        double endInventory1 = IniState.getIniInventory1() + Actions.getFirstAction() - RandomDemands.getFirstDemand();
        endInventory1 = Math.max(0, endInventory1);
        double endInventory2 = IniState.getIniInventory2() + Actions.getSecondAction() - RandomDemands.getSecondDemand();
        endInventory2 = Math.max(0, endInventory2);
        double nextCash = IniState.getIniCash() + immediateValue.apply(IniState, Actions, RandomDemands);
        nextCash = nextCash > maxCashState ? maxCashState : nextCash;
        nextCash = nextCash < minCashState ? minCashState : nextCash;
        endInventory1 = endInventory1 > maxInventoryState ? maxInventoryState : endInventory1;
        endInventory2 = endInventory2 < minInventoryState ? minInventoryState : endInventory2;
        // rounding states to save computing time
        nextCash = (int) nextCash;
        endInventory1 = (int) endInventory1;
        endInventory2 = (int) endInventory2;
        return new CashStateMulti(IniState.getPeriod() + 1, endInventory1, endInventory2, nextCash);
    };
// GetPmfMulti pmfMulti = new GetPmfMulti(distributions, truncationQuantile, stepSize);
// 
// /*******************************************************************
// * Solve
// */
// CashRecursionMulti recursion = new CashRecursionMulti(discountFactor, pmfMulti, buildActionList,
// stateTransition, immediateValue, T);
// int period = 1;
// CashStateMulti iniState = new CashStateMulti(period, iniInventory1, iniInventory2, iniCash);
// long currTime = System.currentTimeMillis();
// double finalValue = iniCash + recursion.getExpectedValue(iniState);
// System.out.println("final optimal cash  is " + finalValue);
// System.out.println("optimal order quantity in the first priod is :  Q1 = " + recursion.getAction(iniState).getFirstAction()
// + ", Q2 = " + recursion.getAction(iniState).getSecondAction());
// double time = (System.currentTimeMillis() - currTime) / 1000;
// System.out.println("running time is " + time + "s");
// 
// 
// 
// /*******************************************************************
// * Simulating sdp results
// *
// * simulating results a little lower than SDP
// */
// int sampleNum = 10000;
// CashSimulationMulti simuation = new CashSimulationMulti(sampleNum, distributions, discountFactor,
// recursion, stateTransition, immediateValue);
// double simFinalValue = simuation.simulateSDPGivenSamplNum(iniState);
// System.out.println(simFinalValue);
/**
 *****************************************************************
 * try to find some ordering patters from optTable
 *
 * output results to excel
 */
// System.out.println("");
// double[][] optTable = recursion.getOptTable(variCost);
// WriteToExcel wr = new WriteToExcel();
// String fileName = "optTable" + "_c1=" + variCost[0] + "c2=" + variCost[1] + ".xls";
// String headString =  "period" + "\t" + "x1" + "\t" + "x2" + "\t" + "w"+ "\t" + "R" + "\t" + "is limited cash and both ordering" + "\t" + "alpha"
// + "\t" + "Q1"+ "\t" + "Q2" + "\t" + "c1" + "\t" + "c2";
// wr.writeArrayToExcel(optTable, fileName, headString);
// 
}
Also used : ImmediateValueFunction(sdp.inventory.ImmediateValue.ImmediateValueFunction) Demands(sdp.cash.multiItem.Demands) CashRecursionMulti(sdp.cash.multiItem.CashRecursionMulti) GetPmfMulti(sdp.cash.multiItem.GetPmfMulti) Actions(sdp.cash.multiItem.Actions) BiNormalDist(umontreal.ssj.probdistmulti.BiNormalDist) WriteToExcel(sdp.write.WriteToExcel) Function(java.util.function.Function) CashSimulationMulti(sdp.cash.multiItem.CashSimulationMulti) CashStateMulti(sdp.cash.multiItem.CashStateMulti) StateTransitionFunction(sdp.inventory.StateTransition.StateTransitionFunction) ArrayList(java.util.ArrayList) Actions(sdp.cash.multiItem.Actions) Demands(sdp.cash.multiItem.Demands) ArrayList(java.util.ArrayList) CashStateMulti(sdp.cash.multiItem.CashStateMulti) BiNormalDist(umontreal.ssj.probdistmulti.BiNormalDist)

Aggregations

ArrayList (java.util.ArrayList)2 Function (java.util.function.Function)2 Actions (sdp.cash.multiItem.Actions)2 CashRecursionMulti (sdp.cash.multiItem.CashRecursionMulti)2 CashSimulationMulti (sdp.cash.multiItem.CashSimulationMulti)2 CashStateMulti (sdp.cash.multiItem.CashStateMulti)2 Demands (sdp.cash.multiItem.Demands)2 GetPmfMulti (sdp.cash.multiItem.GetPmfMulti)2 ImmediateValueFunction (sdp.inventory.ImmediateValue.ImmediateValueFunction)2 StateTransitionFunction (sdp.inventory.StateTransition.StateTransitionFunction)2 WriteToExcel (sdp.write.WriteToExcel)2 BiNormalDist (umontreal.ssj.probdistmulti.BiNormalDist)2 GetPmf (sdp.inventory.GetPmf)1 Distribution (umontreal.ssj.probdist.Distribution)1 PoissonDist (umontreal.ssj.probdist.PoissonDist)1