[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | package ec.util; |
---|
| 9 | import java.util.zip.*; |
---|
| 10 | import ec.EvolutionState; |
---|
| 11 | import java.io.*; |
---|
| 12 | |
---|
| 13 | /* |
---|
| 14 | * Checkpoint.java |
---|
| 15 | * |
---|
| 16 | * Created: Tue Aug 10 22:39:19 1999 |
---|
| 17 | * By: Sean Luke |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * Checkpoints ec.EvolutionState objects out to checkpoint files, or |
---|
| 22 | * restores the same from checkpoint files. Checkpoint take the following |
---|
| 23 | * form: |
---|
| 24 | * |
---|
| 25 | * <p><i>checkpointPrefix</i><tt>.</tt><i>generation</i><tt>.gz</tt> |
---|
| 26 | * |
---|
| 27 | * <p>...where <i>checkpointPrefix</i> is the checkpoing prefix given |
---|
| 28 | * in ec.EvolutionState, and <i>generation</i> is the current generation number |
---|
| 29 | * also given in ec.EvolutionState. |
---|
| 30 | * The ".gz" is added because the file is GZIPped to save space. |
---|
| 31 | * |
---|
| 32 | * @author Sean Luke |
---|
| 33 | * @version 1.0 |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | public class Checkpoint |
---|
| 37 | { |
---|
| 38 | |
---|
| 39 | /** Writes the evolution state out to a file. */ |
---|
| 40 | |
---|
| 41 | public static void setCheckpoint(EvolutionState state) |
---|
| 42 | { |
---|
| 43 | try |
---|
| 44 | { |
---|
| 45 | ObjectOutputStream s = |
---|
| 46 | new ObjectOutputStream( |
---|
| 47 | new GZIPOutputStream ( |
---|
| 48 | new BufferedOutputStream( |
---|
| 49 | new FileOutputStream ("" + state.checkpointPrefix + |
---|
| 50 | "." + state.generation + ".gz")))); |
---|
| 51 | s.writeObject(state); |
---|
| 52 | s.close(); |
---|
| 53 | state.output.message("Wrote out checkpoint file " + |
---|
| 54 | state.checkpointPrefix + "." + |
---|
| 55 | state.generation + ".gz"); |
---|
| 56 | } |
---|
| 57 | catch (IOException e) |
---|
| 58 | { |
---|
| 59 | state.output.warning("Unable to create the checkpoint file " + |
---|
| 60 | state.checkpointPrefix + "." + |
---|
| 61 | state.generation + ".gz" + |
---|
| 62 | "because of an IOException:\n--EXCEPTION--\n" + |
---|
| 63 | e + |
---|
| 64 | "\n--EXCEPTION-END--\n"); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | /** Returns an EvolutionState object read from a checkpoint file |
---|
| 70 | whose filename is <i>checkpoint</i> |
---|
| 71 | * |
---|
| 72 | @exception java.lang.ClassNotFoundException thrown when the checkpoint file contains a class reference which doesn't exist in your class hierarchy. |
---|
| 73 | **/ |
---|
| 74 | public static EvolutionState restoreFromCheckpoint(String checkpoint) |
---|
| 75 | throws IOException, ClassNotFoundException, OptionalDataException |
---|
| 76 | /* must throw something if error -- NEVER return null */ |
---|
| 77 | { |
---|
| 78 | // load from the file |
---|
| 79 | ObjectInputStream s = |
---|
| 80 | new ObjectInputStream( |
---|
| 81 | new GZIPInputStream ( |
---|
| 82 | new BufferedInputStream ( |
---|
| 83 | new FileInputStream (checkpoint)))); |
---|
| 84 | |
---|
| 85 | EvolutionState e = (EvolutionState) s.readObject(); |
---|
| 86 | s.close(); |
---|
| 87 | |
---|
| 88 | // restart from the checkpoint |
---|
| 89 | |
---|
| 90 | e.resetFromCheckpoint(); |
---|
| 91 | return e; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|