-
Notifications
You must be signed in to change notification settings - Fork 0
/
AGUniforme.cs
42 lines (37 loc) · 1.19 KB
/
AGUniforme.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace prGenetico
{
class AGUniforme:AlgoritmoGenetico
{
public AGUniforme(int tP, int tI, int np, double pMut, IProblema pr):base(tP,tI,np,pMut,pr)
{
}
public override Cromosoma Recombinar(Cromosoma cr1, Cromosoma cr2)
{
if (cr1.Longitud != cr2.Longitud) { throw new Exception("Intenta recombinar cromosomas de distinta longitud"); }
else
{
Random rnd = new Random();
int padre = 0;
Cromosoma nuevoCromosoma = new Cromosoma(cr1.Longitud, false);
for(int i=0;i<cr2.Longitud;i++)
{
padre = rnd.Next(2); // Si padre=0 -> Toma el gen de cr1; padre=1-> Toma el de cr2
if (padre == 0)
{
nuevoCromosoma[i] = cr1[i];
}
else
{
nuevoCromosoma[i] = cr2[i];
}
}
return nuevoCromosoma;
}
}
}
}