Skip to content

Commit

Permalink
Refactored code:
Browse files Browse the repository at this point in the history
renamed Instance's getContinuous(int) method to get(int), since all it was doing was getting the ith element (not getting the ith continuous element as claimed)

removed a few extraneous imports
  • Loading branch information
jenleong committed Oct 18, 2014
1 parent 45b6a52 commit 28c9919
Show file tree
Hide file tree
Showing 24 changed files with 72 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/dist/PrecalculatedDistribution.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Instance mode(Instance input) {
* @see hmm.distribution.OutputDistribution#probabilityOfObservation(hmm.observation.Observation)
*/
public double p(Instance inst) {
return inst.getContinuous(i);
return inst.get(i);
}


Expand Down
6 changes: 3 additions & 3 deletions src/func/inst/HyperRectangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ public HyperRectangle splitRight(double value, int dimension) {
public Instance pointNearestTo(Instance target) {
double[] nearest = new double[target.size()];
for (int i = 0; i < nearest.length; i++) {
if (target.getContinuous(i) <= min.get(i)) {
if (target.get(i) <= min.get(i)) {
nearest[i] = min.get(i);
} else if (target.getContinuous(i) >= max.get(i)) {
} else if (target.get(i) >= max.get(i)) {
nearest[i] = max.get(i);
} else {
nearest[i] = target.getContinuous(i);
nearest[i] = target.get(i);
}
}
return new Instance(new DenseVector(nearest));
Expand Down
10 changes: 5 additions & 5 deletions src/func/inst/KDTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ private int chooseSplitterSmart(KDTreeNode[] nodes, int start, int end) {
for (int i = start; i < end; i++) {
Instance key = nodes[i].getInstance();
for (int j = 0; j < dimensions; j++) {
min[j] = Math.min(min[j], key.getContinuous(j));
max[j] = Math.max(max[j], key.getContinuous(j));
min[j] = Math.min(min[j], key.get(j));
max[j] = Math.max(max[j], key.get(j));
}
}
// find the widest dimension
Expand All @@ -176,11 +176,11 @@ private int chooseSplitterSmart(KDTreeNode[] nodes, int start, int end) {
int splitterIndex = -1;
for (int i = start; i < end; i++) {
KDTreeNode node = nodes[i];
if (Math.abs(node.getInstance().getContinuous(widestDimension) - median)
if (Math.abs(node.getInstance().get(widestDimension) - median)
< bestDifference) {
splitterIndex = i;
bestDifference =
Math.abs(node.getInstance().getContinuous(widestDimension) - median);
Math.abs(node.getInstance().get(widestDimension) - median);
}
}
nodes[splitterIndex].setDimension(widestDimension);
Expand Down Expand Up @@ -253,7 +253,7 @@ private void knn(KDTreeNode node, Instance target, HyperRectangle hr,
node.getSplitValue(), node.getDimension());
HyperRectangle nearHR, farHR;
KDTreeNode nearNode, farNode;
if (target.getContinuous(node.getDimension()) < node.getSplitValue()) {
if (target.get(node.getDimension()) < node.getSplitValue()) {
nearHR = leftHR; nearNode = node.getLeft();
farHR = rightHR; farNode = node.getRight();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/func/inst/KDTreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public KDTreeNode(Instance key) {
* @return the value
*/
public double getSplitValue() {
return instance.getContinuous(dimension);
return instance.get(dimension);
}

/**
Expand Down Expand Up @@ -105,7 +105,7 @@ public void setRight(KDTreeNode node) {
*/
public int compareTo(Object o) {
double value = getSplitValue();
double otherValue = ((KDTreeNode) o).getInstance().getContinuous(dimension);
double otherValue = ((KDTreeNode) o).getInstance().get(dimension);
if (value < otherValue) {
return -1;
} else if (value > otherValue) {
Expand Down
2 changes: 1 addition & 1 deletion src/opt/ContinuousAddOneNeighbor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ContinuousAddOneNeighbor() {
public Instance neighbor(Instance d) {
int i = Distribution.random.nextInt(d.size());
Instance cod = (Instance) d.copy();
cod.getData().set(i, cod.getContinuous(i)+ Distribution.random.nextDouble() * amount - amount / 2);
cod.getData().set(i, cod.get(i)+ Distribution.random.nextDouble() * amount - amount / 2);
return cod;
}
}
4 changes: 2 additions & 2 deletions src/opt/SwapNeighbor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public Instance neighbor(Instance d) {
Instance cod = (Instance) d.copy();
int i = Distribution.random.nextInt(cod.getData().size());
int j = Distribution.random.nextInt(cod.getData().size());
double temp = cod.getContinuous(i);
cod.getData().set(i, cod.getContinuous(j));
double temp = cod.get(i);
cod.getData().set(i, cod.get(j));
cod.getData().set(j, temp);
return cod;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public TravelingSalesmanSortEvaluationFunction(double[][] points) {
public double value(Instance d) {
double[] ddata = new double[d.size()];
for (int i = 0; i < ddata.length; i++) {
ddata[i] = d.getContinuous(i);
ddata[i] = d.get(i);
}
int[] order = ABAGAILArrays.indices(d.size());
ABAGAILArrays.quicksort(ddata, order);
Expand Down
2 changes: 1 addition & 1 deletion src/opt/ga/ContinuousAddOneMutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public ContinuousAddOneMutation() {
*/
public void mutate(Instance cod) {
int i = Distribution.random.nextInt(cod.size());
cod.getData().set(i, cod.getContinuous(i)+ Distribution.random.nextDouble() * amount - amount / 2);
cod.getData().set(i, cod.get(i)+ Distribution.random.nextDouble() * amount - amount / 2);
}
}
4 changes: 2 additions & 2 deletions src/opt/ga/SingleCrossOver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public Instance mate(Instance a, Instance b) {
int point = Distribution.random.nextInt(newData.length + 1);
for (int i = 0; i < newData.length; i++) {
if (i >= point) {
newData[i] = a.getContinuous(i);
newData[i] = a.get(i);
} else {
newData[i] = b.getContinuous(i);
newData[i] = b.get(i);
}
}
return new Instance(newData);
Expand Down
4 changes: 2 additions & 2 deletions src/opt/ga/SwapMutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class SwapMutation implements MutationFunction {
public void mutate(Instance d) {
int i = Distribution.random.nextInt(d.size());
int j = Distribution.random.nextInt(d.size());
double temp = d.getContinuous(i);
d.getData().set(i, d.getContinuous(j));
double temp = d.get(i);
d.getData().set(i, d.get(j));
d.getData().set(j, temp);
}
}
4 changes: 2 additions & 2 deletions src/opt/ga/UniformCrossOver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public Instance mate(Instance a, Instance b) {
double[] newData = new double[a.size()];
for (int i = 0; i < newData.length; i++) {
if (Distribution.random.nextBoolean()) {
newData[i] = a.getContinuous(i);
newData[i] = a.get(i);
} else {
newData[i] = b.getContinuous(i);
newData[i] = b.get(i);
}
}
return new Instance(newData);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/DataSetDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ public void induceFrom(DataSet data) {
min.minEquals(cur.getData());
for (int j = 0; j < types.length; j++) {
if (types[j] == AttributeType.BINARY
&& cur.getContinuous(j) != 1 && cur.getContinuous(j) != 0) {
&& cur.get(j) != 1 && cur.get(j) != 0) {

types[j] = AttributeType.DISCRETE;
}
if (types[j] == AttributeType.DISCRETE
&& cur.getDiscrete(j) != cur.getContinuous(j)) {
&& cur.getDiscrete(j) != cur.get(j)) {
types[j] = AttributeType.CONTINUOUS;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/DataSetWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void write() throws IOException {
}
} else {
for (int j = 0; j < data.size(); j++) {
pw.print(data.getContinuous(j));
pw.print(data.get(j));
if (j + 1 < data.size() || data.getLabel() != null) {
pw.print(", ");
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/EuclideanDistance.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class EuclideanDistance extends AbstractDistanceMeasure {
public double value(Instance va, Instance vb) {
double sum = 0;
for (int i = 0; i < va.size(); i++) {
sum += (va.getContinuous(i) - vb.getContinuous(i))
* (va.getContinuous(i) - vb.getContinuous(i));
sum += (va.get(i) - vb.get(i))
* (va.get(i) - vb.get(i));
}
return sum;
}
Expand Down
8 changes: 4 additions & 4 deletions src/shared/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ public int size() {
}

/**
* Get the ith continuous value
* @param i the value to get
* Get the ith value
* @param i the index of the value to get
* @return the value
*/
public double getContinuous(int i) {
public double get(int i) {
return data.get(i);
}

Expand All @@ -159,7 +159,7 @@ public int getDiscrete(int i) {
* @return the value
*/
public double getContinuous() {
return getContinuous(0);
return get(0);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/shared/MixedDistanceMeasure.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public double value(Instance va, Instance vb) {
double distance = 0;
for (int i = 0; i < va.size(); i++) {
if (types[i] == AttributeType.CONTINUOUS) {
distance += (va.getContinuous(i) - vb.getContinuous(i))
* (va.getContinuous(i) - vb.getContinuous(i));
distance += (va.get(i) - vb.get(i))
* (va.get(i) - vb.get(i));
} else {
if (va.getDiscrete(i) != vb.getDiscrete(i)) {
distance += 1;
Expand Down
6 changes: 3 additions & 3 deletions src/shared/SumOfSquaresError.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public double value(Instance output, Instance example) {
double sum = 0;
Instance label = example.getLabel();
for (int i = 0; i < output.size(); i++) {
sum += (output.getContinuous(i) - label.getContinuous(i))
* (output.getContinuous(i) - label.getContinuous(i))
sum += (output.get(i) - label.get(i))
* (output.get(i) - label.get(i))
* example.getWeight();
}
return .5 * sum;
Expand All @@ -34,7 +34,7 @@ public double[] gradient(Instance output, Instance example) {
double[] errorArray = new double[output.size()];
Instance label = example.getLabel();
for (int i = 0; i < output.size(); i++) {
errorArray[i] = (output.getContinuous(i) - label.getContinuous(i))
errorArray[i] = (output.get(i) - label.get(i))
* example.getWeight();
}
return errorArray;
Expand Down
10 changes: 5 additions & 5 deletions src/shared/filt/ContinuousToDiscreteFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ public void filter(DataSet dataSet) {
Instance instance = dataSet.get(i);
for (int j = 0; j < oldDescription.getAttributeCount(); j++) {
if (oldDescription.getAttributeTypes()[j] == AttributeType.CONTINUOUS) {
double cv = instance.getContinuous(j);
double cv = instance.get(j);
int dv = (int) ((cv - oldDescription.getMin(j))
* numberOfBins / oldDescription.getRange(j));
instance.getData().set(j, dv);
}
}
}

// the description is no longer valid so generate a new one
dataSet.setDescription(new DataSetDescription(dataSet));
dataSet.getDescription().setLabelDescription(new DataSetDescription(dataSet.getLabelDataSet()));

// the description is no longer valid so generate a new one
dataSet.setDescription(new DataSetDescription(dataSet));
dataSet.getDescription().setLabelDescription(new DataSetDescription(dataSet.getLabelDataSet()));
}
}
2 changes: 1 addition & 1 deletion src/shared/filt/DiscreteToBinaryFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void filter(DataSet dataSet) {
data[k + instance.getDiscrete(j)] = 1;
k += oldDescription.getDiscreteRange(j);
} else {
data[k] = instance.getContinuous(j);
data[k] = instance.get(j);
k++;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/shared/reader/DataSetLabelBinarySeperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import shared.DataSet;
import shared.Instance;
import shared.tester.Comparison;

/**
* Separates Discrete Labels into Binary representation for better use in Neural Networks
Expand Down Expand Up @@ -61,7 +60,7 @@ public static Instance combineLabels(Instance instance) {
int maxInx = -1;
double max = 0;
for (int ii = 0; ii < instance.size(); ii++) {
double inst = instance.getContinuous(ii);
double inst = instance.get(ii);
if (inst > max) {
maxInx = ii;
max = inst;
Expand Down
24 changes: 24 additions & 0 deletions src/shared/runner/Experiment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package shared.runner;

import shared.DataSet;
import shared.reader.DataSetReader;

/**
* Created by zooky on 10/11/14.
*/
public interface Experiment {
/**
* Set type of DataSetReader according to type of datafile
*/
public void setDataSetReader();
/**
*
* @return A DataSet
*/
public DataSet getDataSet();





}
3 changes: 1 addition & 2 deletions src/shared/tester/Comparison.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package shared.tester;

import shared.Instance;
import util.linalg.Vector;

/**
* An generic utility for comparing values in Instance objects
Expand Down Expand Up @@ -55,7 +54,7 @@ public boolean isAllCorrect() {
public boolean isCorrect(int index) {
//compare the continuous values, down to 1e-6. This accounts for any weird floating point issues, and some
// issues when classifying discrete or boolean functions.
return Math.abs(expected.getContinuous(index) - actual.getContinuous(index)) < epsilon;
return Math.abs(expected.get(index) - actual.get(index)) < epsilon;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/shared/tester/ConfusionMatrixTestMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import shared.AttributeType;
import shared.DataSetDescription;
import shared.Instance;
import shared.reader.DataSetLabelBinarySeperator;

/**
* A test metric to generate a confusion matrix. This metric expects the true labels
Expand Down Expand Up @@ -57,8 +56,8 @@ public int hashCode() {
for (int ii = 0; ii < expected.size(); ii++) {
//scale the expected value, to provide separation between corresponding pairs
// (e.g. a, b should be different from b, a)
hashCode += 0x10000 * expected.getContinuous(ii);
hashCode += actual.getContinuous(ii);
hashCode += 0x10000 * expected.get(ii);
hashCode += actual.get(ii);
}
return hashCode;
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/tester/RawOutputTestMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void addResult(Instance expected, Instance actual) {
if (addComma) {
builder.append(",");
}
builder.append(expected.getContinuous(ii));
builder.append(expected.get(ii));
addComma = true;
}
builder.append(", Actual: ");
Expand All @@ -22,7 +22,7 @@ public void addResult(Instance expected, Instance actual) {
if (addComma) {
builder.append(",");
}
builder.append(actual.getContinuous(ii));
builder.append(actual.get(ii));
addComma = true;
}

Expand Down

0 comments on commit 28c9919

Please sign in to comment.