Skip to content

Commit

Permalink
release 3.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Debabrata Acharya authored and Debabrata Acharya committed Jun 7, 2020
1 parent 197e627 commit 8f2f25b
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<groupId>com.debacharya</groupId>
<artifactId>nsgaii</artifactId>
<version>3.0.4</version>
<version>3.0.10</version>
<packaging>jar</packaging>

<inceptionYear>2019</inceptionYear>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/debacharya/nsgaii/NSGA2.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public Population run() {

Reporter.terminate(child, this.configuration.objectives);

if(Reporter.autoTerminate)
Reporter.commitToDisk();

return child;
}

Expand Down
42 changes: 31 additions & 11 deletions src/main/java/com/debacharya/nsgaii/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,45 @@
import com.debacharya.nsgaii.objectivefunction.AbstractObjectiveFunction;
import com.debacharya.nsgaii.plugin.GraphPlot;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class Reporter {

private static final int fileHash = ThreadLocalRandom.current().nextInt(10000, 100000);
private static final StringBuilder writeContent = new StringBuilder();

private static int fileHash = ThreadLocalRandom.current().nextInt(10000, 100000);
private static StringBuilder writeContent = new StringBuilder();
private static GraphPlot allGenerationGraph;

public static boolean silent = false;
public static boolean autoTerminate = true;
public static boolean plotGraph = true;
public static boolean plotCompiledGraphForEveryGeneration = true;
public static boolean plotGraphForEveryGeneration = false;
public static boolean writeToDisk = true;
public static boolean diskWriteSuccessful = true;
public static String outputDirectory = "";
public static String filename = "NSGA-II-report-" + Reporter.fileHash + ".txt";

public static void init(Configuration configuration) {

if(silent && !writeToDisk) return;
if(!Reporter.outputDirectory.isEmpty() && !Reporter
.outputDirectory
.substring(Reporter.outputDirectory.length() - 1)
.equals(File.separator))
Reporter.outputDirectory += File.separator;

p("\n[ " + java.time.LocalDateTime.now() + " ]");
p("\n** To stop reporter from printing to console, change Reporter.silent to true or call Configuration.beSilent()");
p(
"** Reporter is" +
(Reporter.writeToDisk ? "" : " not") +
" writing to disk" +
(Reporter.writeToDisk ? (" at " + Reporter.filename) : "") +
(Reporter.writeToDisk ? (" at " + Reporter.outputDirectory + Reporter.filename) : "") +
"."
);

Expand Down Expand Up @@ -168,17 +177,33 @@ public static void terminate(Population finalChild, List<AbstractObjectiveFuncti

p("------------------------------------------------");
p("NSGA-II ENDED SUCCESSFULLY\n");
}

public static void commitToDisk() {
if(writeToDisk) {
Reporter.writeToFile();
if(diskWriteSuccessful) p("** Output saved at " + filename + "\n");
if(diskWriteSuccessful) p("** Output saved at " + outputDirectory + filename + "\n");
}
}

public static void flush() {
Reporter.writeContent = new StringBuilder();
Reporter.allGenerationGraph = null;
Reporter.fileHash = ThreadLocalRandom.current().nextInt(10000, 100000);
Reporter.filename = "NSGA-II-report-" + Reporter.fileHash + ".txt";
}

public static void p(Object s) {
if(writeToDisk) Reporter.writeContent.append(s).append(System.lineSeparator());
if(!silent) System.out.println(s.toString());
}

private static void writeToFile() {

try {
FileWriter writer = new FileWriter(Reporter.filename);

Files.createDirectories(Paths.get(Reporter.outputDirectory));
FileWriter writer = new FileWriter(Reporter.outputDirectory + Reporter.filename);

writer.write(Reporter.writeContent.toString());
writer.close();
Expand All @@ -187,9 +212,4 @@ private static void writeToFile() {
System.out.println("\n!!! COULD NOT WRITE FILE TO DISK!\n\n");
}
}

private static void p(String s) {
if(writeToDisk) Reporter.writeContent.append(s).append(System.lineSeparator());
if(!silent) System.out.println(s);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.debacharya.nsgaii.objectivefunction;

import com.debacharya.nsgaii.datastructure.BooleanAllele;
import com.debacharya.nsgaii.datastructure.Chromosome;
import com.debacharya.nsgaii.datastructure.ValueAllele;
import com.debacharya.nsgaii.plugin.SinglePointMutation;

import java.util.stream.Collector;
import java.util.stream.Collectors;

public class ZDT1_1 extends AbstractObjectiveFunction {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.debacharya.nsgaii.objectivefunction;

import com.debacharya.nsgaii.datastructure.BooleanAllele;
import com.debacharya.nsgaii.datastructure.Chromosome;
import com.debacharya.nsgaii.datastructure.ValueAllele;

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/debacharya/nsgaii/plugin/GraphPlot.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.debacharya.nsgaii.plugin;

import com.debacharya.nsgaii.Configuration;
import com.debacharya.nsgaii.datastructure.Population;
import com.debacharya.nsgaii.objectivefunction.AbstractObjectiveFunction;
import org.jfree.chart.ChartFactory;
Expand All @@ -15,6 +14,9 @@
import org.jfree.ui.RefineryUtilities;

import java.awt.*;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/*
* MIT License
*
Expand All @@ -39,9 +41,6 @@
* SOFTWARE.
*/

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class GraphPlot extends ApplicationFrame {

public static String APPLICATION_TITLE = "NSGA-II";
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/FileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import com.debacharya.nsgaii.Reporter;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileTest {

public static void main(String[] args) throws IOException {

Files.createDirectories(Paths.get("abc/xyz/"));
Files.createDirectories(Paths.get(""));

try {
FileWriter writer = new FileWriter("abc/xyz/test.txt");

writer.write("test");
writer.close();
} catch (Exception e) {
System.out.println("\n!!! COULD NOT WRITE FILE TO DISK!\n\n");
}
}
}
16 changes: 16 additions & 0 deletions src/test/java/GeneticCodeProducerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import com.debacharya.nsgaii.Reporter;
import com.debacharya.nsgaii.datastructure.AbstractAllele;
import com.debacharya.nsgaii.plugin.DefaultPluginProvider;

import java.util.List;
import java.util.stream.Collectors;

public class GeneticCodeProducerTest {

public static void main(String[] args) {

List<? extends AbstractAllele> geneticCode = DefaultPluginProvider.defaultGeneticCodeProducer().produce(7000);

Reporter.reportGeneticCode(geneticCode.stream().map(e -> (AbstractAllele) e).collect(Collectors.toList()));
}
}
5 changes: 4 additions & 1 deletion src/test/java/NSGA2Test.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import com.debacharya.nsgaii.Configuration;
import com.debacharya.nsgaii.NSGA2;
import com.debacharya.nsgaii.Reporter;
import com.debacharya.nsgaii.datastructure.Population;

public class NSGA2Test {

public static void main(String[] args) {

NSGA2 nsga2 = new NSGA2();
Configuration configuration = new Configuration();
NSGA2 nsga2 = new NSGA2(configuration);
Population paretoFront = nsga2.run();
}
}
4 changes: 2 additions & 2 deletions src/test/java/ZDT1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static void main(String[] args) {

configuration.objectives = objectives;

configuration.setPopulationSize(100);
configuration.setGenerations(500);
configuration.setPopulationSize(200);
configuration.setGenerations(100);
configuration.setChromosomeLength(30);
configuration.setGeneticCodeProducer(DefaultPluginProvider.valueEncodedGeneticCodeProducer(0, 1));
configuration.setMutation(new PolynomialMutation(0, 1));
Expand Down
1 change: 0 additions & 1 deletion v1/src/main/java/io/onclave/nsga/ii/api/GraphPlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

import java.awt.*;
import java.util.Random;

/**
Expand Down
1 change: 0 additions & 1 deletion v2/src/main/java/io/onclave/nsga/ii/api/GraphPlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

import java.awt.*;
import java.util.Random;

/**
Expand Down

0 comments on commit 8f2f25b

Please sign in to comment.