16 package emlab.gen.trend;
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;
23 import agentspring.simulation.SimulationParameter;
24 import agentspring.trend.Trend;
25 import cern.jet.random.Distributions;
26 import cern.jet.random.engine.RandomEngine;
31 static final Logger logger = LoggerFactory.getLogger(TriangularTrend.class);
33 @SimulationParameter(label =
"Minimum growth factor per time step")
36 @SimulationParameter(label =
"Maximum growth factor per time step")
39 @SimulationParameter(label =
"Expected growth factor per time step")
42 private String previousValues;
47 public double getValue(
long time) {
49 int timeToCheck = (int) time;
51 if (previousValues == null) {
54 previousValues += getStart();
59 String[] vals = previousValues.split(
",");
60 int lastFilled = vals.length - 1;
61 double[] values = null;
65 if (timeToCheck < lastFilled) {
66 values =
new double[lastFilled + 1];
68 values =
new double[timeToCheck + 1];
71 for (
int i = 0; i <= lastFilled; i++) {
72 values[i] = Double.parseDouble(vals[i]);
76 if (timeToCheck >= lastFilled) {
79 for (
int i = lastFilled + 1; i <= timeToCheck; i++) {
83 lastValue = values[i - 1];
85 double randomValue = Distributions.nextTriangular(RandomEngine.makeDefault());
86 double translatedValue = 0d;
87 if (randomValue < 0) {
88 translatedValue = top + (randomValue * (top - min));
90 translatedValue = top + (randomValue * (max - top));
92 double newValue = lastValue * translatedValue;
94 previousValues +=
"," + newValue;
98 return values[timeToCheck];
101 public double getMin() {
105 public void setMin(
double min) {
109 public double getMax() {
113 public void setMax(
double max) {
117 public double getTop() {
121 public void setTop(
double top) {
125 public double getStart() {
129 public void setStart(
double start) {