/**
* --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 genetic_algorithm.grid.*;
import java.io.*;
import mpi.*;
/**
* 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. **/
IO io = new IO();
/** The real data **/
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 in parallel, 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) {
// Run parallel GA.
GeneticGrid gen = new GeneticGrid(args, GAWrapper.class, 800);
// Once finished, if on node zero, run the predictive model.
mpi.MPI.Init(args);
if (mpi.MPI.COMM_WORLD.Rank() == 0) {
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"};
// Running by calling the constructor seems to be blocked by MPJ, so
// run as external application.
try{
Process p = Runtime.getRuntime().exec("java Model 10 3360 " + Double.toString(chromes[0].getGenes()[0].getValue()) + " " + Double.toString(chromes[0].getGenes()[1].getValue()) + " " + "room.txt" + " " + "prediction.txt");
} catch (IOException io) {
io.printStackTrace();
}
}
System.exit(0);
}
}