package gannuOP.testing;
import gannuOP.algorithms.Optimizer;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Class used to run experiments specified in an XML file.
* @author Francisco Viveros-Jiménez
*
*/
public class TestSet {
/**
* Loads a configuration file and prepares everything for just running the tests.
* @param XMLfile Configuration file.
* @throws Exception
*/
public static void runTests(String XMLfile)throws Exception
{
//Load the XML
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fact.newDocumentBuilder();
Document testset=builder.parse(XMLfile);
Element root=testset.getDocumentElement();
Element data=(Element)root.getElementsByTagName("testset").item(0);
double maxfes,reportfes,precision;
//Optimizer.setSeed(1);
maxfes=Double.parseDouble(data.getAttribute("maxFes"));
precision=Double.parseDouble(data.getAttribute("precision"));
reportfes=Double.parseDouble(data.getAttribute("reportFes"));
NodeList functions=root.getElementsByTagName("function");
ArrayList<Function> testbed=new ArrayList<Function>(functions.getLength());
for(int i=0;i<functions.getLength();i++)
{
Element function=(Element)functions.item(i);
if(!function.getAttribute("d").equals(""))//Unconstrained function
{
int d=Integer.parseInt(function.getAttribute("d"));
@SuppressWarnings("unchecked")
Constructor<Function> con=(Constructor<Function>)Class.forName("testing."+function.getAttribute("class")).getConstructor(int.class);
testbed.add((Function)con.newInstance(d));
}
}
NodeList xls=root.getElementsByTagName("xls");
for(int i=0;i<xls.getLength();i++)
{
NodeList algorithms=((Element)xls.item(i)).getElementsByTagName("algorithm");
ArrayList<ArrayList<ArrayList<Report>>> reports=new ArrayList<ArrayList<ArrayList<Report>>>(algorithms.getLength());
for(int j=0;j<algorithms.getLength();j++)
{
Element algorithm=(Element)algorithms.item(j);
Optimizer optimizer=(Optimizer)Class.forName("algorithms."+algorithm.getAttribute("class")).newInstance();
String parameters=algorithm.getAttribute("parameter");
ArrayList<ArrayList<Report>> r=new ArrayList<ArrayList<Report>>(testbed.size());
System.out.println("Algorithm : " +optimizer.toString());
for(Function function:testbed)
{
r.add(optimizer.solve(parameters, function, maxfes, reportfes, precision));
ArrayList<Report> x=r.get(r.size()-1);
Report d=x.get(x.size()-1);
System.out.println("Error: " +d.getBestIndividual()[function.getD()]);
System.out.println("Evaluations: " +d.getFes());
}
reports.add(r);
}
//Writing xls file
}
}
public static void main(String args[]) throws Exception
{
/*Thing to do
* 1) Fix TestSet.
* 2) Fix test.
*/
String test;
if(args.length>0)
{
test=args[0];
}
else
{
test="config.xml";
}
TestSet.runTests(test);
System.out.println("Finished...");
}
}