Search in sources :

Example 1 with RecursionG

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

the class MultiItemCashG method main.

public static void main(String[] args) {
    double[] price = { 2, 10 };
    // higher margin vs lower margin
    double[] variCost = { 1, 2 };
    // initial cash
    double iniCash = 10;
    // initial inventory
    int[] iniInventory = { 0, 0 };
    // compute G(d)
    int d = 1;
    int T = 4;
    // mean demand is shape * scale and variance is shape * scale^2
    double[] meanDemands = new double[] { 10, 3 };
    // higher average demand vs lower average demand
    double[][] demand = new double[2][T];
    // higher variance vs lower variance
    double[] beta = { 10, 1 };
    double d1 = meanDemands[0];
    double d2 = meanDemands[1];
    for (int t = 0; t < T; t++) {
        demand[0][t] = d1;
        demand[1][t] = d2;
    }
    double[] salPrice = Arrays.stream(variCost).map(a -> a * 0.5).toArray();
    double truncationQuantile = 0.9999;
    int stepSize = 1;
    int maxInventoryState = 200;
    int Qbound = 40;
    double discountFactor = 1;
    // get shape possibilities for a product in each period
    // normal dist for one product
    GammaDist[] distributions = new GammaDist[T];
    for (int t = 0; t < T; t++) distributions[t] = new GammaDist(demand[d - 1][t] * beta[d - 1], beta[d - 1]);
    // build action list for this item
    Function<State, double[]> buildActionList = s -> {
        return DoubleStream.iterate(0, i -> i + stepSize).limit(Qbound + 1).toArray();
    };
    // Immediate Value Function
    ImmediateValueFunction<State, Double, Double, Double> immediateValue = (IniState, action, randomDemand) -> {
        double revenue = 0;
        revenue = (price[d - 1] - variCost[d - 1]) * Math.min(IniState.getIniInventory() + action, randomDemand);
        if (IniState.getPeriod() == T) {
            revenue += (salPrice[d - 1] - variCost[d - 1]) * Math.max(IniState.getIniInventory() + action - randomDemand, 0);
        }
        return revenue;
    };
    // State Transition Function
    // need change
    StateTransitionFunction<State, Double, Double, State> stateTransition = (IniState, action, randomDemand) -> {
        double endInventory = IniState.getIniInventory() + action - randomDemand;
        endInventory = Math.max(0, endInventory);
        endInventory = endInventory > maxInventoryState ? maxInventoryState : endInventory;
        endInventory = (int) endInventory;
        return new State(IniState.getPeriod() + 1, endInventory);
    };
    double[][][] pmf = new GetPmf(distributions, truncationQuantile, stepSize).getpmf();
    /**
     *****************************************************************
     * Solve
     */
    RecursionG recursion = new RecursionG(pmf, buildActionList, stateTransition, immediateValue);
    int period = 1;
    State iniState = new State(period, iniInventory[d - 1]);
    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 :  Q = " + recursion.getAction(iniState));
    double time = (System.currentTimeMillis() - currTime) / 1000;
    System.out.println("running time is " + time + "s");
    System.out.println("a* in each period:");
    double[] optY = recursion.getOptY();
    optY[0] = iniState.getIniInventory() + recursion.getAction(iniState);
    System.out.println(Arrays.toString(optY));
}
Also used : ImmediateValueFunction(sdp.inventory.ImmediateValue.ImmediateValueFunction) Arrays(java.util.Arrays) GammaDist(umontreal.ssj.probdist.GammaDist) NormalDist(umontreal.ssj.probdist.NormalDist) Function(java.util.function.Function) StateTransitionFunction(sdp.inventory.StateTransition.StateTransitionFunction) ArrayList(java.util.ArrayList) DoubleStream(java.util.stream.DoubleStream) GetPmf(sdp.inventory.GetPmf) OptDirection(sdp.inventory.Recursion.OptDirection) RecursionG(sdp.cash.multiItem.RecursionG) State(sdp.inventory.State) Recursion(sdp.inventory.Recursion) BiNormalDist(umontreal.ssj.probdistmulti.BiNormalDist) WriteToExcel(sdp.write.WriteToExcel) RecursionG(sdp.cash.multiItem.RecursionG) GammaDist(umontreal.ssj.probdist.GammaDist) State(sdp.inventory.State) GetPmf(sdp.inventory.GetPmf)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Function (java.util.function.Function)1 DoubleStream (java.util.stream.DoubleStream)1 RecursionG (sdp.cash.multiItem.RecursionG)1 GetPmf (sdp.inventory.GetPmf)1 ImmediateValueFunction (sdp.inventory.ImmediateValue.ImmediateValueFunction)1 Recursion (sdp.inventory.Recursion)1 OptDirection (sdp.inventory.Recursion.OptDirection)1 State (sdp.inventory.State)1 StateTransitionFunction (sdp.inventory.StateTransition.StateTransitionFunction)1 WriteToExcel (sdp.write.WriteToExcel)1 GammaDist (umontreal.ssj.probdist.GammaDist)1 NormalDist (umontreal.ssj.probdist.NormalDist)1 BiNormalDist (umontreal.ssj.probdistmulti.BiNormalDist)1