This commit is contained in:
Torben Zwinge 2023-05-19 17:06:12 +02:00
parent 7af92e1295
commit bcb13dda69
7 changed files with 216 additions and 6 deletions

View file

@ -0,0 +1,51 @@
import java.lang.reflect.Array;
import java.util.Random;
public class Individuum
{
private CityNode[] order;
public Individuum(CityNode[] order)
{
this.order = order;
}
public Individuum clone()
{
Individuum tmp = new Individuum(order.clone());
return tmp;
}
public Double getWeight()
{
double totalWeight = 0;
for(int i = 0; i< order.length; i++)
{
int j = i + 1;
if(j==order.length)
{
j = 0;
}
totalWeight = totalWeight + order[i].getRoutes().get(order[j]);
}
return totalWeight;
}
public void mutate(double mutateProbability)
{
for(int i = 0; i< order.length; i++)
{
int j = i + 1;
if(j==order.length)
{
j = 0;
}
if(new Random().nextDouble() <= mutateProbability)
{
CityNode tmp = order[i];
order[i] = order[j];
order[j] = tmp;
}
}
}
}

View file

@ -8,6 +8,8 @@ public class Main {
public static void run()
{
XMLReader xmlReader = new XMLReader();
new TSP(xmlReader.readXML());
new TSP(xmlReader.readXML(),6,3,0.5,1);
}
}

View file

@ -0,0 +1,97 @@
import java.util.*;
public class Population
{
public Set<Individuum> population = new HashSet<>();
public Population(Set<Individuum> population)
{
this.population.addAll(population);
}
public void selektion(int anzahl)
{
Iterator<Individuum> iterator = population.iterator();
for(int i = 0; i<anzahl; i++)
{
iterator = population.iterator();
double highestWeight = 0;
Individuum worstIndividuum = null;
while (iterator.hasNext()) {
Individuum tmp = iterator.next();
double weight = tmp.getWeight();
if (highestWeight == 0 || weight > highestWeight) {
worstIndividuum = tmp;
highestWeight = weight;
}
}
population.remove(worstIndividuum);
}
}
public void variation(int addIndividuum, double mutateProbability)
{
Iterator<Individuum> iterator = population.iterator();
for(int i = 0; i<addIndividuum;i++)
{
population.add(iterator.next().clone());
}
iterator = population.iterator();
while(iterator.hasNext())
{
iterator.next().mutate(mutateProbability);
}
}
public Integer[] rekombination(Integer[] parent1, Integer[] parent2)
{
Integer size = parent1.length;
Integer[] child = new Integer[size];
Arrays.fill(child,-1);
Random random = new Random();
Integer start = random.nextInt() * size;
Integer ende = random.nextInt() * size;
for(int i = start; i<ende;i++)
{
child[i] = parent1[i];
}
int currentIndex = (ende + 1) % size;
for(int i = 0; i<size;i++)
{
if(child[i] == -1)
{
while(contains(child,parent2[currentIndex]))
{
currentIndex = (currentIndex + 1) % size;
}
child[i] = parent2[currentIndex];
currentIndex = (currentIndex + 1) % size;
}
}
return child;
}
private boolean contains(Integer[] array,Integer value)
{
for(int i : array)
{
if(i == value)
{
return true;
}
}
return false;
}
@Override
public String toString()
{
Iterator<Individuum> iterator = population.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next().getWeight());
}
return null;
}
}

View file

@ -1,14 +1,74 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class TSP {
public TSP(Map map)
private Map map;
private Population population;
public TSP(Map map, int anzahlInit, int anzahlSelektion, double mutateProbability, int anzahlGenerationen)
{
TSPSolver(map);
this.map = map;
TSPSolver(map,anzahlInit,anzahlSelektion,mutateProbability,anzahlGenerationen);
}
private void TSPSolver(Map map)
private void TSPSolver(Map map, int anzahlInit, int anzahlSelektion, double mutateProbability, int anzahlGenerationen)
{
map.cities.forEach(x -> {
System.out.println(x.getRoutes().toString());
initPop(anzahlInit);
population.selektion(anzahlSelektion);
population.variation(anzahlSelektion, mutateProbability);
/*
for(int i = 0;i<anzahlGenerationen;i++) {
population.variation(anzahlSelektion, mutateProbability);
population.selektion(anzahlSelektion);
}
*/
outBest();
}
private void initPop(int anzahl)
{
Set<Individuum> population = new HashSet<>();
for(int i = 0;i<anzahl;i++)
{
population.add(zufälligesIndividuum());
}
this.population = new Population(population);
}
private Individuum zufälligesIndividuum()
{
CityNode[] order = new CityNode[map.cities.size()];
Random random = new Random();
map.cities.forEach(city ->
{
int i = random.nextInt(order.length);
while(order[i] != null)
{
i++;
if(i == order.length)
{
i = 0;
}
}
order[i] = city;
});
return new Individuum(order);
}
private void outBest()
{
/*
while(population.population.size() > 1) {
population.selektion(1);
}
*/
population.toString();
}
}