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 14:08] – Denis Pallez | tuto_ecj_android [2014/03/03 16:27] (current) – Denis Pallez | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | + | ||
- | - [[# | + | |
- | - [[# | + | |
- | - [[# | + | |
- | - [[# | + | |
- | - [[# | + | |
- | - [[# | + | |
- | - [[# | + | |
===== 1. Installation of Eclispe and Android SDK ===== | ===== 1. Installation of Eclispe and Android SDK ===== | ||
First of all, to make the development of Android app easier, you have to install the Eclipse framework. In this environment, | 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 ===== | ===== 2. Create a new Android Project ===== | ||
Line 19: | Line 8: | ||
When the two first steps are done, we will create a new Android project with eclipse. < | 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 :</ | - In your workspace, your new project should be as shown below :</ | ||
- | |||
- | [[# | ||
===== 3. Add ECJ library in your project ===== | ===== 3. Add ECJ library in your project ===== | ||
Line 29: | Line 16: | ||
< | < | ||
- Libraries tab -> add external JARs... and select " | - Libraries tab -> add external JARs... and select " | ||
- | - Go on the Order and Export tab -> select " | + | - Go on the Order and Export tab -> select " |
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 : | 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 : | ||
< | < | ||
- | - Right click on src -> new -> package\\ - Name it " | + | - Right click on src -> new -> package |
+ | - Name it " | ||
< | < | ||
- Right click on this new package -> new -> File | - Right click on this new package -> new -> File | ||
- Name the new file " | - Name the new file " | ||
- | |||
- | [[# | ||
===== 4. Collect configuration data ===== | ===== 4. Collect configuration data ===== | ||
- | Now, to get the all data in " | + | Now, to get the all data in " |
+ | <code java> | ||
+ | InputStream configStream = null; | ||
+ | try { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | catch (IOException e) { | ||
+ | | ||
+ | } | ||
+ | </ | ||
- | If your configuration file is named " | + | If your configuration file is named " |
+ | It only remains to transfer the InputStream " | ||
+ | <code java> | ||
+ | ParameterDatabase paramDB = null; | ||
+ | try { | ||
+ | | ||
+ | } | ||
+ | catch (IOException e) { | ||
+ | | ||
+ | } | ||
+ | </code> | ||
- | [[# | + | Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) |
+ | <code java> | ||
+ | EvolutionState evaluatedState = Evolve.initialize(paramDB, | ||
+ | evaluatedState.startFresh(); | ||
+ | int result = EvolutionState.R_NOTDONE; | ||
+ | while( result == EvolutionState.R_NOTDONE ) | ||
+ | result = evaluatedState.evolve(); | ||
+ | </ | ||
===== 5. Print configuration data ===== | ===== 5. Print configuration data ===== | ||
- | If you want to be convinced that your configurations are in the InputStream, | + | If you want to be convinced that your configurations are in the InputStream, |
- | + | <code java> | |
- | [[# | + | String out = ""; |
+ | try{ | ||
+ | | ||
+ | | ||
+ | | ||
+ | while ( (ligne=br.readLine()) != null) { | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | catch (Exception e) { | ||
+ | | ||
+ | } | ||
+ | System.out.println(" | ||
+ | </ | ||
+ | \\ {{ECJ_Android_fichiers: | ||
===== 6. Write in a File with Android ===== | ===== 6. Write in a File with Android ===== | ||
- | 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.\\ As in the previous example you'll need a package named " | + | 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. |
+ | As in the previous example you'll need a package named " | ||
+ | After, you'll need the context of you application : <code java> | ||
+ | Then, you have to load your file on your smartphone : <code java> | ||
+ | Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : <code java> | ||
+ | FileWriter fileWriter = null; | ||
+ | try { | ||
+ | fileWriter = new FileWriter(file); | ||
+ | } | ||
+ | catch (IOException e1) { | ||
+ | e1.printStackTrace(); | ||
+ | } | ||
+ | BufferedWriter output = new BufferedWriter(fileWriter); | ||
+ | try { | ||
+ | output.write(" | ||
+ | } | ||
+ | output.flush(); | ||
+ | catch (Exception e) { | ||
+ | Toast.makeText(mContext, | ||
+ | } | ||
+ | finally { | ||
+ | try { | ||
+ | output.close(); | ||
+ | fileWriter.close(); | ||
+ | } | ||
+ | catch (IOException e) { | ||
+ | Toast.makeText(mContext, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | As you can understand, this code write the text "Hello World" into the file " | ||
===== 7. Use a file in an another one ===== | ===== 7. Use a file in an another one ===== | ||
Line 63: | Line 122: | ||
Now, you can use this file as in the previous [[# | Now, you can use this file as in the previous [[# | ||
- | But, as we said before, ECJ may need a file. Or more precisely, the path of this file.\\ For a population initialization, | + | But, as we said before, ECJ may need a file. Or more precisely, the path of this file.\\ For a population initialization, |
- | + | < | |
- | [[# | + | pop.file = / |
+ | </ | ||
+ | Now, if you load " | ||
Any questions or suggestions ? [[guegan@polytech.unice.fr; | Any questions or suggestions ? [[guegan@polytech.unice.fr; | ||
+ | |||
+ | < | ||
+ |
tuto_ecj_android.1393855699.txt.gz · Last modified: 2014/03/03 14:08 by Denis Pallez