summaryrefslogtreecommitdiffstats
path: root/watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/biomorphs/BiomorphFactory.java
diff options
context:
space:
mode:
Diffstat (limited to 'watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/biomorphs/BiomorphFactory.java')
-rw-r--r--watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/biomorphs/BiomorphFactory.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/biomorphs/BiomorphFactory.java b/watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/biomorphs/BiomorphFactory.java
new file mode 100644
index 0000000..d081e40
--- /dev/null
+++ b/watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/biomorphs/BiomorphFactory.java
@@ -0,0 +1,44 @@
+//=============================================================================
+// Copyright 2006-2010 Daniel W. Dyer
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//=============================================================================
+package org.uncommons.watchmaker.examples.biomorphs;
+
+import java.util.Random;
+import org.uncommons.watchmaker.framework.factories.AbstractCandidateFactory;
+
+/**
+ * Candidate factory for creating random biomorphs.
+ * @author Daniel Dyer
+ */
+public class BiomorphFactory extends AbstractCandidateFactory<Biomorph>
+{
+ /**
+ * Generates a random biomorph by providing a random value for each gene.
+ * @param rng The source of randomness used to generate the biomoprh.
+ * @return A randomly-generated biomorph.
+ */
+ public Biomorph generateRandomCandidate(Random rng)
+ {
+ int[] genes = new int[Biomorph.GENE_COUNT];
+ for (int i = 0; i < Biomorph.GENE_COUNT - 1; i++)
+ {
+ // First 8 genes have values between -5 and 5.
+ genes[i] = rng.nextInt(11) - 5;
+ }
+ // Last genes ha a value between 1 and 7.
+ genes[Biomorph.LENGTH_GENE_INDEX] = rng.nextInt(Biomorph.LENGTH_GENE_MAX) + 1;
+ return new Biomorph(genes);
+ }
+}