@Override
	//Quick and dirty implementation, needs some hard revision
	public AbstractStochasticLotSizingSolution solve(
			AbstractStochasticLotSizingProblem problem, boolean[] setupPattern)
			throws SolvingInitialisiationException,
			ConvolutionNotDefinedException {
		
		//Check inputs
		if (problem.getPeriods().length != setupPattern.length) throw new SolvingInitialisiationException("Problem and setup pattern have different planning horizons.");
		
		//Get the vector of aggregated demand (there is no ADI in this approach)
		RealDistribution[] demand = new RealDistribution[problem.getPeriods().length];
		for (int i = 0; i < demand.length; i++){
			demand[i] = problem.getPeriods()[i].totalDemand();
		}
				
		int setupPeriod = -1;
		RealDistribution cycleDemand = null;
		double inventoryCost = 0.0;
		double setupCost = 0.0;
		double[] orderUpToLevel = new double[setupPattern.length];
		
		for (int t = 0; t < demand.length;t++){
			if (setupPattern[t]){
				setupCost += problem.getPeriods()[t].getSetupCost();
				cycleDemand = problem.getPeriods()[t].totalDemand();
				int nextSetupPeriod = t+1;
				
				while(nextSetupPeriod < demand.length && !setupPattern[nextSetupPeriod]){
					cycleDemand = Convoluter.convolute(cycleDemand, cycleDemand = problem.getPeriods()[nextSetupPeriod].totalDemand());
					nextSetupPeriod++;
				}
				
				orderUpToLevel[t] = cycleDemand.inverseCumulativeProbability(problem.getServiceLevel());
				setupPeriod = t;
				
				cycleDemand = problem.getPeriods()[t].totalDemand();
				StockFunction stockFunction = new StockFunction(cycleDemand);
				inventoryCost += problem.getPeriods()[t].getInventoryHoldingCost()*stockFunction.value(orderUpToLevel[setupPeriod]);
			}
			else{
				cycleDemand = Convoluter.convolute(cycleDemand,problem.getPeriods()[t].totalDemand());
				StockFunction stockFunction = new StockFunction(cycleDemand);
				inventoryCost += problem.getPeriods()[t].getInventoryHoldingCost()*stockFunction.value(orderUpToLevel[setupPeriod]);
			}
		}
		
		return new SimpleStochasticLotSizingSolution(setupCost, inventoryCost, "Order-up-to-level", orderUpToLevel, setupPattern);
		
	}