Skip to content

Commit

Permalink
Fixed bug when running in a headless system, isolated UI so program c…
Browse files Browse the repository at this point in the history
…an launch without crashing
  • Loading branch information
nanoandrew4 committed Aug 31, 2017
1 parent 1049460 commit 3381a95
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 89 deletions.
2 changes: 1 addition & 1 deletion gc/Fitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CircleData getBiggestCircle(Point2D[] circles, Random rand, boolean drawVoronoi)

p.relocate(polygon.getBounds().getX(), polygon.getBounds().getY());
p.setFill(Paint.valueOf(colors[rand.nextInt(colors.length)]));
Main.pane.getChildren().add(p);
UI.pane.getChildren().add(p);
}

circles[i] = new Point2D(polygon.getCentroid().getX(), polygon.getCentroid().getY());
Expand Down
8 changes: 4 additions & 4 deletions gc/GeneticAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ else if (c2.fitness == Integer.MAX_VALUE && Main.isValid(c2.getCircleData(), cir
c = c2;

if (c != null) {
if (Main.pane != null) {
if (UI.pane != null) {
CircleData cd = c.getCircleData();
Platform.runLater(() -> Main.draw(new Circle(cd.coords.getX(), cd.coords.getY(), cd.radius, Paint.valueOf("red")), gen));
Platform.runLater(() -> UI.draw(new Circle(cd.coords.getX(), cd.coords.getY(), cd.radius, Paint.valueOf("red")), gen));
}
System.out.println(output = "Circle with seed " + seed + " from generation " + gen + " with proximity of " + ((Double.valueOf(c.decode().split("-")[0])) / largestRadius) * 100d
+ "% and radius " + (Integer.valueOf(c.decode().split("-")[0])) + " is a valid solution");
Expand All @@ -102,8 +102,8 @@ else if (c2.fitness == Integer.MAX_VALUE && Main.isValid(c2.getCircleData(), cir
//Platform.runLater(() -> {gc.Main.draw(Integer.valueOf(genes[1]), Integer.valueOf(genes[2]), Integer.valueOf(genes[0]));});

// will run algorithm faster
if (Main.pane != null)
Platform.runLater(() -> Main.draw(-1, -1, -1, gen));
if (UI.pane != null)
Platform.runLater(() -> UI.draw(-1, -1, -1, gen));
}
}

Expand Down
91 changes: 7 additions & 84 deletions gc/Main.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
package gc;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import kn.uni.voronoitreemap.j2d.Point2D;

import java.util.ArrayList;
import java.util.Random;

/*
Launches UI along with the Genetic Algorithm
*/

public class Main extends Application {
public class Main {

static Pane pane;
private Point2D[] circles;
private Random rand = new Random(seed);

private static Circle geneticCircle;
private static Text genText;

static int geneLength, largestRadius;
private static long seed = 2;
static long seed;
static int geneLength;

public static void main(String[] args) {
String generalInfo = "Run program with no arguments to output help on usage";
if (args.length == 1 && args[0].equals("ui")) {
seed = System.currentTimeMillis();
launch();
launchUI();
} else if (args.length == 2 && args[0].equals("ui")) {
try {
seed = Integer.valueOf(args[1]);
Expand All @@ -44,7 +29,7 @@ public static void main(String[] args) {
System.out.println("Illegal argument. Please enter positive integer to be used as seed. " + generalInfo);
return;
}
launch();
launchUI();
} else if (args.length == 3 && args[0].equals("headless")) {
if ((args[1].equals("true") || args[1].equals("false")))
DataCollector.generateFile = args[1].equals("true");
Expand Down Expand Up @@ -75,54 +60,8 @@ public static void main(String[] args) {
}
}

@Override
public void start(Stage primaryStage) throws Exception {

// depending on window size, encoded chromosome length will vary due to more digits being present to represent bigger numbers
if (GlobalVars.screenHeight > GlobalVars.screenWidth) {
geneLength = Integer.toBinaryString(GlobalVars.screenHeight).length();
Chromosome.maxSize = GlobalVars.screenWidth / 2;
} else {
geneLength = Integer.toBinaryString(GlobalVars.screenWidth).length();
Chromosome.maxSize = GlobalVars.screenHeight / 2;
}

pane = new Pane();
Scene scene = new Scene(pane, GlobalVars.screenWidth, GlobalVars.screenHeight);

circles = new Point2D[rand.nextInt(85) + 15];

// see fitness class for specification on what it does, returns largest radius that can be drawn in the window
CircleData cd = new Fitness().getBiggestCircle(circles, rand, false);
largestRadius = cd.radius;
System.out.println("largest radius: " + cd.radius);

// add static circles to display
for (Point2D circle : circles)
pane.getChildren().add(new Circle(circle.getX(), circle.getY(), GlobalVars.circlesRadius));

// draw largest circle as determined by the voronoi diagram
pane.getChildren().add(new Circle(cd.coords.getX(), cd.coords.getY(), cd.radius, Paint.valueOf("green")));

// sets graphics elements
geneticCircle = new Circle(-10, -10, 1);
pane.getChildren().add(geneticCircle);

genText = new Text(10, 30, "Generation: 0");
genText.setFont(Font.font(20));
pane.getChildren().add(genText);

primaryStage.setScene(scene);
primaryStage.show();

// launches genetic algorithm thread

GeneticAlgorithm gA = new GeneticAlgorithm(circles, (int)seed, largestRadius);
Thread t = new Thread(gA);

t.setDaemon(true);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
static void launchUI() {
new Thread(() -> Application.launch(UI.class)).start();
}

// returns whether circle is out of bounds of the window
Expand Down Expand Up @@ -160,20 +99,4 @@ static Chromosome selectFittest(ArrayList<Chromosome> arr) {

return c;
}

// draws best circle from the generation and updates text
static void draw(int x, int y, int radius, int gen) {
//pane.getChildren().remove(geneticCircle);
//geneticCircle = new Circle(x, y, radius, Paint.valueOf("red"));
//pane.getChildren().add(geneticCircle);
genText.setText("Generation: " + gen);
}

// draws final circle that conforms to specifications
static void draw(Circle c, int gen) {
pane.getChildren().remove(geneticCircle);
geneticCircle = c;
pane.getChildren().add(geneticCircle);
genText.setText("Final generation: " + gen);
}
}
98 changes: 98 additions & 0 deletions gc/UI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package gc;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import kn.uni.voronoitreemap.j2d.Point2D;

import java.util.Random;

public class UI extends Application {

static Pane pane;

private Point2D[] circles;
static Circle geneticCircle;
private Random rand;

private int largestRadius, seed = (int)Main.seed;

private static Text genText;

public static void main(String[] args) {
Application.launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {

rand = new Random(seed);

// depending on window size, encoded chromosome length will vary due to more digits being present to represent bigger numbers
if (GlobalVars.screenHeight > GlobalVars.screenWidth) {
Main.geneLength = Integer.toBinaryString(GlobalVars.screenHeight).length();
Chromosome.maxSize = GlobalVars.screenWidth / 2;
} else {
Main.geneLength = Integer.toBinaryString(GlobalVars.screenWidth).length();
Chromosome.maxSize = GlobalVars.screenHeight / 2;
}

pane = new Pane();
Scene scene = new Scene(pane, GlobalVars.screenWidth, GlobalVars.screenHeight);

circles = new Point2D[rand.nextInt(85) + 15];

// see fitness class for specification on what it does, returns largest radius that can be drawn in the window
CircleData cd = new Fitness().getBiggestCircle(circles, rand, false);
largestRadius = cd.radius;
System.out.println("largest radius: " + cd.radius);

// add static circles to display
for (Point2D circle : circles)
pane.getChildren().add(new Circle(circle.getX(), circle.getY(), GlobalVars.circlesRadius));

// draw largest circle as determined by the voronoi diagram
pane.getChildren().add(new Circle(cd.coords.getX(), cd.coords.getY(), cd.radius, Paint.valueOf("green")));

// sets graphics elements
geneticCircle = new Circle(-10, -10, 1);
pane.getChildren().add(geneticCircle);

genText = new Text(10, 30, "Generation: 0");
genText.setFont(Font.font(20));
pane.getChildren().add(genText);

primaryStage.setScene(scene);
primaryStage.show();

// launches genetic algorithm thread

GeneticAlgorithm gA = new GeneticAlgorithm(circles, (int)seed, largestRadius);
Thread t = new Thread(gA);

t.setDaemon(true);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}

// draws best circle from the generation and updates text
static void draw(int x, int y, int radius, int gen) {
//pane.getChildren().remove(geneticCircle);
//geneticCircle = new Circle(x, y, radius, Paint.valueOf("red"));
//pane.getChildren().add(geneticCircle);
genText.setText("Generation: " + gen);
}

// draws final circle that conforms to specifications
static void draw(Circle c, int gen) {
pane.getChildren().remove(geneticCircle);
geneticCircle = c;
pane.getChildren().add(geneticCircle);
genText.setText("Final generation: " + gen);
}
}

0 comments on commit 3381a95

Please sign in to comment.