Commit f734ef7e authored by Jovan Tanevski's avatar Jovan Tanevski Committed by Jovan Tanevski

Real experiments for journal paper

parent 1293aee0
No preview for this file type
No preview for this file type
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math3.ode.FirstOrderIntegrator;
import org.apache.commons.math3.ode.nonstiff.ClassicalRungeKuttaIntegrator;
import org.uma.jmetal.algorithm.singleobjective.differentialevolution.DifferentialEvolution;
import org.uma.jmetal.algorithm.singleobjective.differentialevolution.DifferentialEvolutionBuilder;
import org.uma.jmetal.operator.impl.crossover.DifferentialEvolutionCrossover;
import org.uma.jmetal.operator.impl.selection.DifferentialEvolutionSelection;
import org.uma.jmetal.solution.DoubleSolution;
import org.uma.jmetal.util.evaluator.impl.SequentialSolutionListEvaluator;
import org.uma.jmetal.util.pseudorandom.JMetalRandom;
import metaModels.MetaModel;
import metaModels.RelevanceMetaModel;
import models.Brusselator;
import models.DynamicalModel;
import models.Metabolic;
import models.Repressilator;
import models.SSGenes;
import problems.MetaModelOptimization;
import weka.classifiers.Classifier;
import weka.classifiers.functions.GaussianProcesses;
import weka.classifiers.functions.LibSVM;
import weka.classifiers.functions.LinearRegression;
import weka.classifiers.functions.supportVector.RBFKernel;
import weka.classifiers.lazy.IBk;
import weka.classifiers.trees.REPTree;
import weka.classifiers.trees.RandomForest;
import weka.core.SelectedTag;
import weka.core.converters.ArffSaver;
public class RealExperiments {
public static int RANDOM_SEED;
public static enum Alg { LINEAR, TREE, KNN, SVM, GP, RF }
public static enum Opt {DE, MM}
static Classifier surrogate, relevator;
static int trainSlope, trainConst, rebSlope, rebConst;
static double dtMin;
static Opt otype;
static double[][] data;
static FirstOrderIntegrator simulator = new ClassicalRungeKuttaIntegrator(1e-2);
public static void main(String[] args) throws IOException, Exception {
surrogate = getClassifier(Alg.valueOf(args[0]));
relevator = getClassifier(Alg.valueOf(args[1]));
trainSlope = Integer.parseInt(args[2]);
trainConst = Integer.parseInt(args[3]);
rebSlope = Integer.parseInt(args[4]);
rebConst = Integer.parseInt(args[5]);
dtMin = Double.parseDouble(args[6]);
otype = Opt.valueOf(args[7]);
String modelName = args[8];
RANDOM_SEED = Integer.parseInt(args[9]);
switch(otype) {
case DE:
DEOptimize(getModel(modelName));
break;
case MM:
MMOptimize(getModel(modelName));
}
}
public static void DEOptimize(DynamicalModel model) throws Exception {
RandomForest surrM = new RandomForest();
MetaModel realModel = new MetaModel(surrM);
realModel.setUseSurrogateFunction((MetaModel metaM) -> (false)); //Never use the surrogate, this is a metaModel of the actual Model
realModel.setRebuildSurrogateFunction((MetaModel metaM) -> (false));
JMetalRandom.getInstance().setSeed(RANDOM_SEED);
int popSize = 10 * model.getNumParameters();
int maxSteps = 10000*model.getNumParameters();
MetaModelOptimization problem = new MetaModelOptimization(realModel, model, data, maxSteps, true);
DifferentialEvolutionBuilder deBuilder = new DifferentialEvolutionBuilder(problem);
//Parameters
deBuilder.setCrossover(new DifferentialEvolutionCrossover(0.8, 0.9, "rand/1/bin"));
deBuilder.setPopulationSize(popSize);
deBuilder.setMaxEvaluations(maxSteps);
//Selection and solution
deBuilder.setSelection(new DifferentialEvolutionSelection());
deBuilder.setSolutionListEvaluator(new SequentialSolutionListEvaluator<DoubleSolution>());
//Start
DifferentialEvolution de = deBuilder.build();
de.run();
realModel.saveBehaviourInfo(model.getName() + "_DE_" + RANDOM_SEED + ".csv");
}
public static void MMOptimize(DynamicalModel model) throws Exception {
int trainSize = trainConst + model.getNumParameters()*trainSlope;
int startSurr = trainSize;
int rebuildSurr = rebConst + model.getNumParameters()*rebSlope;
JMetalRandom.getInstance().setSeed(RANDOM_SEED);
RelevanceMetaModel RmetaModel = new RelevanceMetaModel(surrogate, relevator);
RmetaModel.setUseSurrogateFunction((MetaModel metaM) -> (metaM.evaluationData.numInstances() > startSurr && metaM.currentEval % 10 != 0));
RmetaModel.setRebuildSurrogateFunctionSimple(rebuildSurr,startSurr);
RmetaModel.setWeightInstances(false);
RmetaModel.setFilterSettings(startSurr, -1, 1, true);
RmetaModel.setSurrogateTrainDataSettings(trainSize, true);
RmetaModel.setDynamicThresholdSettings(dtMin, dtMin+0.1, 0.99999, 0, 0.0001, 200, true);
int popSize = 10 * model.getNumParameters();
//double mod = (dtMin==0.9)? 1 : 1 + (dtMin+0.1)/(0.9-dtMin);
double mod = 5;
int maxSteps = (int) Math.ceil(mod*10000*model.getNumParameters());
maxSteps += (popSize - maxSteps%popSize);
RmetaModel.setRelevanceFunction((mm, value)-> Math.pow(1.0/(1+(value - mm.surrogateTrainDataMin)/(Math.max(mm.surrogateTrainDataAvg-mm.surrogateTrainDataMin,Double.MIN_VALUE))),1));
MetaModelOptimization mmproblem = new MetaModelOptimization(RmetaModel, model ,data, maxSteps, true);
DifferentialEvolutionBuilder deBuilder = new DifferentialEvolutionBuilder(mmproblem);
//Parameters
deBuilder.setCrossover(new DifferentialEvolutionCrossover(0.8, 0.9, "rand/1/bin"));
deBuilder.setPopulationSize(popSize);
deBuilder.setMaxEvaluations(maxSteps);
//Selection and solution
deBuilder.setSelection(new DifferentialEvolutionSelection());
deBuilder.setSolutionListEvaluator(new SequentialSolutionListEvaluator<DoubleSolution>());
//Start
DifferentialEvolution de = deBuilder.build();
de.run();
//region USE SOLUTION
//Get results.
DoubleSolution s = de.getResult();
String printableResult = "";
double[] par = new double[s.getNumberOfVariables()];
for(int i=0; i<s.getNumberOfVariables(); i++){
par[i] = s.getVariableValue(i);
printableResult = printableResult + ", " + par[i];
}
//Set model parameters and create simulation of results.
RmetaModel.displayBehaviourInfo();
RmetaModel.saveBehaviourInfo(model.getName() + "_MM_" + decodeName(surrogate.toString()) + "_" + decodeName(relevator.toString()) + "_" + RANDOM_SEED + ".csv");
}
public static String decodeName(String sName) {
if(sName.startsWith("?RE")) return "TREE";
if(sName.startsWith("IB1")) return "KNN";
if(sName.startsWith("Lib")) return "SVM";
if(sName.startsWith("Ran")) return "RF";
return "NA";
}
public static Classifier getClassifier(Alg alg) {
Classifier classifier;
switch (alg) {
case LINEAR:
LinearRegression lr = new LinearRegression();
lr.setAttributeSelectionMethod(new SelectedTag(LinearRegression.SELECTION_NONE, LinearRegression.TAGS_SELECTION));
classifier = lr;
break;
case TREE:
REPTree tree = new REPTree();
tree.setSeed(RANDOM_SEED);
classifier = tree;
break;
case KNN:
IBk knn = new IBk();
knn.setKNN(5);
//knn.setCrossValidate(true);
classifier = knn;
break;
case SVM:
LibSVM svm = new LibSVM();
svm.setSVMType(new SelectedTag(LibSVM.SVMTYPE_EPSILON_SVR, LibSVM.TAGS_SVMTYPE));
svm.setKernelType(new SelectedTag(LibSVM.KERNELTYPE_RBF, LibSVM.TAGS_KERNELTYPE));
svm.setSeed(RANDOM_SEED);
classifier = svm;
break;
case GP:
GaussianProcesses gp = new GaussianProcesses();
gp.setKernel(new RBFKernel());
gp.setFilterType(new SelectedTag(GaussianProcesses.FILTER_NORMALIZE,GaussianProcesses.TAGS_FILTER));
gp.setSeed(RANDOM_SEED);
classifier = gp;
break;
default:
RandomForest rf = new RandomForest();
rf.setNumExecutionSlots(4);
rf.setSeed(RANDOM_SEED);
classifier = rf;
break;
}
return classifier;
}
public static DynamicalModel getModel(String modelName) throws IOException {
DynamicalModel model = null;
switch (modelName) {
case "Brusselator":
data = readData("DATA/brus_0.5_3.csv", 100, 2);
model = new Brusselator(simulator, data);
break;
case "Repressilator":
data = readData("DATA/repressNew.csv", 30, 6);
model = new Repressilator(simulator, data);
break;
case "Metabolic":
model = new Metabolic(simulator);
break;
case "SSGenes":
model = new SSGenes(simulator);
break;
}
return model;
}
//Imports observation data in a readable representation.
public static double[][] readData(String pathToObservations, int observationPoints, int numVar) throws IOException{
//variable declarations
BufferedReader reader = new BufferedReader(new FileReader(pathToObservations));
String line;
int point = 0;
double[][] data = new double[observationPoints][numVar+1];
//skip header
line = reader.readLine();
while(point < observationPoints){
line = reader.readLine();
String[] spl = line.split(",");
for(int i=0; i<spl.length; i++){
data[point][i] = Double.parseDouble(spl[i]);
}
point++;
}
reader.close();
return data;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment