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