GA/Assets/Scripts/Chromosome.cs
/// <summary>
/// Combines the DNAs of two parents and returns a child
/// (using Edge Recombination Crossover / ERX).
/// </summary>    
Combines the DNAs of two parents and returns a child
(using Edge Recombination Crossover / ERX).
Combines the DNAs of two parents and returns a child ( using Edge Recombination Crossover / ERX ) .
private Chromosome CrossoverERX(Chromosome partner) {
        var size = this.dna.Count;
        Chromosome child = new Chromosome(world);
        child.dna.Clear();
        var edges = new List<City>[world.cityCount];
        for (int i = 0; i < this.dna.Count; i++) {
            edges[i] = FindEdges(i, partner);
        }
        var currentCity = this.dna[0];
        while (child.dna.Count < this.dna.Count) {
            child.dna.Add(currentCity);
            for (int i = 0; i < edges.GetLength(0); i++) {
                edges[i].RemoveAll(x => x.index == currentCity.index);
            }
            if (edges[currentCity.index].Count == 0) {
                for (int i = 0; i < this.dna.Count; i++) {
                    if (!child.dna.Any(x => x.index == this.dna[i].index)) {
                        currentCity = this.dna[i];
                        break;
                    }
                }
            }
            else {
                var shortest = edges[currentCity.index][Random.Range(0, edges[currentCity.index].Count)];
                for (int j = 0; j < edges[currentCity.index].Count; j++) {
                    var edge = edges[currentCity.index][j];
                    if (edges[edge.index].Count < shortest.index) {
                        shortest = edge;
                    }
                }
                currentCity = shortest;
            }
        }
        return child;
    }
private Chromosome CrossoverERX ( Chromosome partner ) { var size = this . dna . Count ; Chromosome child = new Chromosome ( world ) ; child . dna . Clear ( ) ; var edges = new List < City > [ world . cityCount ] ; for ( int i = 0 ; i < this . dna . Count ; i ++ ) { edges [ i ] = FindEdges ( i , partner ) ; } var currentCity = this . dna [ 0 ] ; while ( child . dna . Count < this . dna . Count ) { child . dna . Add ( currentCity ) ; for ( int i = 0 ; i < edges . GetLength ( 0 ) ; i ++ ) { edges [ i ] . RemoveAll ( x => x . index == currentCity . index ) ; } if ( edges [ currentCity . index ] . Count == 0 ) { for ( int i = 0 ; i < this . dna . Count ; i ++ ) { if ( ! child . dna . Any ( x => x . index == this . dna [ i ] . index ) ) { currentCity = this . dna [ i ] ; break ; } } } else { var shortest = edges [ currentCity . index ] [ Random . Range ( 0 , edges [ currentCity . index ] . Count ) ] ; for ( int j = 0 ; j < edges [ currentCity . index ] . Count ; j ++ ) { var edge = edges [ currentCity . index ] [ j ] ; if ( edges [ edge . index ] . Count < shortest . index ) { shortest = edge ; } } currentCity = shortest ; } } return child ; }
Combines the DNAs of two parents and returns a child
(using Edge Recombination Crossover / ERX).
Combines the DNAs of two parents and returns a child ( using Edge Recombination Crossover / ERX ) .
partner
Chromosome