[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke and George Mason University |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | package ec.eval; |
---|
| 9 | |
---|
| 10 | import ec.*; |
---|
| 11 | import java.io.*; |
---|
| 12 | import ec.util.*; |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | * Job.java |
---|
| 16 | * |
---|
| 17 | |
---|
| 18 | This class stores information regarding a job submitted to a Slave: the individuals, |
---|
| 19 | the subpopulations in which they are stored, a scratch array for the individuals used |
---|
| 20 | internally, and various coevolutionary information (whether we should only count victories |
---|
| 21 | single-elimination-tournament style; which individuals should have their fitnesses updated). |
---|
| 22 | |
---|
| 23 | <p>Jobs are of two types: traditional evaluations (Slave.V_EVALUATESIMPLE), and coevolutionary |
---|
| 24 | evaluations (Slave.V_EVALUATEGROUPED). <i>type</i> indicates the type of job. |
---|
| 25 | For traditional evaluations, we may submit a group of individuals all at one time. |
---|
| 26 | Only the individuals and their subpopulation numbers are needed. |
---|
| 27 | Coevolutionary evaluations require the number of individuals, the subpopulations they come from, the |
---|
| 28 | pointers to the individuals, boolean flags indicating whether their fitness is to be updated or |
---|
| 29 | not, and another boolean flag indicating whether to count only victories in competitive tournament. |
---|
| 30 | |
---|
| 31 | * @author Liviu Panait |
---|
| 32 | * @version 1.0 |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | public class Job |
---|
| 36 | { |
---|
| 37 | // either Slave.V_EVALUATESIMPLE or Slave.V_EVALUATEGROUPED |
---|
| 38 | int type; |
---|
| 39 | |
---|
| 40 | boolean sent = false; |
---|
| 41 | Individual[] inds; // original individuals |
---|
| 42 | Individual[] newinds; // individuals that were returned -- may be different individuals! |
---|
| 43 | int[] subPops; |
---|
| 44 | boolean countVictoriesOnly; |
---|
| 45 | boolean[] updateFitness; |
---|
| 46 | |
---|
| 47 | void copyIndividualsForward() |
---|
| 48 | { |
---|
| 49 | if (newinds == null || newinds.length != inds.length) |
---|
| 50 | newinds = new Individual[inds.length]; |
---|
| 51 | for(int i=0; i < inds.length; i++) |
---|
| 52 | { |
---|
| 53 | newinds[i] = (Individual)(inds[i].clone()); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | // a ridiculous hack |
---|
| 58 | void copyIndividualsBack(EvolutionState state) |
---|
| 59 | { |
---|
| 60 | try |
---|
| 61 | { |
---|
| 62 | DataPipe p = new DataPipe(); |
---|
| 63 | DataInputStream in = p.input; |
---|
| 64 | DataOutputStream out = p.output; |
---|
| 65 | |
---|
| 66 | for(int i = 0; i < inds.length; i++) |
---|
| 67 | { |
---|
| 68 | p.reset(); |
---|
| 69 | newinds[i].writeIndividual(state, out); |
---|
| 70 | inds[i].readIndividual(state, in); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | newinds = null; |
---|
| 74 | } |
---|
| 75 | catch (IOException e) |
---|
| 76 | { |
---|
| 77 | e.printStackTrace(); |
---|
| 78 | state.output.fatal("Caught impossible IOException in Job.copyIndividualsBack()"); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|