Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration information to the Opt tutorial #52

Open
FedorSmirnov89 opened this issue Nov 20, 2018 · 11 comments
Open

Add configuration information to the Opt tutorial #52

FedorSmirnov89 opened this issue Nov 20, 2018 · 11 comments

Comments

@FedorSmirnov89
Copy link
Member

Although the information about the configuration is already both on the website and the tutorial document, I think we could make it more prominent (my students also tend to miss it). We could add a short step-by-step instruction (with screenshots and an explanation of the different parts of the Opt4J Gui) how a minimal exploration can be started to the beginning of the tutorial.

@alxhoff
Copy link

alxhoff commented Nov 21, 2018

Would it also be possible to do a quick "how to use as stand alone library"? For myself I am an embedded systems guy and have zero experience with setting up Java projects (thanks to Android Studio). A problem another of my colleagues is also having as we are both very keen to use the framework but don't know where to start when it comes to building our own project.

No doubt a trivial problem for an experienced java dev -_-

@FedorSmirnov89
Copy link
Member Author

Hi Alex,

yes, I think it is a good idea to add this to the tutorial/documentation as well.

Just as quick answer for you:

To be sure that I understand you correctly: you want to integrate Opt4J as a dependency for a Java project, without the need to check out all the code in the repo.

I think the most comfortable way to do this is a combination of gradle and jitpack. Jitpack allows you to use GitHub repos as dependencies. What you have to do is:

  1. Set up your Java project
  2. There, you have to create a build.gradle file that is used to get the project dependencies (very comfortable and easy to use, you can find a tutorial on gradle, e.g., here)
  3. The part of the gradle file defining the dependency to Opt4J will look something like this (sorry for the ugly appearance, not sure how to display it better):

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'jacoco'

mainClassName = 'org.opt4j.core.start.Opt4J'

group 'group'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

// necessary to use jitpack
repositories {
maven {
name "jitpack"
url 'https://jitpack.io' }

mavenCentral()

}

dependencies {
implementation 'com.github.felixreimann:opt4j:v3.2'
}

You can use the website of JitPack to generate the dependency for a certain branch/commit/version.

With a correct build file, gradle build eclipse will download the Opt4J libs and configure the project dependencies (not sure what you have to do for other editors, but it is probably similar). Hope that helps, just write us a message if you run into problems.

P.S.: If you are interested in optimizing embedded systems, you may also want to checkout OpenDSE :)

@alxhoff
Copy link

alxhoff commented Nov 21, 2018

Hi @FedorSmirnov89

thanks for the quick reply. I like what you're saying as I am currently reading trough eclipse tutorials on buildship as well as the build.gradle file from the git repo. I will definable look into JitPack and see if I can get it working. I would also be happy to document this process (should i be successful) for this project.

Also funny you should mention OpenDSE, I was reading papers and went through their documentation yesterday. Very cool but I think for my specific application (DVFS optimization for linux kernel states) Opt4j fits a little better.

I shall post back on my progress, tomorrow this will be my only focus ;) thanks again for the help

@alxhoff
Copy link

alxhoff commented Nov 26, 2018

Hi @FedorSmirnov89

I just wanted to check this is correct, then I will write up a quick how-to for the HelloWorld tutorial outlined here as well as the test_config.xml.

package org.opt4j.tutorial.helloworld;

import org.opt4j.optimizers.ea.EvolutionaryAlgorithmModule ;
import org.opt4j.viewer.Viewer;
import org.opt4j.viewer.ViewerModule;
import org.opt4j.core.start.Opt4JTask;
import org.opt4j.benchmarks.zdt.ZDTModule;


public class OptimizationProblem {
	
	public static void main(String[] args) {
		
		ZDTModule zdt = new ZDTModule();
		zdt.setFunction(ZDTModule.Function.ZDT1);
		zdt.setEncoding(ZDTModule.Encoding.BINARY);
		zdt.setBits(30);
		zdt.setN(30);
		
		EvolutionaryAlgorithmModule ea = new EvolutionaryAlgorithmModule();
		ea.setGenerations(1000);
		ea.setAlpha(100);
		ea.setMu(25);
		ea.setLambda(25);
		ea.setCrossoverRate(0.95);

		ViewerModule viewer = new ViewerModule();
		viewer.setTitle("Opt4j Hello World Tutorial");
		viewer.setCloseOnStop(false);
		viewer.setCloseEvent(Viewer.CloseEvent.STOP);
		
		Opt4JTask task = new Opt4JTask(false);
		task.init(ea,zdt,viewer);
		
		try {
			task.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			task.close();
		}
		
		HelloWorldModule hw = new HelloWorldModule();
		
		Opt4JTask HWtask = new Opt4JTask(false);
		HWtask.init(ea,hw,viewer);

		
		try {
			HWtask.execute();
		} catch (Exception e) {
		    e.printStackTrace();
		} finally {
			HWtask.close();
		}
	}

}

selection_004

Cheers

@alxhoff
Copy link

alxhoff commented Nov 26, 2018

oh and my build.gradle

/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.3/userguide/java_library_plugin.html
 */

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'jacoco'
apply plugin: 'java-library'

mainClassName = 'org.opt4j.core.start.Opt4J'

group 'group'
version '1.0-SNAPSHOT'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
   	mavenCentral()
   
    // necessary to use jitpack
	maven {
	name "jitpack"
	url 'https://jitpack.io' 
	}
}


jar {
    baseName = 'optigame-gradle'
    version =  '0.1.0'
}


sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'
    
    implementation 'com.github.felixreimann:opt4j:v3.2'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
    
    compile "joda-time:joda-time:2.2"
    testCompile "junit:junit:4.12"
}

@FedorSmirnov89
Copy link
Member Author

Hi Alex,

it is great that you want to contribute to the tutorial, thank you 😃 ! I have a paper deadline tomorrow, but I will have a careful look at the code + config first thing at Wednesday.

Best regards,

Fedor

@alxhoff
Copy link

alxhoff commented Nov 26, 2018 via email

@FedorSmirnov89
Copy link
Member Author

FedorSmirnov89 commented Nov 28, 2018

Hi Alex,

just a quick question: what would you like to achieve inside the Hello World that you are describing? The current code will start up the configurator with the modules that you configured in the main().

When using the configurator, there is also a different workflow (this is the one that I, myself, prefer). If you have a project that uses Opt4J as dependency, you can just right-click it in the project viewer of Eclipse and go to Run -> Run as Java application. Eclipse will then ask you what class it should use. You can then select Opt4J and hit the OK button.
This will launch the Opt4J configurator, where you can create your optimization configuration by either adding the individual modules (by double-clicking on them on the left side of the configurator) or by loading a complete configuration.

The tutorial part where a complete optimization is started directly from Java code is, at least from my perspective, intended for the situation where you want to run an optimization within your program and directly get the optimization results, without going to the configuration GUI.

@alxhoff
Copy link

alxhoff commented Nov 28, 2018

Hi Fedor,

I hope your paper deadline was met. I simply want to run the hello world code from the tutorial (The creator, decoder, evaluator and module) in a stand along java project such that every time I do not need to go into the gui and select all the various things. Essentially a script style program. What would be the easiest way to achieve this when implementing my own creator, decoder and evaluator?

Thanks for your continued help.

@FedorSmirnov89
Copy link
Member Author

Yeah, the deadline worked out, thank you :)

Just to mention it:

In the code example you give above, you are actually not running the HelloWorld from the tutorial. Instead, you configure the ZDT benchmark as the used problem model.

Concerning the easiest way for an own creator, decoder and evaluator:

The answer depends on your workflow.
If you are working from within Eclipse, you just need to write a ProblemModule in your project (which has opt4j as configured dependency). If the name of a class ends with "Module" and it extends the module class, it will appear in the opt4j gui. Selecting it will then provide the binding that you configure inside the config() method of the class. For example, the HelloWorldModule from the very first part of the tutorial binds the HelloWorldEvaluator, the HelloWorldDecoder, and the HelloWorldCreator into the problem (so by including the HelloWorldModule in your configuration, you make sure that the HelloWorldEvaluator is used as Evaluator and so on).
You can then save a configuration that contains all the modules specifying your opritmization problem, an optimizer (e.g., an EA) and (optionally) a viewer module.
After that you can create a run configuration where your project is executed with Opt4J as the main class and the config file as an input parameter.
With that, you would have a 1-Click solution from within Eclipse.

If you want sth that is more script-like, you can define a gradle task that will create a fatJar containing your Java project (with your evaluator, creator and other classes) as well as its dependencies (in particular Opt4J).
The so obtained .jar file can then be executed with the config file as input parameter to open the configurator with the configuration specified by the file.

At least for the start, I would recommend to work from within Eclipse. Following steps should help you to get a feeling for the framework and its classes.

  1. Set up a Java Project with Opt4J as dependency
  2. Copy the classes of the HelloWorld project
  3. Get them to run (here, you could also try to create the run configuration)
  4. Change the code of the evaluator, creator, etc. and look how it effects the optimization
  5. Create own classes for the evaluator etc. along with a module that performs the binding

Feel free to write if you run into problems.

@alxhoff
Copy link

alxhoff commented Nov 29, 2018

Ahhh yes, I actually implemented both Hello World then out of curiosity I implemented the ZDT benchmark as well. I will have a play around with your suggestions, they make sense and seem like a less-fuss approach. Thanks for the advice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants