tuto_ecj_android
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
tuto_ecj_android [2014/03/03 13:52] – Denis Pallez | tuto_ecj_android [2014/03/03 15:05] – 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, |
- | < | + | |
- | < | + | [[#begin|Back to begin]] |
- | < | + | |
- | < | + | ===== 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}}\\ |
- | </ | + | |
- | < | + | [[#begin|Back to begin]] |
- | <p> | + | |
- | First of all, to make the development of Android app easier, you have | + | ===== 3. Add ECJ library in your project |
- | 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. | + | [[#begin|Back to begin]] |
- | First of all you have to create it : | + | |
- | <pre class=" | + | ===== 4. Collect configuration data ===== |
- | - Right click on src -& | + | |
- | - Name it " | + | Now, to get the all data in " |
- | </pre>< | + | < |
- | <img src=" | + | InputStream configStream = null; |
- | <pre class=" | + | try { |
- | - Right click on this new package -& | + | Context mContext = this; |
- | - Name the new file " | + | configStream = mContext.getAssets().open(" |
- | </pre>< | + | } |
- | You can now edit your configuration file as usual.< | + | catch (IOException e) { |
- | <img src=" | + | e.printStackTrace(); |
- | </p> | + | } |
- | <div class=" | + | </code> |
- | < | + | |
- | <p> | + | If your configuration file is named " |
- | Now, to get the all data in " | + | It only remains to transfer the InputStream " |
- | Add these lines in your code : | + | < |
- | <pre class=" | + | ParameterDatabase paramDB = null; |
- | InputStream configStream = null;<br> | + | try { |
- | try {<br> | + | paramDB = new ParameterDatabase(configStream); |
- | <span class=" | + | } |
- | <span class=" | + | catch (IOException e) { |
- | } catch (IOException e) {<br> | + | e.printStackTrace(); |
- | <span class=" | + | } |
- | }<br> | + | </code> |
- | </pre> | + | |
- | </ | + | Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) |
- | If your configuration file is named " | + | < |
- | the assets package and " | + | EvolutionState evaluatedState = Evolve.initialize(paramDB, |
- | application, | + | evaluatedState.startFresh(); |
- | It only remains to transfer the InputStream " | + | int result = EvolutionState.R_NOTDONE; |
- | Here is how to initialize the ParameterDatabase : | + | while( result == EvolutionState.R_NOTDONE ) |
- | <pre class=" | + | result = evaluatedState.evolve(); |
- | ParameterDatabase paramDB = null;<br> | + | </code> |
- | try {<br> | + | |
- | <span class=" | + | [[#begin|Back to begin]] |
- | }<br> | + | |
- | catch (IOException e) {<br> | + | ===== 5. Print configuration data ===== |
- | <span class=" | + | |
- | }<br> | + | If you want to be convinced that your configurations are in the InputStream, |
- | </pre>< | + | < |
- | Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) | + | String out = ""; |
- | <pre class=" | + | try{ |
- | EvolutionState evaluatedState = Evolve.initialize(paramDB, | + | InputStreamReader configStreamReader = new InputStreamReader(configStream); |
- | evaluatedState.startFresh(); | + | BufferedReader br = new BufferedReader(configStreamReader); |
- | int result = EvolutionState.R_NOTDONE; | + | String ligne = ""; |
- | while( result == EvolutionState.R_NOTDONE )<br> | + | while ( (ligne=br.readLine()) != null) { |
- | <span class=" | + | out += ligne + " |
- | </pre>< | + | } |
- | </p> | + | br.close(); |
- | <div class=" | + | } |
- | < | + | catch (Exception e) { |
- | <p> | + | e.printStackTrace(); |
- | If you want to be convinced that your configurations are in the | + | } |
- | InputStream, | + | System.out.println(" |
- | in the InputStream on logCat console : | + | </code> |
- | <pre class=" | + | \\ {{ECJ_Android_fichiers:7.png}}\\ |
- | String out = ""; | + | |
- | try{<br> | + | [[#begin|Back to begin]] |
- | <span class=" | + | |
- | <span class=" | + | ===== 6. Write in a File with Android |
- | <span class=" | + | |
- | <span class=" | + | However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. |
- | < | + | |
- | <span class=" | + | In this part we'll see how to write a file. |
- | br.close(); | + | As in the previous example you'll need a package named " |
- | } catch (Exception e) {<br> | + | After, you'll need the context of you application : < |
- | <span class=" | + | Then, you have to load your file on your smartphone : < |
- | }<br> | + | Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : < |
- | System.out.println(" | + | FileWriter fileWriter = null; |
- | </pre>< | + | try { |
- | <img src=" | + | fileWriter = new FileWriter(file); |
- | </p> | + | } |
- | <div class=" | + | catch (IOException e1) { |
- | < | + | e1.printStackTrace(); |
- | <p> | + | } |
- | However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. | + | BufferedWriter output = new BufferedWriter(fileWriter); |
- | In this part we'll see how to write a file.<br> | + | try { |
- | As in the previous example you'll need a package named " | + | output.write(" |
- | Create a file in your workspace named " | + | } |
- | After, you'll need the context of you application : <br> | + | output.flush(); |
- | <pre class=" | + | catch (Exception e) { |
- | Context mContext = this; | + | Toast.makeText(mContext, |
- | </pre> | + | } |
- | Then, you have to load your file on your smartphone : | + | finally { |
- | <pre class=" | + | try { |
- | File file = mContext.getFileStreamPath(" | + | output.close(); |
- | </pre> | + | fileWriter.close(); |
- | Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : | + | } |
- | <pre class=" | + | |
- | FileWriter fileWriter = null;<br> | + | Toast.makeText(mContext, |
- | try {<br> | + | } |
- | <span class=" | + | } |
- | } catch (IOException e1) {<br> | + | </code> |
- | <span class=" | + | |
- | }<br> | + | As you can understand, this code write the text "Hello World" into the file " |
- | BufferedWriter output = new BufferedWriter(fileWriter); | + | |
- | | + | [[#begin|Back to begin]] |
- | <span class=" | + | |
- | }<br> | + | ===== 7. Use a file in an another one ===== |
- | | + | |
- | | + | 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, |
- | | + | < |
- | <span class=" | + | pop.file = / |
- | <span class=" | + | </code> |
- | <span class=" | + | Now, if you load " |
- | < | + | |
- | <span class=" | + | [[#begin|Back to begin]] |
- | <span class=" | + | |
- | }<br> | + | Any questions or suggestions ? [[guegan@polytech.unice.fr; |
- | </pre> | + | |
- | As you can understand, this code write the text "Hello World" into the file " | + | <note>written by Ancelin Arnaud, Aliénor Diaz, Olivier Fauvel-Jaeger, |
- | </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=" | + | |
- | <p> | + | |
- | Any questions or suggestions ? | + | |
- | <a href=" | + | |
- | </p> | + | |
- | </div> | + | |
- | | + | |
- | </ |
tuto_ecj_android.txt · Last modified: 2014/03/03 16:27 by Denis Pallez