Advice needed with genetic algorithm and TSP
in
Programming Questions
•
3 months ago
Hello all!! First of all, excuse me about my english.
I'm working on a genetic algorithm to solve the TSP. I already have a program that does the work, but I want to improve it. Particulary the crossover function. This is what I have so far:
- class Path implements Comparable {
- Map myMap;
- IntList item;
- Path(Map _myMap){
- myMap = _myMap;
- // init a random path
- item = new IntList();
- for(int i=0; i < myMap.n_cities; i++){
- item.append(i);
- }
- item.shuffle();
- }
- ....
- Path crossover(Path partner){
- Path son = new Path(myMap);
- int cut = int(random(this.item.size()));
- son.clear();
- for(int i=0; i < cut; i++)
- son.item.append(this.item.get(i));
- for(int i=0; i < partner.item.size(); i++){
- if(!son.item.hasValue(partner.item.get(i)))
- son.item.append(partner.item.get(i));
- }
- return son;
- }
- ........
- }
Now, I'd like to select a subset inbetween the parent A, copy it to the child, and then complete it with the elements of B. (same concept, but this time cutting the chromosome in 3 points).
Do you have any idea how can I do this??
I figured a way to do it, but I need to use some methods of the IntList class like .getSubset() or .insert(), but I dont know why I cant use them. Here is the reference.
Well, I hope you can help me. Thanks a lot!!!
1