Regards
zia
What yo want to do is perfectly possible and just requires some Java
programming.
However, for making these experiments, we usually take an alternative
route. For each instance, we have a separate input file; and each JGA
run is an independent experiment, which reads the input file and
writes to another file. Because this should be replicated for the 100
instances, we write a MS-DOS batch program that is in charge of
triggering each experiment (multiple experiments). This batch file, in
turn, calls another batch program which is the one that runs a single
JGA experiment. Here is a sample of programs used to run the
experiments for the Capacitated Vehicle Routing Problem shown in the
chapter by Medaglia and Gutierrez (2006). If you want to replicate
this approach, you should tweak them according to your main program
and problem specific settings (for instance, our main program was
CVRPMain). You should run multi-XXX.bat at the root of your program,
that is, at the level where you see the folders bin, src, and lib of
your application. Afterwards, collect all these files and process the
results.
-----------------
cvrp.bat:
Wrapper to a Java program.
Note that each call takes as argument (%1)
the configuration file.
-----------------
---BEGIN FILE----
@echo off
java -cp ./bin;./lib/jga-20040920.jar;./lib/ostermiller.jar CVRPMain
%1
---END FILE----
-----------------
multi-cvrp.bat:
Program that triggers the instances.
Note: it calls cvrp.bat.
-----------------
---BEGIN FILE----
call cvrp JGA-E-n101-k14-01.ini > E-n101-k14-01.out
call cvrp JGA-E-n101-k14-02.ini > E-n101-k14-02.out
call cvrp JGA-E-n101-k14-03.ini > E-n101-k14-03.out
call cvrp JGA-E-n101-k14-04.ini > E-n101-k14-04.out
call cvrp JGA-E-n101-k14-05.ini > E-n101-k14-05.out
call cvrp JGA-E-n101-k14-06.ini > E-n101-k14-06.out
call cvrp JGA-E-n101-k14-07.ini > E-n101-k14-07.out
call cvrp JGA-E-n101-k14-08.ini > E-n101-k14-08.out
call cvrp JGA-E-n101-k14-09.ini > E-n101-k14-09.out
call cvrp JGA-E-n101-k14-10.ini > E-n101-k14-10.out
---END FILE----
Note that by using piping (greater-than sign) what the program usually
shows in the standard output is dumped into a file. I will show you
next a configuration file, responsible to point to a data set.
-----------------
JGA-E-n101-k14-10.ini:
Configuration file.
Note the parameter PROBLEMDATASETTINGS.
-----------------
---BEGIN FILE----
# ---------------------------------
# JGA parameter settings
# Created: March 19, 2003
# Updated: May 18, 2005
# ---------------------------------
POPSIZE = 100
MAXGEN = 1000
MUTRATE = 0.2
CROSSRATE = 0.7
SEED = 10
OBJECTIVES = 1
CRITERIA = MIN
WEIGHTS = 1.0
GENOTYPE = GVRGenotype
PHENOTYPE = edu.uniandes.copa.jga.SingleFitnessPhenotype
FITNESSFCTN = CVRPEvalFunction
MUTATION = InversionMutation
CROSSOVER = GVRCrossover
#SELECTION = edu.uniandes.copa.jga.RouletteWheelSelection
SELECTION = edu.uniandes.copa.jga.BestIndividualSelection
GENETICALGORITHM = edu.uniandes.copa.jga.BasicGeneticAlgorithm
PROBLEMDATASETTINGS = CVRP-E-n101-k14.ini
STATISTICSCOLLECTOR = 1
OUTPUTLEVEL = 1
---END FILE----
The PROBLEMDATASETTINGS parameter points to the file which has the
pointers to the data:
-----------------
CVRP-E-n101-k14.ini:
Problem-specific configuration file.
-----------------
---BEGIN FILE----
# ---------------------------------
# CVRP-E-n101-k14.ini settings
# Created: March 28, 2005
# Updated: May 15, 2005
# ---------------------------------
NODES = 101
NODES_FILE = ./data/Eilon/E-n101-k14-nodes.csv
VEHICLES_FILE = ./data/Eilon/E-n101-k14-vehicles.csv
COST_FILE = ./data/Eilon/E-n101-k14-distance.csv
---END FILE----
Finally, the CVRPSettings.java is the program that knows how to read
those files.
-----------------
CVRPSettings.java:
-----------------
---BEGIN FILE----
//import edu.uniandes.copa.jgalib.*;
import com.Ostermiller.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.HashMap;
/**
* This class holds the global parameters for the symmetric CVRP.
* It implements the singleton design pattern because only
* one instance should be instantiated.
*
* @author A.Medaglia - E.Gutiérrez
* @version %I%, %G%
*/
public final class CVRPSettings
{
/**
* Unique instance (singleton design pattern)
*/
private static CVRPSettings instance=null;
/**
* Settings loaded from configuration file
*/
private Properties data;
/**
* File name with cost information
*/
private String costFileName;
/**
* File name with node information
*/
private String nodeFileName;
/**
* File name with vehicle information
*/
private String vehicleFileName;
/**
* Total number of nodes (excluding depot)
*/
private int nodes;
/**
* Cost matrix (symmetric) c_{ij}, i<=j.
*/
private HashMap [] cost;
/**
* Node data
*/
private VRPNode[] nodeData;
//private HashMap nodeData;
/**
* Vehicles' capacity.
*/
private float capacity;
/**
* Constructor for objects of class CVRPSettings
*/
private CVRPSettings(){
}
/**
* Returns the singleton instance of {@link CVRPSettings}. If it does
* not exist, it creates it.
*
* It uses the problem data file from {@link GASettings}
*
* @return A handle to the unique instance of
* {@link CVRPSettings}.
*
*/
public static CVRPSettings instance(String fileName) {
if( instance == null) {
instance = new CVRPSettings();
instance.load(fileName);
instance.initializeElements();
}
return instance;
}
public static CVRPSettings instance() {
return instance;
}
/**
* Loads the configuration file into a {@link Properties} object.
*
* @param fn Name of the configuration file
*/
private void load(String fn){
costFileName = new String(fn);
data = new Properties();
try {
FileInputStream sf = new FileInputStream(fn);
data.load(sf);
}
catch (FileNotFoundException e){
System.out.println("JGA> File with problem properties not
found.");
e.printStackTrace();
}
catch (IOException e){
System.out.println("JGA> File I/O problem.");
e.printStackTrace();
}
}
/**
* Initializes the globally accessible parameters of the VRP
*/
private void initializeElements(){
nodes = (new Integer(getProperty("NODES"))).intValue();
cost = loadCostCSV(getProperty("COST_FILE"));
nodeData = loadNodeDataCSV(getProperty("NODES_FILE"));
capacity = loadVehicleDataCSV(getProperty("VEHICLES_FILE"));
print();
}
/**
* Loads costs from a CSV formatted file. Assumes nodes
* are numbered 1..N.
*/
private HashMap[] loadCostCSV(String fn){
HashMap[] rows = new HashMap[nodes];
for (int i=0; i<nodes; i++){
rows[i] = new HashMap();
}
try{
File f = new File(fn);
CSVParser parser = new CSVParser(new FileInputStream(f));
parser.setCommentStart("#;!");
parser.setEscapes("nrtf", "\n\r\t\f");
String[] line; // line[0]: tail node; line[1]: head node; line[2]:
cost
int i;
Integer j;
Float c;
boolean moreLines = true;
while ( moreLines ) {
line = parser.getLine();
if (line == null) break;
else{
// Build matrix
i=Integer.valueOf(line[0]).intValue(); // node i
j=Integer.valueOf(line[1]); // node j
c=Float.valueOf(line[2]); // cost c_{ij}
//rows[i-1].put(j,c);
rows[i].put(j,c);
}
}
}
catch (IOException e){
System.out.println("JGA> Error reading cost file " + fn + ".");
}
return rows;
}
/**
* Loads demands from a CSV formatted file. Assumes nodes
* are numbered 1..N. Depot is number 1.
*/
private VRPNode[] loadNodeDataCSV(String fn){
VRPNode[] nodeData = new VRPNode[nodes];
try{
File f = new File(fn);
CSVParser parser = new CSVParser(new FileInputStream(f));
parser.setCommentStart("#;!");
parser.setEscapes("nrtf", "\n\r\t\f");
String[] line; // line[0]: tail node; line[1]: head node; line[2]:
cost
int i;
float d;
boolean moreLines = true;
while ( moreLines ) {
line = parser.getLine();
if (line == null) break;
else{
// Build array
i=Integer.valueOf(line[0]).intValue(); // node i
d=Float.valueOf(line[1]).floatValue(); // demand d_{i}
//nodeData[i-1]=new VRPNode(i,d);
nodeData[i]=new VRPNode(i,d);
}
}
}
catch (IOException e){
System.out.println("JGA> Error reading node data " + fn + ".");
}
return nodeData;
}
/**
* Loads vehicles' capacity from a CSV formatted file.
*/
private float loadVehicleDataCSV(String fn){
float cap=0.0f;
try{
File f = new File(fn);
CSVParser parser = new CSVParser(new FileInputStream(f));
parser.setCommentStart("#;!");
parser.setEscapes("nrtf", "\n\r\t\f");
String[] line; // line[0]: tail node; line[1]: head node; line[2]:
cost
int i;
float d;
boolean moreLines = true;
while ( moreLines ) {
line = parser.getLine();
if (line == null) break;
else{
cap=Float.valueOf(line[1]).floatValue(); // capacity
}
}
}
catch (IOException e){
System.out.println("JGA> Error reading vehicle capacity from file
" + fn + ".");
}
return cap;
}
private String getProperty(String key){
return data.getProperty(key);
}
/**
* Returns the total number of nodes to be visited in the problem
* (excludes origin node).
*
* @return total number of nodes to be visited
*
*/
public int getNodes(){
return nodes;
}
/**
* Returns the cost of moving from node i to node j. Assumes
* nodes are numbered 1..N (where N is the cardinality of
* the nodes set). Costs are defined c_{ij} with i <= j.
* If i>j, returns c_{j,i}.
*
* @return cost from node i to node j.
*
*/
public float getCost(int i, int j){
float c;
if (i <= j ){
//c=((Float)cost[i-1].get(new Integer(j))).floatValue();
//System.out.println("c(" + i + " , " + j + ")" );
c=((Float)cost[i].get(new Integer(j))).floatValue();
}
else {
//c=((Float)cost[j-1].get(new Integer(i))).floatValue();
c=((Float)cost[j].get(new Integer(i))).floatValue();
}
return c;
}
public float getDemand(int i){
//return nodeData[i-1].getDemand();
return nodeData[i].getDemand();
}
public float getCapacity(){
return capacity;
}
public void print(){
System.out.println("*************************************************");
System.out.println("JGA> Problem data file <"+costFileName+">
");
System.out.println("nodes = " + nodes);
System.out.println("Cost matrix:");
for (int i=0; i<nodes-1; i++){
for (int j=i+1; j<nodes; j++ ){
System.out.println("i:"+i+"\tj:"+j+"\tc:"+getCost(i,j));
}
}
System.out.println("*************************************************");
System.out.println("Node data:");
for (int i=0; i<nodes; i++){
System.out.println("i:"+i + "\td:"+ getDemand(i) );
}
System.out.println("*************************************************");
System.out.println("Vehicle data:");
System.out.println("Capacity:\t" + getCapacity() );
System.out.println("*************************************************");
}
}
---END FILE----
Hope this helps.
Andrés
Andres
Regards
zia
Andrés
On Oct 3, 11:52 am, sha...@soton.ac.uk wrote:
> thanx a lot for ur efforts and time.
> I wanted to save myself some time as I needed to test my models (before my
> transfer exam. due soon) for different problem sizes and a variety of
> targetstart times, durations and unit costs. I have already such datsets
> fully prepared and formated and for which integer programming and
> heuristics are already available which will make comparison and validation
> easy. To make a seperate file of each instance for each problem size will
> take a lot of time. Thats y I wanted the program to read all the instances
> one by one from one file only and each time send the result to the same
> file and I need not to change the formate of the datasets i have a lot and
> also make full use of JGA.
>
> Regards
> zia
>