Download Object-Oriented Programming 95-712

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Symbolic Regression 3
The format of the input file
 SimpleInput Class Provided
 DataRow Class Provided
 DataSet Class Provided
 Performing the crossover operation

Input File Format

The format of the input file:
Number of independent variables integer Number of data rows integer
Y-Value double List of X-Values double
Y-Value double List of X-Values double
:
:
Example Input

Example input file with one independent
variable:
1
3
3.19
3.36
3.51
31
0
0.1
0.2
0.3
31 of these (y,x) pairs
DataRow Class

public class DataRow {
private double yVal;
private ArrayList xVals;
DataRow() {
xVals = new ArrayList();
}
// getter and setters provided
DataSet Class Provided
public DataSet(String fileName)
// Reads from the file and
// constructs an ArrayList
// of DataRows
public DataRow get(int i)
public String toString()
public int getNumRows()
public int numIndVars()

Getting the file:
Either

DataSet ds = new
DataSet("D:/www/Java/Week10/CrossoverCode/Cros
soverCode/Data2.dat");

Or
DataSet ds = new DataSet("Data2a.dat");
with Data2a.dat added to the
Eclipse project.
TestGPTree main() Page 1
DataSet ds = new DataSet("Data2a.dat");
Node[] ops = {new Plus(), new Minus(), new Mult(),
new Divide()};
OperatorFactory o = new OperatorFactory(ops);
TerminalFactory t = new
TerminalFactory(ds.numIndepVars());
OperatorFactory
Node[] ops = {new Plus(), new Minus(), new Mult(),
new Divide()};
OperatorFactory o = new OperatorFactory(ops);
Returns a clone of a randomly selected
operator.
TerminalFactory
TerminalFactory t = new
TerminalFactory(ds.numIndepVars());
Returns a randomly selected variable or
constant
TestGPTree main() Page 2
// Build two trees to play with
GPTree tree1 = new GPTree(o, t, 2, rand);
GPTree tree2 = new GPTree(o, t, 2, rand);
TestGPTree main() Page 3
// Display them both.
System.out.println("Tree1 " + tree1);
System.out.println("Tree2 " + tree2);
Tree1 (0.499 / ((0.992 / 0.952) * (0.417 - X0)))
Tree2 ((0.689 + (0.98 - 0.114)) / X0)
Tree1
/
*
.499
-
/
.992
.952
.417
X0
Tree2
/
X0
+
-
.689
.98
.114
TestGPTree main() Page 4

Perform “crossover” and display them again:
crossover(tree1, tree2, rand);
System.out.println("New tree1: " + tree1);
System.out.println("New tree2: " + tree2);
New tree1: ((0.689 + (0.98 - 0.114)) / ((0.992 / 0.952) * (0.417 X0)))
New tree2: (0.499 / X0)
Select an Edge at Random from
Tree1
/
*
.499
-
/
.992
.952
.417
X0
Select an Edge at Random from
Tree2
/
X0
+
-
.689
.98
.114
In the code
provided the
“clipNumber”
is always 2
because size()
method always
returns 0.
The New Tree1
/
*
+
-
.689
.98
.114
-
/
.992
.952
.417
X0
The New Tree2
/
.499
X0
The crossover
public static void crossover(GPTree t1, GPTree t2, Random rand)
{
NodePairPlus pair1 =
t1.randomParentAndChild(rand);
NodePairPlus pair2 =
t2.randomParentAndChild(rand);
pair1.parent.changeChild(pair1.child,
pair2.child);
pair2.parent.changeChild(pair2.child,
pair1.child);
}
GPTree Evaluation (1)
 Three overloaded methods:
/** Evaluate this GPTree for all the rows in
DataSet, and set its
fitness value to the result of the
evaluation.
*/
public double eval(DataSet ds) {
fitness = 0;
int numRows = ds.getNumRows();
for (int i = 0; i < numRows; i++) {
fitness += Math.pow(eval(ds.get(i)) ds.get(i).getY(), 2);
}
return fitness;
}
GPTree Evaluation (2)
/** Evaluate this GPTree for a single row of
data.
*/
public double eval(DataRow dr) {
ArrayList a = dr.getXVals();
double[] data = new double[a.size()];
for (int i = 0; i < a.size(); i++)
data[i] =
((Double)a.get(i)).doubleValue();
return root.eval(data);
}
Generation Class
/** A container of GPTrees. */
public class Generation {
private int numTrees;
private GPTree[] population;
private double[] fitness;
Generation Class (2)
/** Specifies the number of GPTrees in this
generation, the factories used to generate
the individual trees, the maximum tree depth,
and the Random object used. */
Generation(int numTrees, OperatorFactory o,
TerminalFactory t, int m, Random r)
{
this.numTrees = numTrees;
population = new GPTree[numTrees];
for (int i = 0; i < numTrees; i++)
population[i] = new GPTree(o, t, m, r);
}
Generation Class (3)
/** Evaluate each tree in this generation, and set each
tree's fitness value.
*/
public void evalAll(DataSet d) {
for (int i = 0; i < numTrees; i++)
population[i].eval(d);
}
Last Homework
Start with CrossoverForStudents.zip under
Course Documents on Blackboard.
 Write ChooseTreesProportionalToFitness
 Write an Evolver class
 Solve a regression
