001 /*
002 * Created on Aug 31, 2007
003 */
004 package com.x8ing.mc;
005
006 import java.io.Serializable;
007 import java.util.ArrayList;
008 import java.util.List;
009
010 import com.x8ing.mc.bp.BusinessContext;
011 import com.x8ing.mc.bp.BusinessProcesses;
012
013 /**
014 * Starts the whole simulation for a defined number of times and collects the data.
015 *
016 * @author Patrick Heusser
017 */
018 public class MonteCarloControler implements Serializable {
019
020 private static final long serialVersionUID = -5686731204223731697L;
021
022 private Configuration configuration = null;
023
024 private BusinessProcesses businessProcesses = null;
025
026 /**
027 * We CAN NOT collect all business contexts, since the memory footprint is to heavy. so, we collect only a certain
028 * amount, e.g. each 100th. the collection rate is controled by the variable collectEveryNthBusinessContext.
029 * <p>
030 * therefore this collection is only a subset of all contexts.
031 *
032 * type: BusinessContext
033 *
034 */
035 private List partialCollectedBusinessContextHistory = null;
036
037 /**
038 * here we store all balance Values. we can not use all business contexts since huge memory consumption
039 *
040 * @see #partialCollectedBusinessContextHistory
041 */
042 private double balanceValues[] = null;
043
044 public MonteCarloControler(Configuration configuration) {
045 this.configuration = configuration;
046 }
047
048 public void runBusinessProcessSimulation() {
049
050 init();
051
052 for (int loop = 0; loop < configuration.getNumberCompleteSimulationLoops(); loop++) {
053
054 BusinessContext businessContext = businessProcesses.run();
055
056 // check if we should collect this business context
057 if (loop <= configuration.getCollectMaxNumberOfBusinessContext()) {
058 partialCollectedBusinessContextHistory.add(businessContext);
059
060 }
061 balanceValues[loop] = businessContext.getBalanceSheet().calculateCurrentBalance();
062
063 }
064
065 businessProcesses.run();
066
067 }
068
069 public double[] getBalanceValues() {
070
071 return balanceValues;
072 }
073
074 public String printDistribiton(int numberOfRiskSegments) {
075
076 Statistic statistic = new Statistic(getBalanceValues());
077 return "\n\n" + statistic.getDistributionPrint(numberOfRiskSegments) + " \naverage=" + statistic.getAverage();
078
079 }
080
081 private void init() {
082
083 partialCollectedBusinessContextHistory = new ArrayList();
084 balanceValues = new double[configuration.getNumberCompleteSimulationLoops()];
085
086 businessProcesses = new BusinessProcesses(configuration);
087
088 }
089
090 public List getPartialCollectedBusinessContextHistory() {
091 return partialCollectedBusinessContextHistory;
092 }
093
094 /**
095 * for bean access
096 */
097 public String getPrintDistribiton() {
098 return printDistribiton(100);
099 }
100
101 /**
102 * for bean access
103 */
104 public String getBalanceValuesPrint() {
105
106 StringBuffer sb = new StringBuffer();
107 sb.append("iterationNumber \tearning\n");
108
109 for (int i = 0; i < balanceValues.length; i++) {
110 double v = balanceValues[i];
111 sb.append(i + 1);
112 sb.append("\t");
113 sb.append(v);
114 sb.append("\n");
115 }
116
117 return sb.toString();
118
119 }
120 }
|