-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation.cpp
More file actions
99 lines (78 loc) · 2.31 KB
/
Copy pathmutation.cpp
File metadata and controls
99 lines (78 loc) · 2.31 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<bits/stdc++.h>
#include "global.h"
using namespace std;
void swap(individuo &ind){// De toda la vida
//Swap multiple
int moves_size = ind.moves.size();
for (int i = 0; i < moves_size; i++)
{
double p = getRandomProb();
if(p <= params.pmut_swap){
int a = i;
int b = getRandomInt(0,moves_size-1);
int aux = ind.moves[a];
ind.moves[a] = ind.moves[b];
ind.moves[b] = aux;
}
}
//SWAP único
/*
//Seleccionamos dos indices
int a = getRandomInt(0, ind.moves.size()-1);
int b = getRandomInt(0, ind.moves.size()-1);
while(b == a) b = getRandomInt(0, ind.moves.size()-1);
//Realizamos el cambio
int aux = ind.moves[a];
ind.moves[a] = ind.moves[b];
ind.moves[b] = aux;
*/
}
void inversion(individuo &ind){// Entre dos puntos random
int moves_size = ind.moves.size();
// double p = getRandomProb();
//if(p <= params.pmut_inversion*moves_size){ -> creo que esto no debería estar
int a = getRandomInt(0,moves_size-1);
int b = getRandomInt(0,moves_size-1);
//"a" debe ser menor o igual a b
if(a>b){int aux = a; a = b; b = aux;}
//Invertimos sección entre a y b incluidos
vector<int> section;
for (int i = a; i < b+1; i++)
{
section.push_back(ind.moves[i]);
}
for (int i = a; i < b+1; i++)
{
ind.moves[i] = section[b-i];
}
// }
}
void intFlip(individuo &ind){// De toda la vida
int moves_size = ind.moves.size();
for (int i = 0; i < moves_size; i++)
{
double p = getRandomProb();
if(p <= params.pmut_intFlip){
int j = getRandomInt(0,moves_size-1);
ind.moves[i] = getRandomInt(1,params.n_heu);
}
}
}
//Mutar poblacion, con una probabilidad de pmut por individuo
/*
void mutatePop(vector<individuo> &pop){
for (int i = params.elite; i < params.popsize; i++)
{
int mut = getRandomInt(1,3);
if(mut == 1){
swap(pop[i]);
}
else if(mut == 2){
inversion(pop[i]);
}
else{
intFlip(pop[i]);
}
}
}
*/