-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
73 lines (57 loc) · 2.01 KB
/
Main.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
import java.util.Scanner;
import java.util.Arrays;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Random;
import java.io.File;
import java.io.IOException;
public class Main{
public static void main(String args[]) throws IOException {
Scanner reader = new Scanner(System.in);
System.out.println("Numero de generaciones:");
int gen = reader.nextInt();
System.out.println("Numero de individuos por generacion:");
int n = reader.nextInt();
System.out.println("Numero de individuos a considerar para siguiente generación:");
int s = reader.nextInt();
System.out.println("Numero de triangulos por individuo:");
int tri = reader.nextInt();
System.out.println("Probabilidad de mutación:");
double mutR = reader.nextDouble();
System.out.println("Cantidad de mutación:");
double mutA = reader.nextDouble();
System.out.println("Nombre del archivo de la imagen a recrear:");
reader.nextLine();
String file = reader.nextLine();
BufferedImage img = null;
img = ImageIO.read(new File(file));
Random rndGen = new Random( System.currentTimeMillis() );
Individual[] ind = new Individual[n];
Individual[] old = new Individual[n];
int i,j,m,f;
for(i = 0; i < n; i++)
ind[i] = new Individual(tri,img);
for(j = 0; j < gen; j++){
for(i = 0; i < n; i++)
old[i]=ind[i];
Arrays.sort(old);
System.out.println("Gen "+j+": "+old[n-1].fitness+", "+old[n-2].fitness+",... , "+old[n-s].fitness+",... , "+old[0].fitness);
if(j%10 == 0){
File output = new File("gen"+j+".png");
ImageIO.write(old[n-1].myImage, "png", output);
}
for(i = 0; i < n; i++){
if(i<s) ind[i]=old[n-1-i];
else{
m = rndGen.nextInt(s);
f = rndGen.nextInt(s);
ind[i] = new Individual(old[n-1-m], old[n-1-f], mutR, mutA);
}
}
}
Arrays.sort(ind);
System.out.println("Gen "+j+": "+ind[n-1].fitness+", "+ind[n-2].fitness+",... , "+ind[0].fitness);
File output = new File("image.png");
ImageIO.write(ind[n-1].myImage, "png", output);
}
}