Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ArrayList;
import java.util.Random;
public class TSP1
{
private static final int START_SIZE = 75;
// Population s
ize at start.
private static final int MAX_EPOCHS = 1000;
// Arbitrary nu
mber of test cycles.
private static final double MATING_PROBABILITY = 0.7;
// Probability
of two chromosomes mating. Range: 0.0 < MATING_PROBABILITY < 1.0
private static final double MUTATION_RATE = 0.001;
// Mutation Rat
e. Range: 0.0 < MUTATION_RATE < 1.0
private static final int MIN_SELECT = 10;
// Minimum pare
nts allowed for selection.
private static final int MAX_SELECT = 50;
// Maximum pare
nts allowed for selection. Range: MIN_SELECT < MAX_SELECT < START_SIZE
private static final int OFFSPRING_PER_GENERATION = 20;
// New offsprin
g created per generation. Range: 0 < OFFSPRING_PER_GENERATION < MAX_SELECT.
private static final int MINIMUM_SHUFFLES = 8;
// For randomiz
ing starting chromosomes
private static final int MAXIMUM_SHUFFLES = 20;
private static final boolean SHOW_VERBOSE_RESULTS = false;
private static
private static
private static
ng mutations.
private static
int epoch = 0;
int childCount = 0;
int nextMutation = 0;
// For scheduli
int mutations = 0;
static
static
static
find.
static
static
}
getFitness();
if(SHOW_VERBOSE_RESULTS == true){
popSize = population.size();
for(int i = 0; i < popSize; i++)
{
thisChromo = population.get(i);
for(int j = 0; j < CITY_COUNT; j++)
{
System.out.print(thisChromo.data(j));
} // j
System.out.print(" = " + thisChromo.total());
System.out.print("\t" + thisChromo.fitness() + "%\n"
);
} // i
System.out.print("\n");
}
rouletteSelection();
mating();
prepNextEpoch();
epoch++;
// This is here simply to show the runtime status.
System.out.println("Epoch: " + epoch);
}
System.out.println(printBestFromPopulation());
System.out.println("done.");
if(epoch != MAX_EPOCHS){
popSize = population.size();
for(int i = 0; i < popSize; i++)
{
thisChromo = population.get(i);
if(Math.abs(thisChromo.total() - TARGET) <= 0.001){
// Print the chromosome.
for(int j = 0; j < CITY_COUNT; j++)
{
System.out.print(thisChromo.data(j) + ", ");
} // j
System.out.print("\n");
}
} // i
}
System.out.println("Completed " + epoch + " epochs.");
System.out.println("Encountered " + mutations + " mutations in " + child
Count + " offspring.");
return;
}
private static void initializeMap()
{
cCity city = null;
{
// Overloaded function, see also "chooseparent()".
int parent = 0;
Chromosome thisChromo = null;
boolean done = false;
while(!done)
{
// Randomly choose an eligible parent.
parent = getRandomNumber(population.size() - 1);
if(parent != parentA){
thisChromo = population.get(parent);
if(thisChromo.selected() == true){
done = true;
}
}
}
return parent;
}
private static void partiallyMappedCrossover(final int chromA, final int chr
omB, final int childIndex)
{
int j = 0;
int crossPoint1 = 0;
int crossPoint2 = 0;
int item1 = 0;
int item2 = 0;
int pos1 = 0;
int pos2 = 0;
Chromosome thisChromo = population.get(chromA);
Chromosome thatChromo = population.get(chromB);
Chromosome newChromo = population.get(childIndex);
crossPoint1 = getRandomNumber(CITY_COUNT - 1);
crossPoint2 = getExclusiveRandomNumber(CITY_COUNT - 1, crossPoint1);
if(crossPoint2 < crossPoint1){
j = crossPoint1;
crossPoint1 = crossPoint2;
crossPoint2 = j;
}
// Copy parentA genes to offspring.
for(int i = 0; i < CITY_COUNT; i++)
{
newChromo.data(i, thisChromo.data(i));
}
for(int i = crossPoint1; i <= crossPoint2; i++)
{
// Get the two items to swap.
item1 = thisChromo.data(i);
item2 = thatChromo.data(i);
// Get the items' positions in the offspring.
for(int k = 0; k < CITY_COUNT; k++)
{
if(newChromo.data(k) == item1){
pos1 = k;
int popSize = 0;
Chromosome thisChromo = null;
Chromosome thatChromo = null;
int winner = 0;
boolean foundNewWinner = false;
boolean done = false;
while(!done)
{
foundNewWinner = false;
popSize = population.size();
for(int i = 0; i < popSize; i++)
{
if(i != winner){
// Avoid self-comparison.
thisChromo = population.get(i);
thatChromo = population.get(winner);
// The minimum has to be in relation to the TARGET.
if(Math.abs(TARGET - thisChromo.total()) > Math.abs(TARGET thatChromo.total())){
winner = i;
foundNewWinner = true;
}
}
}
if(foundNewWinner == false){
done = true;
}
}
return winner;
}
private static class Chromosome
{
private int mData[] = new int[CITY_COUNT];
private int mRegion = 0;
private double mTotal = 0.0;
private double mFitness = 0.0;
private boolean mSelected = false;
private int mAge = 0;
private double mSelectionProbability = 0.0;
public Chromosome()
{
mTotal = 0.0;
return;
}
public double selectionProbability()
{
return mSelectionProbability;
}
public void selectionProbability(final double SelProb)
{
mSelectionProbability = SelProb;
return;
}
public int age()
{
return mAge;
}
public void age(final int epochs)
{
mAge = epochs;
return;
}
public boolean selected()
{
return mSelected;
}
public void selected(final boolean sValue)
{
mSelected = sValue;
return;
}
public double fitness()
{
return mFitness;
}
public void fitness(final double score)
{
mFitness = score;
return;
}
public double total()
{
return mTotal;
}
public void total(final double Value)
{
mTotal = Value;
return;
}
public int region()
{
return mRegion;
}
public void region(final int regionNumber)
{
mRegion = regionNumber;
return;
}
public int data(final int index)
{
return mData[index];
}
public void data(final int index, final int value)
{
mData[index] = value;
return;
}
} // Chromosome
private static class cCity
{
private int mX = 0;
private int mY = 0;
public int x()
{
return mX;
}
public void x(final int xCoordinate)
{
mX = xCoordinate;
return;
}
public int y()
{
return mY;
}
public void y(final int yCoordinate)
{
mY = yCoordinate;
return;
}
} // cCity
public static void main(String[] args)
{
initializeMap();
geneticAlgorithm();
return;
}
}