Reading configuration from file !!

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading configuration from file !!

Jarek Luks
Hi,
I have been recently writing a small application using JGAP. It has GUI
using SWING and I've encountered a problem with writing and reading my
configuration to/from a file. I want to write to a file configuration
attributes such as population size, natural selectors etc. from a GUI,
and later read them into GUI from a file again. The problem is when I
write configuration to file and try to read them into GUI something goes
wrong!! Below I enclose my functions for writing and reading
configuration to/from a file. Could someone help me out by explaining
where I'd made a mistake or show an example showing how to use
configuration for managing files (reading or writing). I use JGAP in
version 3.3.3.

public void WriteConfigToFile() {
        int returnVal = fc.showSaveDialog(null);
       
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            String filename = fc.getSelectedFile().getPath();
            try {
                FileOutputStream fos = new FileOutputStream(filename);
                ObjectOutputStream out = new ObjectOutputStream(fos);
                out.writeObject(conf);
                //out.writeUTF(conf.toString());
                out.flush();
                out.close();
            } catch(IOException e) {
                System.err.println(e);     }}}

public void ReadConfigFromFile() {
        int returnVal = fc.showOpenDialog(null);
       
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            String filename = fc.getSelectedFile().getPath();
           
            try {
                FileInputStream fis = new FileInputStream(filename);
                ObjectInputStream in = new ObjectInputStream(fis);
                conf = (Configuration)in.readObject();
                //conf = new Configuration(filename, false);
            } catch(Exception e) {
                System.err.println(e);   }

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: Reading configuration from file !!

Klaus Meffert-5
Jarek,

Could you kindly provide me with the exception (stack trace) you get?

Besides, you could use the following (e.g. see
org.jgap.util.PersistableObject):
-
    import com.thoughworks.xstream.*;
    XStream xstream = new XStream();
    FileOutputStream fos = new FileOutputStream(m_file);//m_file is a File
object
    FileWriter fw = new FileWriter(m_file);
    CompactWriter compact = new CompactWriter(fw);
    xstream.marshal(m_object, compact);//m_object is the object to persist,
e.g. the JGAP configuration object
    fos.close();
-

For loading, see method load() in the mentioned class (use Xstream instead
of JGAPXStream in this case).

Best

Klaus
www.klaus-meffert.com

 

> -----Original Message-----
> From: Jarek Luks [mailto:[hidden email]]
> Sent: Monday, December 08, 2008 7:06 PM
> To: [hidden email]
> Subject: [jgap-users] Reading configuration from file !!
>
> Hi,
> I have been recently writing a small application using JGAP.
> It has GUI using SWING and I've encountered a problem with
> writing and reading my configuration to/from a file. I want
> to write to a file configuration attributes such as
> population size, natural selectors etc. from a GUI, and later
> read them into GUI from a file again. The problem is when I
> write configuration to file and try to read them into GUI
> something goes wrong!! Below I enclose my functions for
> writing and reading configuration to/from a file. Could
> someone help me out by explaining where I'd made a mistake or
> show an example showing how to use configuration for managing
> files (reading or writing). I use JGAP in version 3.3.3.
>
> public void WriteConfigToFile() {
>         int returnVal = fc.showSaveDialog(null);
>        
>         if(returnVal == JFileChooser.APPROVE_OPTION) {
>             String filename = fc.getSelectedFile().getPath();
>             try {
>                 FileOutputStream fos = new FileOutputStream(filename);
>                 ObjectOutputStream out = new ObjectOutputStream(fos);
>                 out.writeObject(conf);
>                 //out.writeUTF(conf.toString());
>                 out.flush();
>                 out.close();
>             } catch(IOException e) {
>                 System.err.println(e);     }}}
>
> public void ReadConfigFromFile() {
>         int returnVal = fc.showOpenDialog(null);
>        
>         if(returnVal == JFileChooser.APPROVE_OPTION) {
>             String filename = fc.getSelectedFile().getPath();
>            
>             try {
>                 FileInputStream fis = new FileInputStream(filename);
>                 ObjectInputStream in = new ObjectInputStream(fis);
>                 conf = (Configuration)in.readObject();
>                 //conf = new Configuration(filename, false);
>             } catch(Exception e) {
>                 System.err.println(e);   }
>
> --------------------------------------------------------------
> ----------------
> SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las
> Vegas, Nevada.
> The future of the web can't happen without you.  Join us at
> MIX09 to help pave the way to the Next Web now. Learn more
> and register at
> http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009
.visitmix.com/
> _______________________________________________
> jgap-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jgap-users


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: Reading configuration from file !!

Klaus Meffert-5
Hello Rachid,
 
you can use any custom method you like. It is the following steps:
1) Read your custom file in a proper data structure
2) Create instances of Genes (and Chromosomes out of this genes)
For the chromosomes you could use the following constructor:
  public Chromosome(final Configuration a_configuration, Gene[] a_initialGenes)
      throws InvalidConfigurationException {
3) Put all chromosome objects into a Population object
4) create a Genotype object from this Population object
 
Sorry, that my answer is a little blurry, but it is an open field and you are free to do whatever you like here. JGAP allows to instantiate populations (and chromosomes) on your own.
 
Best
 


From: rachid el amrani [mailto:[hidden email]]
Sent: Thursday, September 3, 2015 10:30 PM
To: [hidden email]
Subject: Re: [jgap-users] Reading configuration from file !!

Hello Klaus, Please are there any methods to import my initial population from a file (big numbers of lines for each gene ), or it's the same solution above? Thanks

View this message in context: Re: Reading configuration from file !!
Sent from the jgap-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------

_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: Reading configuration from file !!

rachid el amrani
This post has NOT been accepted by the mailing list yet.
well received!
thank you Klaus