/**
* --Copyright notice--
*
* Copyright (c) School of Geography, University of Leeds.
* http://www.geog.leeds.ac.uk/
* This software is licensed under 'The Artistic License' which can be found at
* the Open Source Initiative website at...
* http://www.opensource.org/licenses/artistic-license.php
* Please note that the optional Clause 8 does not apply to this code.
*
* The Standard Version source code, and associated documentation can be found at...
* [online] http://mass.leeds.ac.uk/
*
*
* --End of Copyright notice--
*
*/
import genetic_algorithm.*;
import java.io.*;
/**
* Class to wrap a model in code for GA running.
* @version 1.0
* @author Andy Evans and Nick Malleson
**/
public class GAWrapper extends Chromosome {
/** For I/O. **/
public IO io = new IO();
/** The real data **/
public double[][] expected = io.readData(new File("real.txt"));
/**
* Use two Genes, each of which can vary between 1 and 200 in changes of 1.
* @return genes
*/
@Override
public Gene[] createGenes() {
Gene[] thegenes = new Gene[2];
for (int i = 0; i < 2; i++) {
thegenes[i] = new Gene("Gene" + i, 1.0, 200.0, 1.0);
}
return thegenes;
}
/**
* Fitness calculated by running the model and comparing with real data.
* @throws GAException
*/
@Override
public void calcFitness() throws GAException {
// Run the model.
// To do this we build what would be the command line args, using the current genes.
String[] args = {Integer.toString(10), Integer.toString(3360), Double.toString(genes[0].getValue()), Double.toString(genes[1].getValue()), "room.txt", "out.txt"};
// We then pass this straight to the model constructor, bypassing main.
new Model(args);
// Read the output created by the model.
double[][] results = io.readData(new File("out.txt"));
// Calculate Total Absolute Error between model output and real data (read in instance variable section).
double totalAbsoluteError = 0;
for (int i = 0; i < results.length; i++) {
for (int j = 0; j < results[i].length; j++) {
totalAbsoluteError = totalAbsoluteError + Math.abs(results[i][j] - expected[i][j]);
}
}
System.out.println(totalAbsoluteError);
this.fitness = totalAbsoluteError;
}
/**
* Runs the GA, using this class as the Chromosome.
* @param args here, the filename for GA output -- all Model args created within this program
*/
public static void main(String[] args) {
String outFileName = args[0];
File f = new File(outFileName);
System.out.println("Will write output to file: " + f.getAbsolutePath());
try {
// Run the GA.
Genetic gen = new Genetic(GAWrapper.class, f, 400, 30, 10);
gen.run();
// Get the best Chromosome and use it to run a prediction.
Chromosome[] chromes = gen.getChromes();
String[] newArgs = {Integer.toString(10), Integer.toString(3360), Double.toString(chromes[0].getGenes()[0].getValue()), Double.toString(chromes[0].getGenes()[1].getValue()), "newroom.txt", "prediction.txt"};
new Model(newArgs);
} catch (GAException ex) {
System.err.println("Error running GA: " + ex.getMessage() + ". Cause is: "
+ ex.getCause() == null ? "null" : ex.getCause().getClass() + ", "
+ ex.getCause().getMessage() + ".");
}
System.exit(0);
}
}