-
Notifications
You must be signed in to change notification settings - Fork 2
/
AdjMatrixGraph.java
291 lines (257 loc) · 7.74 KB
/
AdjMatrixGraph.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// this code comes from http://algs4.cs.princeton.edu/41undirected/AdjMatrixGraph.java.html
// I have made modifications to suit my needs
import java.util.Iterator;
import java.util.*;
import java.math.*;
import java.io.*;
public class AdjMatrixGraph {
private int V;
private int E;
private boolean[][] adj;
//empty graph with V vertices
public AdjMatrixGraph(int V, boolean complete) {
if (V < 0) throw new RuntimeException("Number of vertices must be nonnegative");
this.V = V;
this.E = 0;
this.adj = new boolean[V][V];
}
// Complete graph with V vertices
public AdjMatrixGraph(int V) {
this(V, true);
int totalEdges = (V*(V-1))/2;
// can be inefficient
for(int i = 0; i<V; i++)
{
for(int j = 0; j<V; j++)
{
if(i!=j) addEdge(i, j);
}
}
}
// number of vertices and edges
public int getVertices() { return this.V; }
public int getEdges() { return this.E; }
// add undirected edge v-w
public void addEdge(int v, int w) {
if (!adj[v][w]) this.E++;
adj[v][w] = true;
adj[w][v] = true;
}
// does the graph contain the edge v-w?
public boolean contains(int v, int w) {
return adj[v][w];
}
// return list of neighbors of v
public Iterable<Integer> adj(int v) {
return new AdjIterator(v);
}
// support iteration over graph vertices
private class AdjIterator implements Iterator<Integer>, Iterable<Integer> {
int v, w = 0;
AdjIterator(int v) { this.v = v; }
public Iterator<Integer> iterator() { return this; }
public boolean hasNext() {
while (w < V) {
if (adj[v][w]) return true;
w++;
}
return false;
}
public Integer next() {
if (hasNext()) { return w++; }
else { throw new NoSuchElementException(); }
}
public void remove() { throw new UnsupportedOperationException(); }
}
// string representation of Graph - takes quadratic time
public String toString() {
String NEWLINE = System.getProperty("line.separator");
StringBuilder s = new StringBuilder();
s.append("V: "+V + " " + " E: "+E + NEWLINE);
for (int v = 0; v < V; v++) {
s.append(v + ": ");
for (int w : adj(v)) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
//takes a file (in the buffreader) and loads it into a population
//for pre-formed pops, not random
public static Population graphLoad(BufferedReader b, AdjMatrixGraph G)
{
Population p = new Population();
ArrayList<Chromosome> poplist = new ArrayList<Chromosome>();
int linenum = 0;
try{
String read = b.readLine();
boolean[][] c = new boolean[G.getVertices()][G.getVertices()]; //holds the colors in a matrix
//while((read.contains("0") || read.contains("1")) && read != null)
while(read != null)
{
//we want only the lines with numbers, this is how we will break it up
if((read.contains("0") || read.contains("1")))
{
//System.out.println(read);
for(int pos = 0; pos<read.length(); pos++)
{
if(read.charAt(pos) == '0')
{
//use these positions to set up a colormatrix
//System.out.println("0 at position "+linenum+","+pos/2);
//load these values into a matrix
c[linenum][pos/2] = false; //0
}
else if(read.charAt(pos) == '1')
{
//System.out.println("1 at position "+linenum+","+pos/2);
c[linenum][pos/2] = true; //1
}
else
{
//other stuff
}
}
//gets the next line
read = b.readLine();
linenum++;
}
else
{
//testing values of the matrix
/*for(int first=0; first<c.length; first++)
{
for(int second=0; second<c.length; second++)
{
System.out.print(c[first][second]+" ");
}
System.out.println();
}*/
//end of a block, that means load in the matrix to an object
ColorMatrix cm = new ColorMatrix(G, c);
//cm.printColoring();
Chromosome chro = new Chromosome(cm);
//add this Chromosome to a list so we can have a pop at the end
poplist.add(chro);
//reset to read in a new block of nums
linenum = 0;
//reset the matrix
/*for(int a = 0; a<G.getVertices(); a++)
{
for(int d = 0; d<G.getVertices(); d++)
{
c[a][d] = false;
}
}*/
read = b.readLine();
}
}
//System.out.println(poplist);
for(Chromosome chromo: poplist)
{
//c.getColorMatrix().printColoring();
//System.out.println("\n");
p.addChromosome(chromo);
}
}catch(Exception e)
{e.printStackTrace(System.out);}
return p;
}
// test client
public static void main(String[] args) {
long startTime = System.nanoTime();
//gets the number of vertices we want
int V = Integer.parseInt(args[0]);
//makes a new graph object
AdjMatrixGraph G = new AdjMatrixGraph(V);
//after this, we want to check if we have a file to pull from
System.out.println("if you have a data file, enter the name now:");
Scanner in = new Scanner(System.in);
String file = in.nextLine();
BufferedReader br = null;
DataInputStream data = null;
try{
FileInputStream fstream = new FileInputStream(file);
data = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(data));
}catch(Exception e){
System.out.println("file didn't work");
}
Population pop = null;
if(!br.equals(null))
{
pop = graphLoad(br, G); //load in the data from the file
System.out.println(pop);
try{
data.close();
}catch(Exception e)
{}
System.exit(1); //for testing purposes, no need to do all the other stuff yet
}
else{
//print the graph
//System.out.println(G);
//System.out.println("\n");
//get a random coloring
ColorMatrix c = new ColorMatrix(G);
//System.out.println("\nColoring: ");
//make a new Chromosome (basically just a ColorMatrix)
Chromosome chr = new Chromosome(c);
//print our coloring
//c.printColoring();
//get fitness (number of same colored cliques of size x (x is the parameter)
int fit = chr.getFitness(5);
//System.out.println("Number of same colored triangles: "+fit+"\n");
//make a new population
pop = new Population(200, G.getVertices());
}
//begin the mating process!!!
int gen = 0;
while(pop.getWorst().getFitness(5)>0)
//while(gen<10)
{
System.out.println("Generation "+gen);
System.out.println("=================");
pop = Genetics.evolve(pop);
System.out.println(pop);
System.out.println("\n");
gen++;
}
System.out.println("Took "+gen+" generations");
long endTime = System.nanoTime();
long elapsed = endTime-startTime;
double seconds = (double)elapsed / 1000000000.0;
double minutes = (double)seconds / 60;
System.out.println("Took "+(seconds) + " s");
System.out.println("Took "+(minutes) + " m");
//print the "0s" and their colorings to a file
//do further testing on these
PrintWriter writer = null;
int number = 0;
System.out.println(pop);
try{
writer = new PrintWriter("datazeroes.txt", "UTF-8");
for(Chromosome chrom:pop.getPop())
{
//if(chrom.getFitness(5)==0)
// {
//chrom.getColorMatrix().printColoring();
//System.out.println();
//number++;
//System.out.println(number);
writer.println(chrom.getColorMatrix().getColoring());
writer.println();
// }
}
writer.close();
System.out.println("possible zeroes dumped");
}
catch(Exception e)
{
System.out.println("fail");
}
//print the population
//System.out.println(pop);
}
}