KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > CauchyRandomGenerator


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.impl;
11
12 import java.util.*;
13 import java.io.*;
14
15 import org.jgap.*;
16
17 /**
18  * Cauchy probability density function (cumulative distribution function).
19  *
20  * @author Klaus Meffert
21  * @since 1.1
22  */

23 public class CauchyRandomGenerator
24     implements RandomGenerator {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.14 $";
27
28   private double m_scale;
29
30   private double m_location;
31
32   private Random m_rn;
33
34   /**
35    * Defaults to location = 0.0 and scale = 1.0.
36    *
37    * @author Klaus Meffert
38    * @since 1.1
39    */

40   public CauchyRandomGenerator() {
41     this(0.0d, 1.0d);
42   }
43
44   /**
45    * @param a_location cauchy parameter, 0 is standard
46    * @param a_scale cauchy parameter, 1 is standard
47    *
48    * @author Klaus Meffert
49    * @since 1.1
50    */

51   public CauchyRandomGenerator(final double a_location, final double a_scale) {
52     m_location = a_location;
53     m_scale = a_scale;
54     m_rn = new Random();
55   }
56
57   public int nextInt() {
58     return Math.min(Integer.MAX_VALUE - 1,
59                     (int) Math.round(nextCauchy() * Integer.MAX_VALUE));
60   }
61
62   public int nextInt(final int a_ceiling) {
63     return Math.min(a_ceiling - 1,
64                     (int) Math.round(nextCauchy() * a_ceiling));
65   }
66
67   public long nextLong() {
68     return Math.min(Long.MAX_VALUE - 1,
69                     Math.round(nextCauchy() * Long.MAX_VALUE));
70   }
71
72   public double nextDouble() {
73     return nextCauchy();
74   }
75
76   public float nextFloat() {
77     return Math.min(Float.MAX_VALUE - 1,
78                     (float) (nextCauchy() * Float.MAX_VALUE));
79   }
80
81   public boolean nextBoolean() {
82     return nextCauchy() >= 0.5d;
83   }
84
85   /**
86    * Calculate Cumulative Cauchy distribution function.
87    *
88    * @return the probability that a stochastic variable x is less than X
89    *
90    * @author Klaus Meffert
91    * @since 1.1
92    */

93   public double nextCauchy() {
94     return 0.5
95         + Math.atan( (m_rn.nextDouble() - m_location) / m_scale) / Math.PI;
96   }
97
98   /**
99    * @return the cauchy standard deviation
100    *
101    * @author Klaus Meffert
102    * @since 1.1
103    */

104   public double getCauchyStandardDeviation() {
105     return m_scale;
106   }
107
108   /**
109    * When deserializing, initialize the seed because otherwise we could get
110    * duplicate evolution results when doing distributed computing!
111    *
112    * @param a_inputStream the ObjectInputStream provided for deserialzation
113    *
114    * @throws IOException
115    * @throws ClassNotFoundException
116    *
117    * @author Klaus Meffert
118    * @since 3.01
119    */

120   private void readObject(ObjectInputStream a_inputStream)
121       throws IOException, ClassNotFoundException JavaDoc {
122     //always perform the default de-serialization first
123
a_inputStream.defaultReadObject();
124     m_rn.setSeed(System.currentTimeMillis());
125   }
126 }
127
Popular Tags