User Tools

Site Tools


tuto_ecj_android

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
tuto_ecj_android [2014/03/03 15:08]
Denis Pallez
tuto_ecj_android [2014/03/03 16:05]
Denis Pallez
Line 1: Line 1:
-====== ​TutorialHow ​to install ECJ library on Android environment ? ====== +====== ​How to install ECJ library on Android environment ? ======
- +
-  - [[#​install|Installation of Eclispe and Android SDK]] +
-  - [[#​createANewProject|Create a new Android Project]] +
-  - [[#​addEcj|Add ECJ library in your project]] +
-  - [[#​collectInfo|Collect configuration data]] +
-  - [[#​printInfo|Print configuration data]] +
-  - [[#​writeInfo|Write in a File with Android]] +
-  - [[#​useNewConfigFile|Use a file in an another one]] +
 ===== 1. Installation of Eclispe and Android SDK ===== ===== 1. Installation of Eclispe and Android SDK =====
  
Line 29: Line 20:
 <​code>​- Right click on the project name -> build path -> configure build path... <​code>​- Right click on the project name -> build path -> configure build path...
 - Libraries tab -> add external JARs... and select "​ecj.jar"​ in the "​libs"​ folder - Libraries tab -> add external JARs... and select "​ecj.jar"​ in the "​libs"​ folder
-- Go on the Order and Export tab -> select "​ecj.jar"​ </​code>​\\ \\ {{ECJ_Android_fichiers:​3.png}}\\ {{ECJ_Android_fichiers:​4.png}}+- Go on the Order and Export tab -> select "​ecj.jar"​ </​code>​\\ \\ {{ECJ_Android_fichiers:​3.png}}\\ {{ECJ_Android_fichiers:​4.png}}\\
 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 : 
 <​code>​ <​code>​
-- Right click on src -> new -> package\\ - Name it "​assets"​ </​code>​\\ \\ {{ECJ_Android_fichiers:​5.png}}+- Right click on src -> new -> package 
 +- Name it "​assets"​ </​code>​\\ \\ {{ECJ_Android_fichiers:​5.png}}
 <​code>​ <​code>​
 - Right click on this new package -> new -> File - Right click on this new package -> new -> File
Line 41: Line 33:
 ===== 4. Collect configuration data ===== ===== 4. Collect configuration data =====
  
-Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code : InputStream configStream = null;\\ try {\\ Context mContext = this;\\  ​configStream = mContext.getAssets().open("​config_ecj.params"​);​\\  ​} catch (IOException e) {\\ e.printStackTrace();​\\  ​}\\ +Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code :  
 +<code java> 
 +InputStream configStream = null; 
 +try { 
 +  ​Context mContext = this; 
 +  ​configStream = mContext.getAssets().open("​config_ecj.params"​);​ 
 +} 
 +catch (IOException e) { 
 +  ​e.printStackTrace();​ 
 +} 
 +</​code>​
  
-If your configuration file is named "​config_ecj.params",​ located in the assets package and "​this"​ correspond to the current context of your application,​ this code shouldn'​t cause any problem.\\ It only remains to transfer the InputStream "​configStream"​ at the ParameterDatabase who's in charge of initializing ECJ. Here is how to initialize the ParameterDatabase : ParameterDatabase paramDB = null;\\ try {\\ paramDB = new ParameterDatabase(configStream);​\\  ​}\\ catch (IOException e) {\\ e.printStackTrace();​\\  ​}\\ \\ Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0);\\ evaluatedState.startFresh();​\\ int result = EvolutionState.R_NOTDONE;​\\ while( result == EvolutionState.R_NOTDONE )\\ result = evaluatedState.evolve();​\\ \\ +If your configuration file is named "​config_ecj.params",​ located in the assets package and "​this"​ correspond to the current context of your application,​ this code shouldn'​t cause any problem. 
 +It only remains to transfer the InputStream "​configStream"​ at the ParameterDatabase who's in charge of initializing ECJ. Here is how to initialize the ParameterDatabase : 
 +<code java> 
 +ParameterDatabase paramDB = null; 
 +try { 
 +  ​paramDB = new ParameterDatabase(configStream);​ 
 +} 
 +catch (IOException e) { 
 +  ​e.printStackTrace();​ 
 +} 
 +</​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,​ 0); 
 +evaluatedState.startFresh();​ 
 +int result = EvolutionState.R_NOTDONE;​ 
 +while( result == EvolutionState.R_NOTDONE ) 
 +  ​result = evaluatedState.evolve();​ 
 + </​code> ​
  
 [[#​begin|Back to begin]] [[#​begin|Back to begin]]
Line 49: Line 70:
 ===== 5. Print configuration data ===== ===== 5. Print configuration data =====
  
-If you want to be convinced that your configurations are in the InputStream,​ you can add this code who's in charge of printing the data in the InputStream on logCat console : String out = "";​\\ try{\\ InputStreamReader configStreamReader = new InputStreamReader(configStream);​\\  ​BufferedReader br = new BufferedReader(configStreamReader);​\\  ​String ligne = "";​\\  while ( (ligne=br.readLine()) != null) {\\  ​out += ligne + "​\n";​\\  }\\  ​br.close();​\\ } catch (Exception e) {\\ e.printStackTrace();​\\  ​}\\ System.out.println("​Out => " + out);\\ \\ {{ECJ_Android_fichiers:​7.png}}\\ ​+If you want to be convinced that your configurations are in the InputStream,​ you can add this code who's in charge of printing the data in the InputStream on logCat console :  
 +<code java> 
 +String out = "";​ 
 +try{ 
 +  ​InputStreamReader configStreamReader = new InputStreamReader(configStream);​ 
 +  ​BufferedReader br = new BufferedReader(configStreamReader);​ 
 +  ​String ligne = "";​ 
 +  while ( (ligne=br.readLine()) != null) { 
 +    ​out += ligne + "​\n";​ 
 +  } 
 +  ​br.close();​ 
 + 
 +catch (Exception e) { 
 +  ​e.printStackTrace();​ 
 +} 
 +System.out.println("​Out => " + out); 
 +</​code>​ 
 +\\ {{ECJ_Android_fichiers:​7.png}}\\ ​
  
 [[#​begin|Back to begin]] [[#​begin|Back to begin]]
Line 55: Line 93:
 ===== 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 "​assets"​.\\ Create a file in your workspace named "​config_ecj_in.in"​ (for an initialization file, by example).\\ After, you'll need the context of you application :\\ Context mContext = this; Then, you have to load your file on your smartphone : File file = mContext.getFileStreamPath("​config_ecj_in.in"​);​ Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : FileWriter fileWriter = null;\\ try {\\ fileWriter = new FileWriter(file);​\\  ​} catch (IOException e1) {\\ e1.printStackTrace();​\\  ​}\\ BufferedWriter output = new BufferedWriter(fileWriter);​\\ try {\\ output.write("​Hello World ! ");\\  ​}\\ output.flush();​\\ catch (Exception e) {\\ Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​\\  ​}\\ finally {\\ try {\\  ​output.close();​\\  ​fileWriter.close();​\\  } catch (IOException e) {\\  ​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​\\  }\\  ​}\\  ​As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.\\ ​+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 "​assets"​.\\ Create a file in your workspace named "​config_ecj_in.in"​ (for an initialization file, by example). 
 +After, you'll need the context of you application : <code java>Context mContext = this; </​code>​ 
 +Then, you have to load your file on your smartphone : <code java>File file = mContext.getFileStreamPath("​config_ecj_in.in"​); ​</​code>​ 
 +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("​Hello World ! "); 
 +} 
 +output.flush();​ 
 +catch (Exception e) { 
 +  ​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
 +} 
 +finally { 
 +  ​try { 
 +    ​output.close();​ 
 +    ​fileWriter.close();​ 
 +  } 
 +  ​catch (IOException e) { 
 +    ​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
 +  } 
 +} 
 +</​code>​ 
 + 
 +As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.\\ ​
  
 [[#​begin|Back to begin]] [[#​begin|Back to begin]]
Line 63: Line 134:
 Now, you can use this file as in the previous [[#​collectInfo|example]].\\ ​ Now, you can use this file as in the previous [[#​collectInfo|example]].\\ ​
  
-But, as we said before, ECJ may need a file. Or more precisely, the path of this file.\\ For a population initialization,​ you give the path of the initialization file to the configuration file.\\ This path is the following one:\\ /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in.\\ \\ If you want to initialize your population "​config_ecj_in.in"​ from the configuration file "​mainConfigFile.params",​ add this line in "​mainConfigFile.params":​\\ pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in\\  ​Now, if you load "​mainConfigFile.params"​ as show in this [[#​collectInfo|example]] you will also initialize you popullation with "​config_ecj_in.in"​.\\ Simple, isn't it ?\\ Here is the end of our tutorial. Good luck !\\ +But, as we said before, ECJ may need a file. Or more precisely, the path of this file.\\ For a population initialization,​ you give the path of the initialization file to the configuration file.\\ This path is the following one:\\ /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in.\\ \\ If you want to initialize your population "​config_ecj_in.in"​ from the configuration file "​mainConfigFile.params",​ add this line in "​mainConfigFile.params":​ 
 +<​code>​ 
 +pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in 
 +</​code>​ 
 +Now, if you load "​mainConfigFile.params"​ as show in this [[#​collectInfo|example]] you will also initialize you popullation with "​config_ecj_in.in"​.\\ Simple, isn't it ?\\ Here is the end of our tutorial. Good luck !\\ 
  
 [[#​begin|Back to begin]] [[#​begin|Back to begin]]
  
 Any questions or suggestions ? [[guegan@polytech.unice.fr;​fauvelja@polytech.unice.fr;​adiaz@polytech.unice.fr;​arnaud@polyech.unice.fr?​subject=Tutorial%20ECJ%20in%20Android|Contact us !]] Any questions or suggestions ? [[guegan@polytech.unice.fr;​fauvelja@polytech.unice.fr;​adiaz@polytech.unice.fr;​arnaud@polyech.unice.fr?​subject=Tutorial%20ECJ%20in%20Android|Contact us !]]
 +
 +<​note>​written by Ancelin Arnaud, Aliénor Diaz, Olivier Fauvel-Jaeger,​ Frédéric Guégan in 2013/​06</​note>​
 +
tuto_ecj_android.txt · Last modified: 2014/03/03 17:27 by Denis Pallez