-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesignCircuit.java
executable file
·195 lines (170 loc) · 5.25 KB
/
designCircuit.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import GeneticAlgorithm.GateFunctions.Gates;
import GeneticAlgorithm.Cell.Cell;
import GeneticAlgorithm.Population.Population;
import GeneticAlgorithm.GeneticOperations.Fitness;
import GeneticAlgorithm.Population.Chromosome;
import GeneticAlgorithm.GeneticOperations.Selection;
import GeneticAlgorithm.GeneticOperations.Crossover;
import GeneticAlgorithm.GeneticOperations.Mutate;
import java.util.Scanner;
import java.util.List;
import java.util.Arrays;
// Class to perform the genetic algorithm and solve the given function
public class designCircuit
{
public static void main (String [] args)
{
// GA parameters
int TournamentSize = 5;
int populationSize = 150;
double crossoverRate = 0.7;
double mutationRate = 0.4;
int circuitRow = 3;
int circuitCol = 3;
Scanner scan = new Scanner(System.in);
String min = "";
List<String> items;
int generation = 0;
boolean found = false;
Chromosome c1;
Chromosome c2;
Chromosome c3;
Chromosome c4;
int newChildren = 0;
// User parameters
int numOutputs;
int numInputs;
// Gets number of inputs from user
System.out.print("Enter number of inputs (valid range 3-5): ");
numInputs = scan.nextInt();
System.out.println();
// Gets number of outputs from user
System.out.print("Enter number of outputs (current max is " + circuitCol + "): ");
numOutputs = scan.nextInt();
System.out.println();
// Array to hold users minterms from the user
boolean[][] minterms = new boolean[numOutputs][(int) Math.pow(2,numInputs)];
// Initiaze minterm array to flase
for (int x = 0; x<numOutputs; x++)
for (int y = 0; y < (int) Math.pow(2,numInputs); y++)
minterms[x][y] = false;
// Get minterms from user and insert into array
for (int f = 0; f<= numOutputs; f++)
{
if (f != 0)
System.out.print("Enter the minterms for function " + f +" separated by comas:");
min = scan.nextLine();
if (f != 0)
System.out.println();
if (f != 0)
{
items = Arrays.asList(min.split("\\s*,\\s*"));
for (int e = 0; e < items.size(); e++)
{
minterms[f-1][Integer.parseInt(items.get(e))] = true;
}
}
}
/*
// Prints out minterms
for (int r=0;r<numOutputs;r++)
for (int t=0;t<(int) Math.pow(2,numInputs);t++)
System.out.println("Minterms[" + r + "]" + "[" + t + "]" + " = "+ minterms[r][t]);
*/
// Initialize the population
Population p1 = new Population(populationSize, circuitRow, circuitCol, numInputs, numOutputs);
p1.initPop();
// Calculate and set the fitness for each chromosome
for (int i = 0; i<populationSize; i++)
{
c1 = p1.getChromosome(i);
setFit(c1,numOutputs,numInputs,minterms,0);
}
// Keep going until we have a fully functional circuit
while (!found)
{
// For the entire population
for (int j = 0; j < populationSize; j++)
{
// Probablity, if selected for crossover
if (Math.random() <= crossoverRate)
{
newChildren++;
// Select chromosmes for mateing
c1 = Selection.select(p1, TournamentSize);
c2 = Selection.select(p1, TournamentSize);
// Make two children
c3 = Crossover.crossover(c1,c2);
c4 = Crossover.crossover(c2,c1);
// Evaluate the fitness of the children
setFit(c3,numOutputs,numInputs,minterms,0);
setFit(c4,numOutputs,numInputs,minterms,0);
// Add the most fit to the population, disgaurd the other
if (c3.getFitness() >= c4.getFitness())
{
// Mutate child
if (Math.random() <= mutationRate)
{
Mutate.mutate(c3);
setFit(c3,numOutputs,numInputs,minterms,0);
}
// Insert child
p1.insertChromosome(c3);
}
else
{
// Mutate child
if (Math.random() <= mutationRate)
{
Mutate.mutate(c4);
setFit(c4,numOutputs,numInputs,minterms,0);
}
// Insert child
p1.insertChromosome(c4);
}
}
}
// Remove the weakest from the population, maintain original population size
for (int k = 0; k<newChildren; k++)
p1.deleteUnfit();
newChildren = 0;
generation++;
// Get the fittest out of the population
c1 = p1.getFittest();
// Print the most fit chromosome for each generation
System.out.println();
System.out.println("Generation: " + generation);
c1.printChromosome();
// If the fittest is a fully functional circuit
if (c1.getFitness() == (float)numOutputs)
{
setFit(c1,numOutputs,numInputs,minterms,0);
found = true;
}
}
System.out.println();
}
// Sets the chromosomes fitness for each output
// Note: Can be made more efficient by putting this function in Fitness.java
private static void setFit(Chromosome c1, int numOutputs, int numInputs, boolean[][] minterms, int debug)
{
float fitness = 0;
float totalFitness = 0;
int numCols = (int) Math.pow(2,numInputs);
// Holds one function
boolean minterm[] = new boolean[numCols];
// For every output
for (int k = 0; k < numOutputs; k++)
{
// Set the current function for the output
for (int i = 0; i < numCols; i++)
minterm[i] = minterms[k][i];
// Calculate the fitness of the chromosome at that output
fitness = Fitness.calcFitness(c1, minterm, k, debug);
totalFitness = totalFitness + fitness;
}
// Set the total fitness
// Total fitness = numOutputs*(fitness of each output)
c1.setFitness(totalFitness);
}
}