Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
2.4 KiB

<html>
<head>
<script>
class Gene
{
constructor(min, max, value)
{
this.min = min;
this.max = max;
this.value = value;
}
getRealValue()
{
return (this.max - this.min) * this.value + this.min;
}
getValue()
{
return this.value;
}
setValue(val)
{
this.value = val;
}
makeClone()
{
return new Gene(this.min, this.max, this.value);
}
}
class Chromosome
{
constructor(geneArray)
{
this.genes = [];
for(let i = 0; i < geneArray.length; i++)
{
this.genes.push(geneArray[i].makeClone());
}
}
getGenes()
{
return this.genes;
}
mutate()
{
this.genes[Math.round(Math.random() * (this.genes.length-1))].setValue(Math.random());
}
}
const mate = function(father, mother)
{
let son = new Chromosome(father.getGenes());
let daughter = new Chromosome(mother.getGenes());
for(let i = 0;i < son.getGenes().length; i++)
{
let blendCoef = Math.random();
blendGene(son.getGenes()[i], daughter.getGenes()[i], blendCoef);
}
};
const blendGene = function(gene1, gene2, blendCoef)
{
let value1 = (blendCoef * gene1.getValue()) +
(gene2.getValue() * (1- blendCoef));
let value2 = ((1-blendCoef) * gene1.getValue()) +
(gene2.getValue() * blendCoef);
gene1.setValue(value1);
gene2.setValue(value2);
};
let gene1 = new Gene(1,10,.6);
let gene2 = new Gene(2,5,0.4);
let geneList = [gene1, gene2];
var c = new Chromosome(geneList);
var c2 = new Chromosome(c.getGenes());
console.log(c.getGenes());
console.log(c2.getGenes());
c.mutate();
c.mutate();
console.log(c.getGenes());
console.log(c2.getGenes());
</script>
</head>
<body>
</body>
</html>