tuto_ecj_android
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tuto_ecj_android [2014/03/03 13:35] – created Denis Pallez | tuto_ecj_android [2014/03/03 16:27] (current) – Denis Pallez | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | + | ====== How to install ECJ library on Android environment ? ====== |
- | < | + | ===== 1. Installation of Eclispe and Android SDK ===== |
- | <h1 id=" | + | |
- | < | + | First of all, to make the development of Android app easier, you have to install the Eclipse framework. In this environment, |
- | < | + | |
- | < | + | ===== 2. Create a new Android Project |
- | < | + | |
- | < | + | When the two first steps are done, we will create a new Android project with eclipse. < |
- | < | + | - In your workspace, your new project should be as shown below :</code>\\ \\ {{ECJ_Android_fichiers:2.png}}\\ |
- | < | + | |
- | < | + | ===== 3. Add ECJ library in your project |
- | </ | + | < |
- | < | + | - Add in the libs folder the ecj.jar file. |
- | <p> | + | </code> |
- | First of all, to make the development of Android app easier, you have | + | Even if ecj.jar is in the libs folder, it shouldn' |
- | to install the Eclipse framework. In this environment, | + | < |
- | install the Android SDK to get all the tools. | + | - Libraries tab -> add external JARs... and select " |
- | This two first steps are explained here : < | + | - Go on the Order and Export tab -> select " |
- | <a href="http:// | + | Now, the ECJ library can be used in your Android app, but this library uses some external files including a configuration file. From your Android app point of view, this file is an external file. It's not obvious to include in your app. We will see how to collect the information of this configuration file. First of all you have to create it : |
- | <a href="http:// | + | < |
- | Then, you have to download the ECJ library | + | - Right click on src -> new -> package |
- | <a href="http:// | + | - Name it " |
- | You can find the official ECJ manual | + | < |
- | <a href="http:// | + | - Right click on this new package -> new -> File |
- | </p> | + | - Name the new file " |
- | <div class=" | + | |
- | < | + | ===== 4. Collect configuration data ===== |
- | <p> | + | |
- | When the two first steps are done, we will create a new Android project with eclipse. | + | Now, to get the all data in " |
- | <span class=" | + | < |
- | - File -& | + | InputStream configStream = null; |
- | </span>< | + | try { |
- | <img src="ECJ_Android_fichiers/1.png"><br> | + | Context mContext = this; |
- | <span class=" | + | configStream = mContext.getAssets().open(" |
- | - Give a name at your project and let the default configurations.<br> | + | } |
- | - In your workspace, your new project should be as shown below : < | + | catch (IOException e) { |
- | </span>< | + | e.printStackTrace(); |
- | <img src="ECJ_Android_fichiers/2.png">< | + | } |
- | </p> | + | </code> |
- | <div class=" | + | |
- | < | + | If your configuration file is named " |
- | <p> | + | It only remains to transfer the InputStream " |
- | <span class=" | + | < |
- | - Add in the libs folder the ecj.jar file. | + | ParameterDatabase paramDB = null; |
- | </span>< | + | try { |
- | Even if ecj.jar is in the libs folder, it shouldn' | + | paramDB = new ParameterDatabase(configStream); |
- | <span class=" | + | } |
- | - Right click on the project name -& | + | catch (IOException e) { |
- | - Libraries tab -& | + | e.printStackTrace(); |
- | - Go on the Order and Export tab -& | + | } |
- | </span>< | + | </code> |
- | <img src="ECJ_Android_fichiers/3.png">< | + | |
- | <img src="ECJ_Android_fichiers/4.png">< | + | Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) |
- | Now, the ECJ library can be used in your Android app, but this library | + | < |
- | uses some external files including a configuration file. From your | + | EvolutionState evaluatedState = Evolve.initialize(paramDB, |
- | Android app point of view, this file is an external file. It's not | + | evaluatedState.startFresh(); |
- | obvious to include in your app. We will see how to collect the | + | int result = EvolutionState.R_NOTDONE; |
- | information of this configuration file. | + | while( result == EvolutionState.R_NOTDONE ) |
- | First of all you have to create it : | + | result = evaluatedState.evolve(); |
- | <span class=" | + | </code> |
- | - Right click on src -& | + | |
- | - Name it " | + | ===== 5. Print configuration data ===== |
- | </span>< | + | |
- | <img src="ECJ_Android_fichiers/5.png">< | + | If you want to be convinced that your configurations are in the InputStream, |
- | <span class=" | + | < |
- | - Right click on this new package -& | + | String out = ""; |
- | - Name the new file " | + | try{ |
- | </span>< | + | InputStreamReader configStreamReader = new InputStreamReader(configStream); |
- | You can now edit your configuration file as usual.< | + | BufferedReader br = new BufferedReader(configStreamReader); |
- | <img src="ECJ_Android_fichiers/6.png">< | + | String ligne = ""; |
- | </p> | + | while ( (ligne=br.readLine()) != null) { |
- | <div class=" | + | out += ligne + " |
- | < | + | } |
- | <p> | + | br.close(); |
- | Now, to get the all data in " | + | } |
- | Add these lines in your code : | + | catch (Exception e) { |
- | <span class=" | + | e.printStackTrace(); |
- | InputStream configStream = null;<br> | + | } |
- | try {<br> | + | System.out.println(" |
- | <span class=" | + | </code> |
- | <span class=" | + | \\ {{ECJ_Android_fichiers:7.png}}\\ |
- | } catch (IOException e) {<br> | + | |
- | <span class=" | + | ===== 6. Write in a File with Android |
- | }<br> | + | |
- | </span> | + | However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. |
- | </ | + | |
- | If your configuration file is named " | + | In this part we'll see how to write a file. |
- | the assets package and " | + | As in the previous example you'll need a package named " |
- | application, | + | After, you'll need the context of you application : < |
- | It only remains to transfer the InputStream " | + | Then, you have to load your file on your smartphone : < |
- | Here is how to initialize the ParameterDatabase : | + | Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : < |
- | <span class=" | + | FileWriter fileWriter = null; |
- | ParameterDatabase paramDB = null;<br> | + | try { |
- | try {<br> | + | fileWriter = new FileWriter(file); |
- | <span class=" | + | } |
- | }<br> | + | catch (IOException e1) { |
- | catch (IOException e) {<br> | + | e1.printStackTrace(); |
- | <span class=" | + | } |
- | }<br> | + | BufferedWriter output = new BufferedWriter(fileWriter); |
- | </span>< | + | try { |
- | Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) | + | output.write(" |
- | <span class=" | + | } |
- | EvolutionState evaluatedState = Evolve.initialize(paramDB, | + | output.flush(); |
- | evaluatedState.startFresh(); | + | catch (Exception e) { |
- | int result = EvolutionState.R_NOTDONE; | + | Toast.makeText(mContext, |
- | while( result == EvolutionState.R_NOTDONE )<br> | + | } |
- | <span class=" | + | finally { |
- | </span>< | + | try { |
- | </p> | + | output.close(); |
- | <div class=" | + | fileWriter.close(); |
- | < | + | } |
- | <p> | + | |
- | If you want to be convinced that your configurations are in the | + | Toast.makeText(mContext, |
- | InputStream, | + | } |
- | in the InputStream on logCat console : | + | } |
- | <span class=" | + | </code> |
- | String out = ""; | + | |
- | try{<br> | + | As you can understand, this code write the text "Hello World" into the file " |
- | <span class=" | + | |
- | <span class=" | + | ===== 7. Use a file in an another one ===== |
- | <span class=" | + | |
- | <span class=" | + | Now, you can use this file as in the previous |
- | < | + | |
- | <span class=" | + | But, as we said before, ECJ may need a file. Or more precisely, the path of this file.\\ For a population initialization, |
- | br.close(); | + | < |
- | } catch (Exception e) {<br> | + | pop.file = / |
- | <span class=" | + | </code> |
- | }<br> | + | Now, if you load " |
- | System.out.println(" | + | |
- | </span>< | + | Any questions or suggestions ? [[guegan@polytech.unice.fr; |
- | <img src="ECJ_Android_fichiers/7.png">< | + | |
- | </p> | + | <note>written by Ancelin Arnaud, Aliénor Diaz, Olivier Fauvel-Jaeger, |
- | <div class=" | + | |
- | < | + | |
- | <p> | + | |
- | However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. | + | |
- | In this part we'll see how to write a file.<br> | + | |
- | As in the previous example you'll need a package named " | + | |
- | Create a file in your workspace named " | + | |
- | After, you'll need the context of you application : <br> | + | |
- | <span class=" | + | |
- | Context mContext = this; | + | |
- | </span> | + | |
- | Then, you have to load your file on your smartphone : | + | |
- | <span class=" | + | |
- | File file = mContext.getFileStreamPath(" | + | |
- | </span> | + | |
- | Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : | + | |
- | <span class=" | + | |
- | FileWriter fileWriter = null;<br> | + | |
- | try {<br> | + | |
- | <span class=" | + | |
- | } catch (IOException e1) {<br> | + | |
- | <span class=" | + | |
- | }<br> | + | |
- | BufferedWriter output = new BufferedWriter(fileWriter); | + | |
- | | + | |
- | <span class=" | + | |
- | }<br> | + | |
- | | + | |
- | | + | |
- | <span class=" | + | |
- | } <br> | + | |
- | | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | < | + | |
- | <span class=" | + | |
- | <span class=" | + | |
- | }<br> | + | |
- | </span> | + | |
- | As you can understand, this code write the text "Hello World" into the file " | + | |
- | </p> | + | |
- | <div class=" | + | |
- | < | + | |
- | Now, you can use this file as in the previous | + | |
- | <p> | + | |
- | But, as we said before, ECJ may need a file. Or more precisely, the path of this file.< | + | |
- | For a population initialization, | + | |
- | This path is the following one: < | + | |
- | <span class=" | + | |
- | If you want to initialize your population " | + | |
- | configuration file " | + | |
- | " | + | |
- | <span class=" | + | |
- | pop.file = / | + | |
- | </span> | + | |
- | Now, if you load " | + | |
- | Simple, isn't it ?< | + | |
- | Here is the end of our tutorial. Good luck ! <br> | + | |
- | </p> | + | |
- | <div class=" | + | |
- | <div id=" | + | |
- | < | + | |
- | Any questions or suggestions ? | + | |
- | <a href=" | + | |
- | </p> | + | |
- | </div> | + | |
- | | + | |
- | </ |
tuto_ecj_android.1393853750.txt.gz · Last modified: 2014/03/03 13:35 by Denis Pallez