class Individual {
constructor(genome){
this._genome= genome;
this.transitions = transitions;
this.bottom = transitions.map(t=>t.weight*transitions.length).reduce((acc,curr)=>acc+curr,0)*10;
}
replicate(){
return new Individual(this.genome);
}
get fitness(){
const crossovers = this.crossOvers
return this.bottom - 10*this.crossOvers
}
get crossOvers(){
let crossOvers = 0;
for(const transition of this.transitions){
crossOvers+=Math.abs(this._genome.indexOf(transition.source)- this._genome.indexOf(transition.target))*transition.weight
}
return crossOvers;
}
get genome(){
return this._genome.slice();
}
mutate(){
const swaps = [Math.round(Math.random()*(this._genome.length-1)),Math.round(Math.random()*(this._genome.length-1))].sort((a,b)=>a-b);
if(swaps[0]!==swaps[1]){
this._genome = [...this._genome.slice(0,swaps[0]),this._genome[swaps[1]],...this._genome.slice(swaps[0]+1,swaps[1]),this._genome[swaps[0]],...this._genome.slice(swaps[1]+1)]
}
}
}