-
Notifications
You must be signed in to change notification settings - Fork 1
/
TesterExp6.java
172 lines (143 loc) · 5.86 KB
/
TesterExp6.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import IA.Bicing.Estaciones;
import aima.search.framework.Problem;
import aima.search.framework.Search;
import aima.search.framework.SearchAgent;
import aima.search.informed.HillClimbingSearch;
import practica.PracBoard;
import practica.PracGoalTest;
import practica.PracHeuristicFunction;
import practica.PracSuccessorFunction;
public class TesterExp6
{
private static final int NUM_SEEDS = 100;
private static int seeds[];
public static void main(String args[]) throws Exception
{
try
{
PracBoard.TipoSolucion tipoSol = PracBoard.TipoSolucion.RANDOM;
String modos[] = { "ChangeSwapAdd", "ChangeChange2SwapAdd", "ChangeChange2Change3SwapAdd" };
initVars(modos.length);
String filePath = "./R/exp6.txt";
FileWriter fileWriter = new FileWriter(filePath);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("demanda\t");
bufferedWriter.write("tiempo\n");
//Inicialización estaciones ( en este caso, solo una vez )
int numEstaciones = 25;
int numBicis = 1250;
int maxFurgonetas = 5;
int[] Demanda = {Estaciones.EQUILIBRIUM, Estaciones.RUSH_HOUR};
//Para que la primera ejecución no tenga un tiempo mucho mayor que el resto
cargaEnCache();
for(int j = 0; j < 2; ++j) {
int tipoDemanda = Demanda[j];
for(int i = 0; i < seeds.length; ++i)
{
printProgreso(i);
if (j == 0) bufferedWriter.write("equilibrada\t");
else bufferedWriter.write("horaPunta\t");
int seed = seeds[i];
Estaciones estaciones = new Estaciones(numEstaciones, numBicis, tipoDemanda, seed);
//Búsqueda Hill Climbing
PracSuccessorFunction successorFunction = new PracSuccessorFunction(PracSuccessorFunction.SearchType.HillClimbing);
setOperadores(successorFunction,modos[0]);
PracBoard board = new PracBoard(estaciones, maxFurgonetas);
board.creaSolucionInicial(tipoSol,seed);
Problem p = new Problem(board, successorFunction, new PracGoalTest(), new PracHeuristicFunction(PracHeuristicFunction.Function.Heuristico_2));
Search alg = new HillClimbingSearch();
double startTime = System.nanoTime();
SearchAgent agent = new SearchAgent(p, alg);
double endTime = System.nanoTime();
double tiempo = endTime - startTime;
bufferedWriter.write(tiempo/1000000 + "\n");
}
}
bufferedWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
static private void initVars(int size)
{
seeds = new int[NUM_SEEDS];
for(int i = 0; i < seeds.length; ++i)
{
seeds[i] = i*3;
}
}
static private void printProgreso(int it)
{
int valores[] = {(2*NUM_SEEDS)/10, (4*NUM_SEEDS)/10, (6*NUM_SEEDS)/10,(8*NUM_SEEDS)/10};
for(int i = 0; i < valores.length; ++i)
{
if(it == valores[i])
{
System.out.println("Progreso: " + (i+1)*2 + "0%");
}
}
}
static private void setOperadores(PracSuccessorFunction successorFunction, String ops)
{
//"Change", "ChangeSwap", "ChangeSwapAdd", "ChangeChange2SwapAdd", "ChangeChange3SwapAdd", "ChangeChange2Change3SwapAdd", "ChangeChange2Change3Swap"
successorFunction.enableAllOperators();
if(ops == "Change")
{
successorFunction.disableAddVan();
successorFunction.disableChange2Est();
successorFunction.disableChange3Est();
successorFunction.disableSwapEst();
}
else if(ops == "ChangeSwap")
{
successorFunction.disableAddVan();
successorFunction.disableChange2Est();
successorFunction.disableChange3Est();
}
else if(ops == "ChangeSwapAdd")
{
successorFunction.disableChange2Est();
successorFunction.disableChange3Est();
}
else if(ops == "ChangeChange2SwapAdd")
{
successorFunction.disableChange3Est();
}
else if(ops == "ChangeChange3SwapAdd")
{
successorFunction.disableChange2Est();
}
else if(ops == "ChangeChange2Change3SwapAdd")
{
}
else if(ops == "ChangeChange2Change3Swap")
{
successorFunction.disableAddVan();
}
}
static private void cargaEnCache() throws Exception {
try
{
int seed = -1;
int numEstaciones = 25;
int numBicis = 1250;
int maxFurgonetas = 5;
Estaciones estaciones = new Estaciones(numEstaciones, numBicis, Estaciones.EQUILIBRIUM, seed);
//Búsqueda Hill Climbing
PracSuccessorFunction successorFunction = new PracSuccessorFunction(PracSuccessorFunction.SearchType.HillClimbing);
setOperadores(successorFunction,"ChangeSwapAdd");
PracBoard board = new PracBoard(estaciones, maxFurgonetas);
board.creaSolucionInicial(PracBoard.TipoSolucion.RANDOM,seed);
Problem p = new Problem(board, successorFunction, new PracGoalTest(), new PracHeuristicFunction(PracHeuristicFunction.Function.Heuristico_2));
Search alg = new HillClimbingSearch();
SearchAgent agent = new SearchAgent(p, alg);
}
catch (IOException e) {
e.printStackTrace();
}
}
}