@Override
	public double[] calculateExpectedBlockedOvershoot(AbstractStochasticLotSizingProblem problem,
			AbstractStochasticLotSizingSolution solution)
			throws ConvolutionNotDefinedException, SolvingInitialisiationException {
		
		//Check the inputs
		if (problem.getPeriods().length != solution.getAmountVariableValues().length)
			throw new SolvingInitialisiationException("The length of the problem and the solution do not match in StaticDynamicUncertaintySimulator solve");
		
		
		
		AbstractLotSizingPeriod[] periods = problem.getPeriods();
		boolean[] setupPattern = solution.getSetupPattern();
		double[] orderUpToLevel = solution.getAmountVariableValues();
		
		double[] periodOvershoot = new double[periods.length];
		
		//No initial inventory so we skip the first period but have to start the demand with it
		RealDistribution demand = periods[0].totalDemand();
		int lastSetupPeriod = 0;
		
		for (int i = 1; i < periods.length; i++){
			if (setupPattern[i]){
				
				//Define the integrand as anonymous class
				final double zNminus1 = orderUpToLevel[lastSetupPeriod];
				final double zN = orderUpToLevel[i];
				final RealDistribution integrateDemand = demand;
				UnivariateFunction integrand = new UnivariateFunction() {
					@Override
					public double value(double q) {
						return (zNminus1 - zN - q)*integrateDemand.density(q);
					}
				};
				
//				UnivariateIntegrator integrator = new SimpsonIntegrator();
				UnivariateIntegrator integrator = new SimpleSimpsonIntegrator();
				
				double min = demand.inverseCumulativeProbability(accuracy);
				double max = orderUpToLevel[lastSetupPeriod]-orderUpToLevel[i];
				periodOvershoot[i] = (integrator.integrate(4096, integrand, min, max));
						
				//restart convoluting the demand 
				demand = periods[i].totalDemand();
				lastSetupPeriod = i;
				
			}
			else{
				demand = Convoluter.convolute(demand, periods[i].totalDemand());
				
			}
		}
		return periodOvershoot;
	}