Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 132 additions & 1 deletion src/gaknn/core/Instance.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

package gaknn.core;





/**
* Reprsents an instance of the data set
*
Expand Down Expand Up @@ -69,11 +73,138 @@ public Instances dataset() {
public void AddElement(double value, int index){
m_AttValues[index] = value;
}

/** Return number of values in instance.
*
*
*/
//@author thimal
public int numValues(){
return m_AttValues.length;
}
/**
* Returns the index of the attribute stored at the given position.
* Just returns the given value.
*
* @param position the position
* @return the index of the attribute stored at the given position
*/
//@author thimal
public /*@pure@*/ int index(int position) {

return position;
}
/**
* Returns an instance's attribute value in internal format.
* Does exactly the same thing as value() if applied to an Instance.
*
* @param indexOfIndex the index of the attribute's index
* @return the specified value as a double (If the corresponding
* attribute is nominal (or a string) then it returns the value's index as a
* double).
*/
//@author thimal
public /*@pure@*/ double valueSparse(int indexOfIndex) {

return m_AttValues[indexOfIndex];
}
/**
* Tests if a specific value is "missing".
*
* @param attIndex the attribute's index
* @return true if the value is "missing"
*/
//@author thimal
public /*@pure@*/ boolean isMissing(int attIndex) {

if (Double.isNaN(m_AttValues[attIndex])) {
return true;
}
return false;
}
/**
* Tests if the given value codes "missing".
*
* @param val the value to be tested
* @return true if val codes "missing"
*/
//@author thimal
public static /*@pure@*/ boolean isMissingValue(double val) {

return Double.isNaN(val);
}
/** Returns the element at the given position
*
* @param index of the elrment.
*/
/**
* Returns the values of each attribute as an array of doubles.
*
* @return an array containing all the instance attribute values
*/
//@author thimal
public double[] toDoubleArray() {

double[] newValues = new double[m_AttValues.length];
System.arraycopy(m_AttValues, 0, newValues, 0,
m_AttValues.length);
return newValues;
}
/**
* Clones the attribute vector of the instance and
* overwrites it with the clone.
*/
//@author thimal
private void freshAttributeVector() {

m_AttValues = toDoubleArray();
}
/**
* Sets a specific value in the instance to the given value
* (internal floating-point format). Performs a deep copy
* of the vector of attribute values before the value is set.
*
* @param attIndex the attribute's index
* @param value the new attribute value (If the corresponding
* attribute is nominal (or a string) then this is the new value's
* index as a double).
*/
//@author thimal
public void setValue(int attIndex, double value) {

freshAttributeVector();
m_AttValues[attIndex] = value;
}
/**
* Sets the reference to the dataset. Does not check if the instance
* is compatible with the dataset. Note: the dataset does not know
* about this instance. If the structure of the dataset's header
* gets changed, this instance will not be adjusted automatically.
*
* @param instances the reference to the dataset
*/
//@author thimal
public final void setDataset(Instances instances) {

m_Dataset = instances;
}
/**
* Produces a shallow copy of this instance. The copy has
* access to the same dataset. (if you want to make a copy
* that doesn't have access to the dataset, use
* <code>new Instance(instance)</code>
*
* @return the shallow copy
*/
//@ also ensures \result != null;
//@ also ensures \result instanceof Instance;
//@ also ensures ((Instance)\result).m_Dataset == m_Dataset;
public /*@pure@*/ Object copy() {

Instance result = new Instance(this);
result.m_Dataset = m_Dataset;
return result;
}

public double GetElementAt(int index){
return m_AttValues[index];
}
Expand Down
77 changes: 77 additions & 0 deletions src/gaknn/core/Instances.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package gaknn.core;





//import Instance;

/**
* Instances class represents the data.
*
Expand Down Expand Up @@ -39,6 +45,9 @@ public Instances(String name,
m_RelationName = name;
m_ClassIndex = -1;
m_Attributes = attInfo;

//initialize the m_instances
m_Instances=new FastVector();

for (int i=0; i<attInfo.size(); i++ )
{
Expand All @@ -52,6 +61,56 @@ public Instances(String name,
m_Capacity = CAPACITY;
}

/**
* Constructor creating an empty set of instances. Copies references to the
* header information from the given set of instances. Sets the capacity of
* the set of instances to 0 if its negative.
*
* @param dataset the instances from which the header information is to be
* taken
* @param capacity the capacity of the new dataset
*/
//@author thimal
public Instances(/* @non_null@ */Instances dataset, int capacity) {
initialize(dataset, capacity);
}

/**
* initializes with the header information of the given dataset and sets the
* capacity of the set of instances.
*
* @param dataset the dataset to use as template
* @param capacity the number of rows to reserve
*/
//@author thimal
protected void initialize(Instances dataset, int capacity) {
if (capacity < 0)
capacity = 0;

// Strings only have to be "shallow" copied because
// they can't be modified.
m_ClassIndex = dataset.m_ClassIndex;
m_RelationName = dataset.m_RelationName;
m_Attributes = dataset.m_Attributes;
m_Instances = new FastVector(capacity);
}
/**
* Adds one instance to the end of the set. Shallow copies instance before it
* is added. Increases the size of the dataset if it is not large enough. Does
* not check if the instance is compatible with the dataset. Note: String or
* relational values are not transferred.
*
* @param instance the instance to be added
*/
//thimal
public void add(/* @non_null@ */Instance instance) {

Instance newInstance = (Instance) instance.copy();

newInstance.setDataset(this);
m_Instances.addElement(newInstance);
}

/** Sets the weight vector of the attributes
*
* @param weights for the attributes.
Expand Down Expand Up @@ -185,6 +244,10 @@ public void AddElement(int RecNo, double[] values, int classIndex){
if (m_NumAttributes > 0) m_DataSet[RecNo] = values;
m_ClassIdList[RecNo] = classIndex;
m_Lines++;

// set the m_instances
Instance ins=new Instance(values);
add(ins);
}


Expand Down Expand Up @@ -225,6 +288,20 @@ private static Object ResizeArray (Object oldArray, int newSize) {
System.arraycopy (oldArray,0,newArray,0,preserveLength);
return newArray;
}

/**
* Returns the instance at the given position.
*
* @param index the instance's index (index starts with 0)
* @return the instance at the given position
*/
// @ requires 0 <= index;
// @ requires index < numInstances();
// @ author thimal
public/* @non_null pure@ */Instance instance(int index) {

return (Instance) m_Instances.elementAt(index);
}


}
51 changes: 51 additions & 0 deletions src/gaknn/core/kdtree/AdditionalMeasureProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
* AdditionalMeasureProducer.java
* Copyright (C) 2000 University of Waikato, Hamilton, New Zealand
*
*/

package gaknn.core.kdtree;

import java.util.*;

/**
* Interface to something that can produce measures other than those
* calculated by evaluation modules.
*
* @author Mark Hall (mhall@cs.waikato.ac.nz)
* @version $Revision: 1.8 $
*/
public interface AdditionalMeasureProducer {

/**
* Returns an enumeration of the measure names. Additional measures
* must follow the naming convention of starting with "measure", eg.
* double measureBlah()
* @return an enumeration of the measure names
*/
Enumeration enumerateMeasures();

/**
* Returns the value of the named measure
* @param measureName the name of the measure to query for its value
* @return the value of the named measure
* @exception IllegalArgumentException if the named measure is not supported
*/
double getMeasure(String measureName);
}
Loading