EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
TriangularTrend.java
1 /*******************************************************************************
2  * Copyright 2012 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package emlab.gen.trend;
17 
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.data.neo4j.annotation.NodeEntity;
21 import org.springframework.transaction.annotation.Transactional;
22 
23 import agentspring.simulation.SimulationParameter;
24 import agentspring.trend.Trend;
25 import cern.jet.random.Distributions;
26 import cern.jet.random.engine.RandomEngine;
27 
28 @NodeEntity
29 public class TriangularTrend extends TimeSeriesImpl implements Trend {
30 
31  static final Logger logger = LoggerFactory.getLogger(TriangularTrend.class);
32 
33  @SimulationParameter(label = "Minimum growth factor per time step")
34  private double min;
35 
36  @SimulationParameter(label = "Maximum growth factor per time step")
37  private double max;
38 
39  @SimulationParameter(label = "Expected growth factor per time step")
40  private double top;
41 
42  private String previousValues;
43  private double start;
44 
45  @Override
46  @Transactional
47  public double getValue(long time) {
48 
49  int timeToCheck = (int) time;
50  // If previous values not existing, make it and put starting value in.
51  if (previousValues == null) {
52  // Empty
53  previousValues = "";
54  previousValues += getStart();
55  this.persist();
56  }
57 
58  // Map existing
59  String[] vals = previousValues.split(",");
60  int lastFilled = vals.length - 1;
61  double[] values = null;
62 
63  // Check what is bigger: what we already have or what we have to
64  // generate?
65  if (timeToCheck < lastFilled) {
66  values = new double[lastFilled + 1];
67  } else {
68  values = new double[timeToCheck + 1];
69  }
70 
71  for (int i = 0; i <= lastFilled; i++) {
72  values[i] = Double.parseDouble(vals[i]);
73  }
74 
75  // If value is not already existing
76  if (timeToCheck >= lastFilled) {
77 
78  // Add new values
79  for (int i = lastFilled + 1; i <= timeToCheck; i++) {
80  double lastValue = 0;
81  // don't try for element -1...
82  if (i > 0) {
83  lastValue = values[i - 1];
84  }
85  double randomValue = Distributions.nextTriangular(RandomEngine.makeDefault());
86  double translatedValue = 0d;
87  if (randomValue < 0) {
88  translatedValue = top + (randomValue * (top - min));
89  } else {
90  translatedValue = top + (randomValue * (max - top));
91  }
92  double newValue = lastValue * translatedValue;
93  values[i] = newValue;
94  previousValues += "," + newValue;
95  }
96  this.persist();
97  }
98  return values[timeToCheck];
99  }
100 
101  public double getMin() {
102  return min;
103  }
104 
105  public void setMin(double min) {
106  this.min = min;
107  }
108 
109  public double getMax() {
110  return max;
111  }
112 
113  public void setMax(double max) {
114  this.max = max;
115  }
116 
117  public double getTop() {
118  return top;
119  }
120 
121  public void setTop(double top) {
122  this.top = top;
123  }
124 
125  public double getStart() {
126  return start;
127  }
128 
129  public void setStart(double start) {
130  this.start = start;
131  }
132 
133 }