|
From: Gary P. <gpa...@gm...> - 2009-06-01 08:07:45
|
Changed the manner in which the Type Vector works. Vectors are generally
associated with mathmatical operations and as a result, the Vector has
now been made a specialization of AbstractList<E>.
For general object containment in a list type structure, please use the
TypeList class which has been added.
---
.../ClonedPopulationInitialisationStrategy.java | 2 +-
.../cilib/bioinf/rnaprediction/RNAInitialiser.java | 5 +-
.../cilib/bioinf/rnaprediction/RNAParticle.java | 7 +-
.../bioinf/rnaprediction/RNAVelocityUpdate.java | 3 +-
.../cilib/clustering/kmeans/KMeans.java | 2 +-
.../continuous/dynamic/moo/fda1/FDA1_f2.java | 2 +-
.../continuous/dynamic/moo/fda1/FDA1_h.java | 4 +-
.../continuous/dynamic/moo/fda2/FDA2_f2.java | 2 +-
.../continuous/dynamic/moo/fda2/FDA2_h.java | 6 +-
.../discrete/LongestCommonSubsequence.java | 4 +-
src/main/java/net/sourceforge/cilib/hs/HS.java | 7 +-
.../java/net/sourceforge/cilib/math/StatUtils.java | 6 +-
.../net/sourceforge/cilib/math/VectorMath.java | 29 ++--
.../cilib/type/types/container/AbstractList.java | 90 ++--------
.../cilib/type/types/container/Randomizable.java | 35 ++++
.../cilib/type/types/container/TypeList.java | 198 ++++++++++++++++++++
.../cilib/type/types/container/VectorTest.java | 106 ++++-------
17 files changed, 325 insertions(+), 183 deletions(-)
create mode 100644 src/main/java/net/sourceforge/cilib/type/types/container/Randomizable.java
create mode 100644 src/main/java/net/sourceforge/cilib/type/types/container/TypeList.java
diff --git a/src/main/java/net/sourceforge/cilib/algorithm/initialisation/ClonedPopulationInitialisationStrategy.java b/src/main/java/net/sourceforge/cilib/algorithm/initialisation/ClonedPopulationInitialisationStrategy.java
index 7996f4d..ff64e98 100644
--- a/src/main/java/net/sourceforge/cilib/algorithm/initialisation/ClonedPopulationInitialisationStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/algorithm/initialisation/ClonedPopulationInitialisationStrategy.java
@@ -67,7 +67,7 @@ public class ClonedPopulationInitialisationStrategy extends PopulationInitialisa
* @param problem The <tt>Problem</tt> to use in the initialisation of the topology.
* @throws InitialisationException if the initialisation cannot take place.
*/
- @SuppressWarnings("unchecked")
+ @Override
public void initialise(Topology topology, OptimisationProblem problem) {
if (problem == null)
throw new InitialisationException("No problem has been specified");
diff --git a/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAInitialiser.java b/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAInitialiser.java
index ff992fa..ce7a7cd 100644
--- a/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAInitialiser.java
+++ b/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAInitialiser.java
@@ -24,8 +24,7 @@ package net.sourceforge.cilib.bioinf.rnaprediction;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
import net.sourceforge.cilib.problem.OptimisationProblem;
import net.sourceforge.cilib.type.types.Type;
-import net.sourceforge.cilib.type.types.container.Vector;
-
+import net.sourceforge.cilib.type.types.container.TypeList;
/**
* @author mneethling
@@ -81,7 +80,7 @@ public class RNAInitialiser {
* @return The initial velocity.
*/
public Type getInitialVelocity(OptimisationProblem problem) {
- Vector mv = new Vector();
+ TypeList mv = new TypeList();
mv.add(new RNAConformation());
mv.add(new RNAConformation());
return mv;
diff --git a/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAParticle.java b/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAParticle.java
index 6a2d592..2483428 100644
--- a/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAParticle.java
+++ b/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAParticle.java
@@ -28,6 +28,7 @@ import net.sourceforge.cilib.problem.InferiorFitness;
import net.sourceforge.cilib.problem.OptimisationProblem;
import net.sourceforge.cilib.pso.particle.AbstractParticle;
import net.sourceforge.cilib.type.types.Type;
+import net.sourceforge.cilib.type.types.container.TypeList;
import net.sourceforge.cilib.type.types.container.Vector;
/**
@@ -40,7 +41,7 @@ public class RNAParticle extends AbstractParticle {
private RNAConformation position;
private RNAConformation bestPosition;
- private Vector velocity;
+ private TypeList velocity;
private Fitness bestFitness;
@@ -54,7 +55,7 @@ public class RNAParticle extends AbstractParticle {
public RNAParticle() {
position = new RNAConformation();
bestPosition = new RNAConformation();
- velocity = new Vector();
+ velocity = new TypeList();
}
/**
@@ -169,7 +170,7 @@ public class RNAParticle extends AbstractParticle {
position = (RNAConformation) i.getInitialPosition(problem);
bestPosition.clear();
bestPosition.addAll(position);
- velocity = (Vector) i.getInitialVelocity(problem);
+ velocity = (TypeList) i.getInitialVelocity(problem);
bestFitness = InferiorFitness.instance();
neighbourhoodBest = this;
//fitnessCalc = new SimpleRNAFitness();
diff --git a/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAVelocityUpdate.java b/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAVelocityUpdate.java
index da72fa7..230b39c 100644
--- a/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAVelocityUpdate.java
+++ b/src/main/java/net/sourceforge/cilib/bioinf/rnaprediction/RNAVelocityUpdate.java
@@ -27,6 +27,7 @@ import java.util.Random;
import net.sourceforge.cilib.entity.Particle;
import net.sourceforge.cilib.math.random.generator.KnuthSubtractive;
import net.sourceforge.cilib.pso.velocityupdatestrategies.VelocityUpdateStrategy;
+import net.sourceforge.cilib.type.types.container.TypeList;
import net.sourceforge.cilib.type.types.container.Vector;
/**
@@ -83,7 +84,7 @@ public class RNAVelocityUpdate implements VelocityUpdateStrategy {
*/
public void updateVelocity(Particle particle) {
- Vector velocity = (Vector) particle.getVelocity();
+ TypeList velocity = (TypeList) particle.getVelocity();
RNAConformation openStems = (RNAConformation) velocity.get(0);
RNAConformation closeStems = (RNAConformation) velocity.get(1);
diff --git a/src/main/java/net/sourceforge/cilib/clustering/kmeans/KMeans.java b/src/main/java/net/sourceforge/cilib/clustering/kmeans/KMeans.java
index 178ff47..beba140 100644
--- a/src/main/java/net/sourceforge/cilib/clustering/kmeans/KMeans.java
+++ b/src/main/java/net/sourceforge/cilib/clustering/kmeans/KMeans.java
@@ -155,7 +155,7 @@ public class KMeans extends SingularAlgorithm {
Vector tmp = centroidsInitialisationStrategy.initialise(helper.getClusteringProblem(), helper.getClusterableDataSet());
// this first centroid will do
- return tmp.subVector(0, centroid.getDimension() - 1);
+ return tmp.subList(0, centroid.getDimension() - 1);
}
/**
diff --git a/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda1/FDA1_f2.java b/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda1/FDA1_f2.java
index 2ef5e2c..afbfa72 100755
--- a/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda1/FDA1_f2.java
+++ b/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda1/FDA1_f2.java
@@ -146,7 +146,7 @@ public class FDA1_f2 extends ContinuousFunction {
*/
public double evaluate(Vector x) {
- Vector y = x.subVector(1, x.getDimension()-1);
+ Vector y = x.subList(1, x.getDimension()-1);
double g = this.fda1_g.evaluate(y);
double h = this.fda1_h.evaluate(x);
diff --git a/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda1/FDA1_h.java b/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda1/FDA1_h.java
index fb31ae1..b63b0c2 100755
--- a/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda1/FDA1_h.java
+++ b/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda1/FDA1_h.java
@@ -147,9 +147,9 @@ public class FDA1_h extends ContinuousFunction {
public double evaluate(Vector x) {
//only the first element
- Vector y = x.subVector(0, 0);
+ Vector y = x.subList(0, 0);
//all the elements except the first element
- Vector z = x.subVector(1, x.getDimension()-1);
+ Vector z = x.subList(1, x.getDimension()-1);
//evaluate the fda1_g function
double g = this.fda1_g.evaluate(z);
//evaluate the fda1_f1 function
diff --git a/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda2/FDA2_f2.java b/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda2/FDA2_f2.java
index 50b944a..2e5ecdd 100755
--- a/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda2/FDA2_f2.java
+++ b/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda2/FDA2_f2.java
@@ -147,7 +147,7 @@ public class FDA2_f2 extends ContinuousFunction {
public double evaluate(Vector x) {
Vector y = x;
if (x.getDimension() > 1)
- y = x.subVector(1, fda2_g.getDimension()); //-1
+ y = x.subList(1, fda2_g.getDimension()); //-1
double g = this.fda2_g.evaluate(y);
double h = this.fda2_h.evaluate(x);
diff --git a/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda2/FDA2_h.java b/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda2/FDA2_h.java
index 5f45fa2..7836cba 100755
--- a/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda2/FDA2_h.java
+++ b/src/main/java/net/sourceforge/cilib/functions/continuous/dynamic/moo/fda2/FDA2_h.java
@@ -217,9 +217,9 @@ public class FDA2_h extends ContinuousFunction {
Vector xII = x;
Vector xIII = x;
if (x.getDimension() > 1) {
- xI = x.subVector(0, 0);
- xII = x.subVector(1, 15);
- xIII = x.subVector(16, x.getDimension()-1);
+ xI = x.subList(0, 0);
+ xII = x.subList(1, 15);
+ xIII = x.subList(16, x.getDimension()-1);
}
double f = this.fda2_f.evaluate(xI);
diff --git a/src/main/java/net/sourceforge/cilib/functions/discrete/LongestCommonSubsequence.java b/src/main/java/net/sourceforge/cilib/functions/discrete/LongestCommonSubsequence.java
index 90e132b..34b9494 100644
--- a/src/main/java/net/sourceforge/cilib/functions/discrete/LongestCommonSubsequence.java
+++ b/src/main/java/net/sourceforge/cilib/functions/discrete/LongestCommonSubsequence.java
@@ -99,10 +99,8 @@ public class LongestCommonSubsequence extends DiscreteFunction {
*/
private int length(Vector x) {
int count = 0;
- Iterator<Type> i = x.iterator();
- while (i.hasNext()) {
- Numeric n = (Numeric) i.next();
+ for (Numeric n : x) {
if (n.getBit())
count++;
}
diff --git a/src/main/java/net/sourceforge/cilib/hs/HS.java b/src/main/java/net/sourceforge/cilib/hs/HS.java
index 4d40e29..38bb51d 100644
--- a/src/main/java/net/sourceforge/cilib/hs/HS.java
+++ b/src/main/java/net/sourceforge/cilib/hs/HS.java
@@ -33,7 +33,6 @@ import net.sourceforge.cilib.math.random.RandomNumber;
import net.sourceforge.cilib.problem.OptimisationProblem;
import net.sourceforge.cilib.problem.OptimisationSolution;
import net.sourceforge.cilib.type.types.Real;
-import net.sourceforge.cilib.type.types.container.AbstractList;
import net.sourceforge.cilib.type.types.container.Vector;
/**
@@ -197,7 +196,7 @@ public class HS extends SingularAlgorithm {
// Real newHarmonyValue;
for (int i = 0; i < problem.getDomain().getDimension(); ++i) {
if (random1.getUniform() < harmonyMemoryConsideringRate.getParameter()) {
- Harmony selectedHarmony = (Harmony) this.harmonyMemory.get((int) random2.getUniform(0, harmonyMemory.size()-1));
+ Harmony selectedHarmony = this.harmonyMemory.get((int) random2.getUniform(0, harmonyMemory.size()-1));
Vector selectedHarmonyContents = (Vector) selectedHarmony.getCandidateSolution();
Real newHarmonyValue = (Real) selectedHarmonyContents.get(i).getClone();
if (random1.getUniform() < pitchAdjustingRate.getParameter()) {
@@ -209,8 +208,8 @@ public class HS extends SingularAlgorithm {
newHarmonyVector.set(i, newHarmonyValue);
}
else {
- double upper = ((AbstractList) problem.getDomain().getBuiltRepresenation()).getNumeric(i).getBounds().getUpperBound();
- double lower = ((AbstractList) problem.getDomain().getBuiltRepresenation()).getNumeric(i).getBounds().getLowerBound();
+ double upper = ((Vector) problem.getDomain().getBuiltRepresenation()).get(i).getBounds().getUpperBound();
+ double lower = ((Vector) problem.getDomain().getBuiltRepresenation()).get(i).getBounds().getLowerBound();
newHarmonyVector.set(i, new Real(random3.getUniform(lower, upper)));
}
}
diff --git a/src/main/java/net/sourceforge/cilib/math/StatUtils.java b/src/main/java/net/sourceforge/cilib/math/StatUtils.java
index fa84e36..792211c 100644
--- a/src/main/java/net/sourceforge/cilib/math/StatUtils.java
+++ b/src/main/java/net/sourceforge/cilib/math/StatUtils.java
@@ -24,6 +24,7 @@ package net.sourceforge.cilib.math;
import java.util.Collection;
import net.sourceforge.cilib.problem.dataset.ClusterableDataSet.Pattern;
+import net.sourceforge.cilib.type.types.Numeric;
import net.sourceforge.cilib.type.types.container.Vector;
/**
@@ -86,7 +87,7 @@ public final class StatUtils {
* Calculates the variance of the given set/cluster/collection of @{link Pattern}s.
*
* This is illustrated in Equation 4.c of:<br/>
- * @InProceedings{ 657864, author = "Maria Halkidi and Michalis Vazirgiannis", title =
+ * {@literal @}InProceedings{ 657864, author = "Maria Halkidi and Michalis Vazirgiannis", title =
* "Clustering Validity Assessment: Finding the Optimal Partitioning of a Data
* Set", booktitle = "Proceedings of the IEEE International Conference on Data
* Mining", year = "2001", isbn = "0-7695-1119-8", pages = "187--194", publisher =
@@ -104,7 +105,8 @@ public final class StatUtils {
variance.reset(); // initialize the variance to be all zeroes
for (Pattern pattern : set) {
Vector diffSquare = pattern.data.subtract(center);
- diffSquare = diffSquare.multiply(diffSquare);
+ for (Numeric numeric : diffSquare)
+ numeric.setReal(numeric.getReal()*numeric.getReal());
variance = variance.plus(diffSquare);
}
return variance.norm() / set.size();
diff --git a/src/main/java/net/sourceforge/cilib/math/VectorMath.java b/src/main/java/net/sourceforge/cilib/math/VectorMath.java
index 46efeae..e62bd6d 100644
--- a/src/main/java/net/sourceforge/cilib/math/VectorMath.java
+++ b/src/main/java/net/sourceforge/cilib/math/VectorMath.java
@@ -31,35 +31,34 @@ import net.sourceforge.cilib.type.types.container.Vector;
public interface VectorMath {
/**
- * Adding this {@see net.sourceforge.cilib.type.types.Vector} to another
- * will result in a resultant {@see net.sourceforge.cilib.type.types.Vector}.
+ * Adding this {@code Vector} to another will result in a resultant {@code Vector}.
*
- * @param vector The {@see net.sourceforge.cilib.type.types.Vector} to add to the current one
- * @return The resultant {@see net.sourceforge.cilib.type.types.Vector}
+ * @param vector The {@code Vector} to add to the current one
+ * @return The resultant {@code Vector}
*/
public Vector plus(Vector vector);
/**
- *
- * @param vector
- * @return
+ * Subtract the provided {@code Vector} from the current {@code Vector}.
+ * The result of the subtraction operation is a new {@code Vector} instance,
+ * maintaining the immutability of the operation.
+ * @param vector The {@code Vector} to subtract.
+ * @return A new {@code Vector} instance representing the result of the operation.
*/
public Vector subtract(Vector vector);
/**
- *
- * @param vector
- * @return
+ * Divide the elements of the current {@code Vector} by the provided {@code scalar}.
+ * @param scalar The value to divide all elements within the {@code Vector} by.
+ * @return A new {@code Vector} instance containing the result of the operator.
*/
- public Vector divide(Vector vector);
public Vector divide(double scalar);
/**
- *
- * @param vector
- * @return
+ * Multiply a {@code scalar} with each component in the {@code Vector}.
+ * @param scalar The scalar to multiply in.
+ * @return A new {@code Vector} instance containing the result of the operator.
*/
- public Vector multiply(Vector vector);
public Vector multiply(double scalar);
/**
diff --git a/src/main/java/net/sourceforge/cilib/type/types/container/AbstractList.java b/src/main/java/net/sourceforge/cilib/type/types/container/AbstractList.java
index a53e2b4..50fc7b6 100644
--- a/src/main/java/net/sourceforge/cilib/type/types/container/AbstractList.java
+++ b/src/main/java/net/sourceforge/cilib/type/types/container/AbstractList.java
@@ -21,7 +21,6 @@
*/
package net.sourceforge.cilib.type.types.container;
-import net.sourceforge.cilib.type.types.Numeric;
import net.sourceforge.cilib.type.types.Type;
import net.sourceforge.cilib.type.types.TypeUtil;
@@ -30,13 +29,13 @@ import net.sourceforge.cilib.type.types.TypeUtil;
*
* @author Gary Pampara
*/
-public abstract class AbstractList implements StructuredType<Type> {
+public abstract class AbstractList<E extends Type> implements StructuredType<E> {
private static final long serialVersionUID = -7855489699409219241L;
/**
* {@inheritDoc}
*/
- public abstract AbstractList getClone();
+ public abstract AbstractList<E> getClone();
/**
* {@inheritDoc}
@@ -53,43 +52,43 @@ public abstract class AbstractList implements StructuredType<Type> {
* @param index The index to inspect to return.
* @return The {@linkplain Type} found at <code>index</code>.
*/
- public abstract Type get(int index);
+ public abstract E get(int index);
/**
* Set the {@linkplain Type} at the index <code>index</code>.
* @param index The index to set.
* @param value The value to set.
*/
- public abstract void set(int index, Type value);
+ public abstract void set(int index, E value);
/**
* Insert the provided {@linkplain Type} at the specified {@code index}.
* @param index The index where to insert the {@linkplain Type}.
* @param value The value to set.
*/
- public abstract void insert(int index, Type value);
+ public abstract void insert(int index, E value);
/**
* Add the provided {@linkplain Type} to the end of the current list.
* @param value The {@linkplain Type} to add.
*/
- public void append(Type value) {
+ public void append(E value) {
int position = TypeUtil.getDimension(this);
insert(position, value);
}
/**
* Add the provided {@linkplain AbstractList} to the end of the current list.
- * @param vector The object to add.
+ * @param list The object to add.
* @return <code>true</code> if the operation was successful, <code>false</code> otherwise.
*/
- public abstract boolean append(AbstractList vector);
+ public abstract boolean append(AbstractList<E> list);
/**
* Prepend the provided {@linkplain Type} to the from of this list.
* @param value The {@linkplain Type} to prepend.
*/
- public void prepend(Type value) {
+ public void prepend(E value) {
insert(0, value);
}
@@ -98,56 +97,7 @@ public abstract class AbstractList implements StructuredType<Type> {
* @param vector The object to add.
* @return <code>true</code> if the operation was successful, <code>false</code> otherwise.
*/
- public abstract boolean prepend(AbstractList vector);
-
- /**
- * Get the {@linkplain Numeric} at the given <code>index</code>.
- * @param index The index of the desired {@linkplain Numeric}.
- * @return The {@linkplain Numeric} at position <code>index</code>.
- */
- public abstract Numeric getNumeric(int index);
-
- /**
- * Get the bit-value of the {@linkplain Type} at the given <code>index</code>.
- * @param index The index of the desired {@linkplain Numeric}.
- * @return The bit-value at position <code>index</code>.
- */
- public abstract boolean getBit(int index);
-
- /**
- * Set the value of the {@linkplain net.sourceforge.cilib.type.types.Bit} located at position <code>index</code>.
- * @param index The index of the bit to set the value.
- * @param value The value of the bit to set.
- */
- public abstract void setBit(int index, boolean value);
-
- /**
- * Get the value specified at {@code index} as an {@code int}.
- * @param index The index of the value to get.
- * @return The value at {@code index}.
- */
- public abstract int getInt(int index);
-
- /**
- * Set the value at {@code index} to {@code value}.
- * @param index The index of the value to set.
- * @param value The value to set.s
- */
- public abstract void setInt(int index, int value);
-
- /**
- * Get the value at {@code index} as a {@code double}.
- * @param index The index of the value to get.
- * @return The value as a {@code double}.
- */
- public abstract double getReal(int index);
-
- /**
- * Set the value at {@code index} to {@code value}.
- * @param index The index of the value to set.
- * @param value The value to set.
- */
- public abstract void setReal(int index, double value);
+ public abstract boolean prepend(AbstractList<E> list);
/**
* Create an <code>Object []</code> from this <code>Vector</code>.
@@ -156,13 +106,6 @@ public abstract class AbstractList implements StructuredType<Type> {
public abstract Object[] toArray();
/**
- * Get the {@linkplain Type} instance at the given index.
- * @param index The position of the {@linkplain Type} to return.
- * @return The {@linkplain Type} at index {@literal index}.
- */
- protected abstract Type getType(int index);
-
- /**
* Get the representation of this <tt>Vector</tt> object in the form expressed by the domain notation.
* This method calls the <code>getRepresentation</code> method for each element in the <tt>Vector</tt>.
* This method is also a bit clever in the sense that it will try to detect elements with the same
@@ -211,7 +154,7 @@ public abstract class AbstractList implements StructuredType<Type> {
* @param toIndex The last index to end the sub-vector at.
* @return The created sub-vector instance.
*/
- public abstract AbstractList subVector(int fromIndex, int toIndex);
+ public abstract AbstractList<E> subList(int fromIndex, int toIndex);
/**
* Create a new (cloned) <tt>Vector</tt> consisting of <tt>rhs</tt> that has been appended to
@@ -223,8 +166,8 @@ public abstract class AbstractList implements StructuredType<Type> {
* @return A new <tt>Vector</tt> consisting of the concatenation of <tt>lhs</tt> and
* <tt>rhs</tt>.
*/
- public static AbstractList append(AbstractList lhs, AbstractList rhs) {
- AbstractList cat = lhs.getClone();
+ public static <T extends Type> AbstractList<T> append(AbstractList<T> lhs, AbstractList<T> rhs) {
+ AbstractList<T> cat = lhs.getClone();
cat.append(rhs.getClone());
return cat;
}
@@ -299,11 +242,4 @@ public abstract class AbstractList implements StructuredType<Type> {
return toString('[', ']', delimiter);
}
- /**
- * Initialise the {@linkplain Type} to contain <code>size</code> elements all of the type
- * <code>element</code>.
- * @param size The required size
- * @param element The {@linkplain Type} to use to initialise the {@linkplain AbstractList}.
- */
- public abstract void initialise(int size, Type element);
}
diff --git a/src/main/java/net/sourceforge/cilib/type/types/container/Randomizable.java b/src/main/java/net/sourceforge/cilib/type/types/container/Randomizable.java
new file mode 100644
index 0000000..be15611
--- /dev/null
+++ b/src/main/java/net/sourceforge/cilib/type/types/container/Randomizable.java
@@ -0,0 +1,35 @@
+/**
+ * Copyright (C) 2003 - 2008
+ * Computational Intelligence Research Group (CIRG@UP)
+ * Department of Computer Science
+ * University of Pretoria
+ * South Africa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package net.sourceforge.cilib.type.types.container;
+
+/**
+ *
+ * @author gpampara
+ */
+public interface Randomizable {
+ public boolean randomize();
+}
diff --git a/src/main/java/net/sourceforge/cilib/type/types/container/TypeList.java b/src/main/java/net/sourceforge/cilib/type/types/container/TypeList.java
new file mode 100644
index 0000000..5398e22
--- /dev/null
+++ b/src/main/java/net/sourceforge/cilib/type/types/container/TypeList.java
@@ -0,0 +1,198 @@
+/*
+ * Copyright (C) 2003 - 2008
+ * Computational Intelligence Research Group (CIRG@UP)
+ * Department of Computer Science
+ * University of Pretoria
+ * South Africa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package net.sourceforge.cilib.type.types.container;
+
+import java.util.ArrayList;
+
+import java.util.Iterator;
+import java.util.List;
+import net.sourceforge.cilib.container.visitor.Visitor;
+import net.sourceforge.cilib.type.types.Type;
+
+/**
+ * Concrete implementation of the {@see net.sourceforge.cilib.type.types.Vector}
+ * class. Any {@see net.sourceforge.cilib.type.types.Type} object may be contained
+ * within this object.
+ *
+ * @author Gary Pampara
+ * @author Edwin Peer
+ */
+public class TypeList extends AbstractList<Type> {
+ private static final long serialVersionUID = 136711882764612609L;
+ private List<Type> components;
+
+ public TypeList() {
+ this.components = new ArrayList<Type>();
+ }
+
+ /**
+ *
+ * @param copy
+ */
+ public TypeList(TypeList copy) {
+ this.components = new ArrayList<Type>(copy.components.size());
+
+ for (Type type : copy.components)
+ this.components.add(type.getClone());
+ }
+
+ @Override
+ public TypeList getClone() {
+ return new TypeList(this);
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object other) {
+ if (other == this)
+ return true;
+
+ if ((other == null) || (this.getClass() != other.getClass()))
+ return false;
+
+ TypeList otherList = (TypeList) other;
+ return this.components.equals(otherList.components);
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ int hash = 7;
+ hash = 31 * hash + (this.components == null ? 0 : this.components.hashCode());
+ return hash;
+ }
+
+ @Override
+ public Type get(int index) {
+ return this.components.get(index);
+ }
+
+ @Override
+ public void set(int index, Type value) {
+ this.components.set(index, value);
+ }
+
+ @Override
+ public void insert(int index, Type value) {
+ this.components.add(index, value);
+ }
+
+ @Override
+ public boolean append(AbstractList<Type> list) {
+ for (Type type : list)
+ this.components.add(type);
+
+ return true;
+ }
+
+ @Override
+ public boolean prepend(AbstractList<Type> list) {
+ for (int i = list.size()-1; i >= 0; i--)
+ this.components.add(0, list.get(i));
+
+ return true;
+ }
+
+ @Override
+ public Object[] toArray() {
+ return this.components.toArray();
+ }
+
+ @Override
+ public TypeList subList(int fromIndex, int toIndex) {
+ List<Type> result = this.components.subList(fromIndex, toIndex);
+ TypeList sublist = new TypeList();
+
+ for (Type type : result)
+ sublist.add(type);
+
+ return sublist;
+ }
+
+ @Override
+ public boolean add(Type element) {
+ return this.components.add(element);
+ }
+
+ @Override
+ public boolean addAll(StructuredType<? extends Type> structure) {
+ for (Type type : structure)
+ this.components.add(type);
+
+ return true;
+ }
+
+ @Override
+ public void clear() {
+ this.components.clear();
+ }
+
+ @Override
+ public boolean contains(Type element) {
+ return this.components.contains(element);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return this.components.isEmpty();
+ }
+
+ @Override
+ public Iterator<Type> iterator() {
+ return this.components.iterator();
+ }
+
+ @Override
+ public boolean remove(Type element) {
+ return this.components.remove(element);
+ }
+
+ @Override
+ public Type remove(int index) {
+ return this.components.remove(index);
+ }
+
+ @Override
+ public boolean removeAll(StructuredType<Type> structure) {
+ for (Type type : structure)
+ this.components.remove(type);
+
+ return true;
+ }
+
+ @Override
+ public int size() {
+ return this.components.size();
+ }
+
+ @Override
+ public void accept(Visitor<Type> visitor) {
+ for (Type type : this.components)
+ if (!visitor.isDone())
+ visitor.visit(type);
+ }
+
+}
diff --git a/src/test/java/net/sourceforge/cilib/type/types/container/VectorTest.java b/src/test/java/net/sourceforge/cilib/type/types/container/VectorTest.java
index bb49872..914f37f 100644
--- a/src/test/java/net/sourceforge/cilib/type/types/container/VectorTest.java
+++ b/src/test/java/net/sourceforge/cilib/type/types/container/VectorTest.java
@@ -105,45 +105,19 @@ public class VectorTest {
}
@Test
- public void testNonNumericGet() {
- Vector m = new Vector();
- Set<Real> realSet = new Set<Real>();
-
- m.add(realSet);
-
- assertFalse(m.get(0) instanceof Numeric);
- assertSame(realSet, m.get(0));
-
- assertNotSame(m.get(0), vector.get(0));
- }
-
- @Test
public void testNumericSet() {
assertEquals(1.0, vector.getReal(0), 0.0);
vector.setReal(0, 99.9);
assertEquals(99.9, vector.getReal(0), 0.0);
vector.setInt(0, 2);
- assertEquals((int) 2, vector.getInt(0), 0.0);
+ assertEquals(2, vector.getInt(0), 0.0);
vector.setReal(0, 1.0);
assertEquals(1.0, vector.getReal(0), 0.0);
}
@Test
- public void testNonNumericSet() {
- Vector m = new Vector();
- Set<Object> s = new Set<Object>();
- Vector v = new Vector();
-
- m.add(s);
- m.add(v);
-
- assertSame(s, m.get(0));
- assertSame(v, m.get(1));
- }
-
- @Test
public void testDimension() {
assertFalse(vector.getDimension() == 3);
assertTrue(vector.getDimension() == 4);
@@ -409,9 +383,9 @@ public class VectorTest {
assertNotSame(sum, b);
for(int i = 0; i < 10; i++) {
- assertNotSame(a.getType(i), b.getType(i));
- assertNotSame(sum.getType(i), a.getType(i));
- assertNotSame(sum.getType(i), b.getType(i));
+ assertNotSame(a.get(i), b.get(i));
+ assertNotSame(sum.get(i), a.get(i));
+ assertNotSame(sum.get(i), b.get(i));
assertEquals(a.getReal(i), Integer.valueOf(i).doubleValue(), 0.0);
assertEquals(b.getReal(i), Double.valueOf(9.0 - i), 0.0);
@@ -453,16 +427,16 @@ public class VectorTest {
assertNotSame(difference, b);
for(int i = 0; i < 10; i++) {
- assertNotNull(a.getType(i));
- assertNotNull(b.getType(i));
- assertNotNull(difference.getType(i));
- assertNotSame(a.getType(i), b.getType(i));
- assertNotSame(difference.getType(i), a.getType(i));
- assertNotSame(difference.getType(i), b.getType(i));
+ assertNotNull(a.get(i));
+ assertNotNull(b.get(i));
+ assertNotNull(difference.get(i));
+ assertNotSame(a.get(i), b.get(i));
+ assertNotSame(difference.get(i), a.get(i));
+ assertNotSame(difference.get(i), b.get(i));
assertEquals(a.getReal(i), (double)i, 0.0);
- assertEquals(b.getReal(i), (double)(9.0 - i), 0.0);
- assertEquals(difference.getReal(i), (double)(i - (9.0 - i)), 0.0);
+ assertEquals(b.getReal(i), (9.0 - i), 0.0);
+ assertEquals(difference.getReal(i), (i - (9.0 - i)), 0.0);
}
}
@@ -505,7 +479,7 @@ public class VectorTest {
b.prepend(new Real(i));
b.prepend(new Real(0));
- ((Numeric)b.getType(0)).setReal(10);
+ b.get(0).setReal(10);
Vector divided = a.divide(b);
assertNotNull(a);
@@ -516,16 +490,16 @@ public class VectorTest {
assertNotSame(divided, b);
for(int i = 0; i < 10; i++) {
- assertNotNull(a.getType(i));
- assertNotNull(b.getType(i));
- assertNotNull(divided.getType(i));
- assertNotSame(a.getType(i), b.getType(i));
- assertNotSame(divided.getType(i), a.getType(i));
- assertNotSame(divided.getType(i), b.getType(i));
-
- assertEquals(a.getReal(i), (double)(i + 1), 0.0);
- assertEquals(b.getReal(i), (double)(10.0 - i), 0.0);
- assertEquals(divided.getReal(i), (double)((i + 1) / (10.0 - i)), 0.0);
+ assertNotNull(a.get(i));
+ assertNotNull(b.get(i));
+ assertNotNull(divided.get(i));
+ assertNotSame(a.get(i), b.get(i));
+ assertNotSame(divided.get(i), a.get(i));
+ assertNotSame(divided.get(i), b.get(i));
+
+ assertEquals(a.getReal(i), (i + 1.0), 0.0);
+ assertEquals(b.getReal(i), (10.0 - i), 0.0);
+ assertEquals(divided.getReal(i), ((i + 1) / (10.0 - i)), 0.0);
}
}
@@ -553,12 +527,12 @@ public class VectorTest {
assertNotSame(divided, a);
for(int i = 0; i < 10; i++) {
- assertNotNull(a.getType(i));
- assertNotNull(divided.getType(i));
- assertNotSame(divided.getType(i), a.getType(i));
+ assertNotNull(a.get(i));
+ assertNotNull(divided.get(i));
+ assertNotSame(divided.get(i), a.get(i));
- assertEquals(a.getReal(i), (double)i, 0.0);
- assertEquals(divided.getReal(i), (double)(i / 3.0), 0.000000001);
+ assertEquals(a.getReal(i), (double) i, 0.0);
+ assertEquals(divided.getReal(i), (i / 3.0), 0.000000001);
}
}
@@ -596,16 +570,16 @@ public class VectorTest {
assertNotSame(product, b);
for(int i = 0; i < 10; i++) {
- assertNotNull(a.getType(i));
- assertNotNull(b.getType(i));
- assertNotNull(product.getType(i));
- assertNotSame(a.getType(i), b.getType(i));
- assertNotSame(product.getType(i), a.getType(i));
- assertNotSame(product.getType(i), b.getType(i));
+ assertNotNull(a.get(i));
+ assertNotNull(b.get(i));
+ assertNotNull(product.get(i));
+ assertNotSame(a.get(i), b.get(i));
+ assertNotSame(product.get(i), a.get(i));
+ assertNotSame(product.get(i), b.get(i));
assertEquals(a.getReal(i), (double)i, 0.0);
- assertEquals(b.getReal(i), (double)(9.0 - i), 0.0);
- assertEquals(product.getReal(i), (double)(i * (9.0 - i)), 0.0);
+ assertEquals(b.getReal(i), (9.0 - i), 0.0);
+ assertEquals(product.getReal(i), (i * (9.0 - i)), 0.0);
}
}
@@ -623,12 +597,12 @@ public class VectorTest {
assertNotSame(product, a);
for(int i = 0; i < 10; i++) {
- assertNotNull(a.getType(i));
- assertNotNull(product.getType(i));
- assertNotSame(product.getType(i), a.getType(i));
+ assertNotNull(a.get(i));
+ assertNotNull(product.get(i));
+ assertNotSame(product.get(i), a.get(i));
assertEquals(a.getReal(i), (double)i, 0.0);
- assertEquals(product.getReal(i), (double)(i * 3.0), 0.0);
+ assertEquals(product.getReal(i), (i * 3.0), 0.0);
}
}
--
1.6.2.3
|