Skip to content

Commit ca1915f

Browse files
committed
Projetinho pra brincar de GA
0 parents  commit ca1915f

13 files changed

+302
-0
lines changed

.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-12">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>cardGA</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=12
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=12

bin/cardGA/Ga.class

4.35 KB
Binary file not shown.

bin/cardGA/MeuRunnable.class

453 Bytes
Binary file not shown.

bin/cardGA/MinhaThread.class

718 Bytes
Binary file not shown.

bin/cardGA/Principal.class

1.05 KB
Binary file not shown.

bin/module-info.class

145 Bytes
Binary file not shown.

src/cardGA/Ga.java

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package cardGA;
2+
3+
import java.util.Random;
4+
5+
public class Ga {
6+
private double SUMTARG = 36;
7+
private double PRODTARG = 360;
8+
int POP = 40;
9+
int LEN = 10;
10+
int GEN = 1000;
11+
double mR = 0.1;
12+
double cR = 0.75;
13+
int CHILDREN = 40;
14+
Random r = new Random();
15+
public int[][] genotipo = new int[POP][LEN];
16+
double[] rank = new double[POP];
17+
18+
void init_pop(){
19+
20+
for(int i=0 ; i < POP ; i++) {
21+
for(int j=0; j < LEN ; j++) {
22+
if(r.nextDouble() < 0.5) {
23+
genotipo[i][j] = 0;
24+
}else{
25+
genotipo[i][j] = 1;
26+
}
27+
}
28+
}
29+
}
30+
Double valida(int p) {
31+
32+
int sum=0;
33+
double mult=1;
34+
double scaled_sum_error, scaled_prod_error, combined_error;
35+
//1- BARALHO DA SOMA 2- BARALHO DA MULTIPLICAÇÃO
36+
for(int i = 0 ; i<LEN;i++) {
37+
if(genotipo[p][i] == 1){
38+
sum+=i+1;
39+
}else {
40+
mult*=(i+1);
41+
}
42+
}
43+
scaled_sum_error = (sum - SUMTARG) / SUMTARG;
44+
scaled_prod_error = (mult - PRODTARG) / PRODTARG;
45+
combined_error = Math.abs(scaled_sum_error) +
46+
Math.abs(scaled_prod_error);
47+
48+
return combined_error;
49+
}
50+
51+
void fitness (int[][]pop) {
52+
53+
int[]aux1 = null;
54+
double aux2;
55+
for(int i = 0; i < POP ; i++) {
56+
rank[i] = valida(i);
57+
}
58+
for(int i = 0 ; i < POP ; i++) {
59+
for(int j = 1 ; j < POP ; j++) {
60+
if(rank[i] > rank[j]) {
61+
62+
aux1 = pop[j];
63+
pop[j] = pop[i];
64+
pop[i] = aux1;
65+
66+
aux2 = rank[j];
67+
rank[j] = rank[i];
68+
rank[i] = aux2;
69+
70+
}
71+
}
72+
}
73+
}
74+
75+
int roulette() {
76+
77+
int p = 0;
78+
double sum=0;
79+
double c;
80+
81+
for(int i = 0 ; i < POP ; i++) {
82+
sum+=rank[i];
83+
}
84+
85+
c = r.nextDouble()*sum;
86+
87+
if(c > 0 && c < rank[0] ) {
88+
return 0;
89+
}else {
90+
for(int i = 0 ; i < POP-1; i++) {
91+
if(rank[i] > 0 && c < rank[i+1] ) {
92+
return (i+1);
93+
}
94+
}
95+
}
96+
return p;
97+
98+
}
99+
void printaBest(int n) {
100+
101+
double sum=0,mult=1;
102+
for(int j=0; j < LEN ; j++) {
103+
System.out.print(genotipo[0][j]);
104+
if(genotipo[0][j] == 1){
105+
sum+=j+1;
106+
}else {
107+
mult*=(j+1);
108+
}
109+
}
110+
System.out.println(" -> Score: "+rank[0]+" Soma : " + sum + " Mult : " + mult);
111+
112+
113+
System.out.println("Geração : "+ n );
114+
}
115+
116+
117+
118+
119+
void printaAll(int n) {
120+
for(int i=0 ; i < POP ; i++) {
121+
for(int j=0; j < LEN ; j++) {
122+
System.out.print(genotipo[i][j]);
123+
}
124+
//PRINTADA BRABA
125+
double sum=0,mult=1;
126+
for(int i1 = 0 ; i1<LEN;i1++) {
127+
if(genotipo[i][i1] == 1){
128+
sum+=i1+1;
129+
}else {
130+
mult*=(i1+1);
131+
}
132+
}
133+
System.out.println(" -> Score: "+rank[i]+" Soma : " + sum + " Mult : " + mult);
134+
135+
136+
}
137+
System.out.println("Geração : "+ n );
138+
}
139+
140+
void run() {
141+
142+
init_pop();
143+
int n = 0;
144+
int x1, x2 ,mp;
145+
int[][] aux = new int[POP][LEN];
146+
int cut;
147+
double m,c;
148+
//boolean ext = false; //QUANDO ESTACA
149+
fitness(genotipo);
150+
151+
152+
while(n<GEN) {
153+
154+
155+
156+
for(int i = 0 ; i < POP ; i++) {
157+
m = r.nextDouble();
158+
mp = r.nextInt(LEN);
159+
c = r.nextDouble();
160+
cut = r.nextInt(LEN);
161+
x1 = roulette();
162+
x2 = roulette();
163+
if(c < cR) {
164+
for(int j = 0 ; j < LEN ; j++) {
165+
166+
if(j<cut) {
167+
aux[i][j] = genotipo[x1][j];
168+
}else {
169+
aux[i][j] = genotipo[x2][j];
170+
}
171+
172+
}
173+
}
174+
if(m < mR) {
175+
if(genotipo[x1][mp] == 0) {
176+
genotipo[x1][mp] = 1;
177+
}else {
178+
genotipo[x1][mp] = 0;
179+
}
180+
}
181+
}
182+
fitness(aux);
183+
184+
for(int i = 0 ; i < POP ; i++) {
185+
genotipo[i]=aux[i];
186+
}
187+
n++;
188+
189+
}
190+
printaBest(n);
191+
192+
}
193+
194+
public static void main(String[] args) {
195+
196+
Ga a = new Ga();
197+
198+
a.run();
199+
200+
201+
}
202+
203+
204+
}

src/cardGA/MeuRunnable.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cardGA;
2+
3+
public class MeuRunnable implements Runnable{
4+
public Ga a;
5+
6+
@Override
7+
public void run() {
8+
a = new Ga();
9+
a.run();
10+
11+
}
12+
13+
}

0 commit comments

Comments
 (0)