public void Update(IEvolutionState state, int subpop, int myindex, int thread) { // update personal best if (PersonalBestFitness == null || Fitness.BetterThan(PersonalBestFitness)) { PersonalBestFitness = (Fitness)Fitness.Clone(); PersonalBestGenome = (double[])genome.Clone(); } // initialize Neighborhood if it's not been created yet PSOBreeder psob = (PSOBreeder)state.Breeder; if (Neighborhood == null || psob.Neighborhood == PSOBreeder.C_NEIGHBORHOOD_RANDOM_EACH_TIME) { if (psob.Neighborhood == PSOBreeder.C_NEIGHBORHOOD_RANDOM ) // "random" scheme is the only thing that is available for now { Neighborhood = CreateRandomPattern(myindex, psob.IncludeSelf, state.Population.Subpops[subpop].Individuals.Count, psob.NeighborhoodSize, state, thread); } else if (psob.Neighborhood == PSOBreeder.C_NEIGHBORHOOD_TOROIDAL || psob.Neighborhood == PSOBreeder.C_NEIGHBORHOOD_RANDOM_EACH_TIME) { Neighborhood = CreateToroidalPattern(myindex, psob.IncludeSelf, state.Population.Subpops[subpop].Individuals.Count, psob.NeighborhoodSize); } else // huh? { state.Output.Fatal("internal error: invalid PSO Neighborhood style: " + psob.Neighborhood); } } // identify Neighborhood best NeighborhoodBestFitness = Fitness; // initially me NeighborhoodBestGenome = genome; for (int i = 0; i < Neighborhood.Length; i++) { int ind = Neighborhood[i]; if (state.Population.Subpops[subpop].Individuals[ind].Fitness.BetterThan(Fitness)) { NeighborhoodBestFitness = state.Population.Subpops[subpop].Individuals[ind].Fitness; NeighborhoodBestGenome = ((DoubleVectorIndividual)(state.Population.Subpops[subpop].Individuals[ind])) .genome; } } // clone Neighborhood best NeighborhoodBestFitness = (Fitness)(NeighborhoodBestFitness.Clone()); NeighborhoodBestGenome = (double[])(NeighborhoodBestGenome.Clone()); }
public override void ReadIndividual(IEvolutionState state, BinaryReader dataInput) { base.ReadIndividual(state, dataInput); // Next, read auxillary arrays. if (dataInput.ReadBoolean()) { Velocity = new double[dataInput.ReadInt32()]; for (int i = 0; i < Velocity.Length; i++) { Velocity[i] = dataInput.ReadDouble(); } } else { Velocity = new double[genome.Length]; } if (dataInput.ReadBoolean()) { Neighborhood = new int[dataInput.ReadInt32()]; for (int i = 0; i < Neighborhood.Length; i++) { Neighborhood[i] = dataInput.ReadInt32(); } } else { Neighborhood = null; } if (dataInput.ReadBoolean()) { NeighborhoodBestGenome = new double[dataInput.ReadInt32()]; for (int i = 0; i < NeighborhoodBestGenome.Length; i++) { NeighborhoodBestGenome[i] = dataInput.ReadDouble(); } } else { NeighborhoodBestGenome = null; } if (dataInput.ReadBoolean()) { NeighborhoodBestFitness = (Fitness)Fitness.Clone(); NeighborhoodBestFitness.ReadFitness(state, dataInput); } if (dataInput.ReadBoolean()) { PersonalBestGenome = new double[dataInput.ReadInt32()]; for (int i = 0; i < PersonalBestGenome.Length; i++) { PersonalBestGenome[i] = dataInput.ReadDouble(); } } else { PersonalBestGenome = null; } if (dataInput.ReadBoolean()) { PersonalBestFitness = (Fitness)Fitness.Clone(); PersonalBestFitness.ReadFitness(state, dataInput); } }
public override void WriteIndividual(IEvolutionState state, BinaryWriter dataOutput) { base.WriteIndividual(state, dataOutput); if (Velocity != null) // it's always non-null { dataOutput.Write(true); dataOutput.Write(Velocity.Length); for (int i = 0; i < Velocity.Length; i++) { dataOutput.Write(Velocity[i]); } } else { dataOutput.Write(false); // this will never happen } if (Neighborhood != null) { dataOutput.Write(true); dataOutput.Write(Neighborhood.Length); for (int i = 0; i < Neighborhood.Length; i++) { dataOutput.Write(Neighborhood[i]); } } else { dataOutput.Write(false); } if (NeighborhoodBestGenome != null) { dataOutput.Write(true); dataOutput.Write(NeighborhoodBestGenome.Length); for (int i = 0; i < NeighborhoodBestGenome.Length; i++) { dataOutput.Write(NeighborhoodBestGenome[i]); } } else { dataOutput.Write(false); } if (NeighborhoodBestFitness != null) { dataOutput.Write(true); NeighborhoodBestFitness.WriteFitness(state, dataOutput); } else { dataOutput.Write(false); } if (PersonalBestGenome != null) // it's always non-null { dataOutput.Write(true); dataOutput.Write(PersonalBestGenome.Length); for (int i = 0; i < PersonalBestGenome.Length; i++) { dataOutput.Write(PersonalBestGenome[i]); } } else { dataOutput.Write(false); } if (PersonalBestFitness != null) { dataOutput.Write(true); PersonalBestFitness.WriteFitness(state, dataOutput); } else { dataOutput.Write(false); } }
public override void ReadIndividual(IEvolutionState state, StreamReader reader) { base.ReadIndividual(state, reader); // Next, read auxillary header. DecodeReturn d = new DecodeReturn(Code.ReadStringWithPreamble(AUXILLARY_PREAMBLE, state, reader)); Code.Decode(d); if (d.Type != DecodeReturn.T_BOOLEAN) { state.Output.Fatal("Line " + d.LineNumber + " should have six boolean values but seems to have fewer."); } bool v = d.L != 0; Code.Decode(d); if (d.Type != DecodeReturn.T_BOOLEAN) { state.Output.Fatal("Line " + d.LineNumber + " should have six boolean values but seems to have fewer."); } bool n = d.L != 0; Code.Decode(d); if (d.Type != DecodeReturn.T_BOOLEAN) { state.Output.Fatal("Line " + d.LineNumber + " should have six boolean values but seems to have fewer."); } bool nb = d.L != 0; Code.Decode(d); if (d.Type != DecodeReturn.T_BOOLEAN) { state.Output.Fatal("Line " + d.LineNumber + " should have six boolean values but seems to have fewer."); } bool nbf = d.L != 0; Code.Decode(d); if (d.Type != DecodeReturn.T_BOOLEAN) { state.Output.Fatal("Line " + d.LineNumber + " should have six boolean values but seems to have fewer."); } bool pb = d.L != 0; Code.Decode(d); if (d.Type != DecodeReturn.T_BOOLEAN) { state.Output.Fatal("Line " + d.LineNumber + " should have six boolean values but seems to have fewer."); } bool pbf = d.L != 0; // Next, read auxillary arrays. if (v) { String s = reader.ReadLine(); d = new DecodeReturn(s); Code.Decode(d); if (d.Type != DecodeReturn.T_INT) { state.Output.Fatal("Velocity length missing."); } Velocity = new double[(int)d.L]; for (int i = 0; i < Velocity.Length; i++) { Code.Decode(d); if (d.Type != DecodeReturn.T_DOUBLE) { state.Output.Fatal("Velocity information not long enough"); } Velocity[i] = d.D; } } else { Velocity = new double[genome.Length]; } if (n) { String s = reader.ReadLine(); d = new DecodeReturn(s); Code.Decode(d); if (d.Type != DecodeReturn.T_INT) { state.Output.Fatal("Neighborhood length missing."); } Neighborhood = new int[(int)(d.L)]; for (int i = 0; i < Neighborhood.Length; i++) { Code.Decode(d); if (d.Type != DecodeReturn.T_INT) { state.Output.Fatal("Neighborhood information not long enough"); } Neighborhood[i] = (int)(d.L); } } else { Neighborhood = null; } if (nb) { String s = reader.ReadLine(); d = new DecodeReturn(s); Code.Decode(d); if (d.Type != DecodeReturn.T_INT) { state.Output.Fatal("Neighborhood-Best length missing."); } NeighborhoodBestGenome = new double[(int)(d.L)]; for (int i = 0; i < NeighborhoodBestGenome.Length; i++) { Code.Decode(d); if (d.Type != DecodeReturn.T_DOUBLE) { state.Output.Fatal("Neighborhood-Best genome not long enough"); } NeighborhoodBestGenome[i] = d.D; } } else { NeighborhoodBestGenome = null; } if (nbf) { // here we don't know what kind of fitness it is. So we'll do our best and guess // that it's the same fitness as our own Particle NeighborhoodBestFitness = (Fitness)(Fitness.Clone()); NeighborhoodBestFitness.ReadFitness(state, reader); } if (pb) { String s = reader.ReadLine(); d = new DecodeReturn(s); Code.Decode(d); if (d.Type != DecodeReturn.T_INT) { state.Output.Fatal("Personal-Best length missing."); } PersonalBestGenome = new double[(int)(d.L)]; for (int i = 0; i < PersonalBestGenome.Length; i++) { Code.Decode(d); if (d.Type != DecodeReturn.T_DOUBLE) { state.Output.Fatal("Personal-Best genome not long enough"); } PersonalBestGenome[i] = d.D; } } else { PersonalBestGenome = null; } if (pbf) { // here we don't know what kind of fitness it is. So we'll do our best and guess // that it's the same fitness as our own Particle PersonalBestFitness = (Fitness)Fitness.Clone(); PersonalBestFitness.ReadFitness(state, reader); } }
/// gunk for reading and writing, but trying to preserve some of the /// auxillary information StringBuilder EncodeAuxillary() { StringBuilder sb = new StringBuilder(); sb.Append(AUXILLARY_PREAMBLE); sb.Append(Code.Encode(true)); sb.Append(Code.Encode(Neighborhood != null)); sb.Append(Code.Encode(NeighborhoodBestGenome != null)); sb.Append(Code.Encode(NeighborhoodBestFitness != null)); sb.Append(Code.Encode(PersonalBestGenome != null)); sb.Append(Code.Encode(PersonalBestFitness != null)); sb.Append("\n"); // Velocity sb.Append(Code.Encode(Velocity.Length)); for (int i = 0; i < Velocity.Length; i++) { sb.Append(Code.Encode(Velocity[i])); } sb.Append("\n"); // Neighborhood if (Neighborhood != null) { sb.Append(Code.Encode(Neighborhood.Length)); for (int i = 0; i < Neighborhood.Length; i++) { sb.Append(Code.Encode(Neighborhood[i])); } sb.Append("\n"); } // Neighborhood best if (NeighborhoodBestGenome != null) { sb.Append(Code.Encode(NeighborhoodBestGenome.Length)); for (int i = 0; i < NeighborhoodBestGenome.Length; i++) { sb.Append(Code.Encode(NeighborhoodBestGenome[i])); } sb.Append("\n"); } if (NeighborhoodBestFitness != null) { sb.Append(NeighborhoodBestFitness.FitnessToString()); } // personal best if (PersonalBestGenome != null) { sb.Append(Code.Encode(PersonalBestGenome.Length)); for (int i = 0; i < PersonalBestGenome.Length; i++) { sb.Append(Code.Encode(PersonalBestGenome[i])); } sb.Append("\n"); } if (PersonalBestFitness != null) { sb.Append(PersonalBestFitness.FitnessToString()); } sb.Append("\n"); return(sb); }
public override bool Equals(object ind) { if (!base.Equals(ind)) { return(false); } Particle i = (Particle)ind; if ((Velocity == null && i.Velocity != null) || (Velocity != null && i.Velocity == null)) { return(false); } if (Velocity != null) { if (Velocity.Length != i.Velocity.Length) { return(false); } for (int j = 0; j < Velocity.Length; j++) { if (!Velocity[j].Equals(i.Velocity[j])) { return(false); // Not Equal } } } if ((Neighborhood == null && i.Neighborhood != null) || (Neighborhood != null && i.Neighborhood == null)) { return(false); } if (Neighborhood != null) { if (Neighborhood.Length != i.Neighborhood.Length) { return(false); } for (int j = 0; j < Neighborhood.Length; j++) { if (Neighborhood[j] != i.Neighborhood[j]) { return(false); } } } if ((NeighborhoodBestGenome == null && i.NeighborhoodBestGenome != null) || (NeighborhoodBestGenome != null && i.NeighborhoodBestGenome == null)) { return(false); } if (NeighborhoodBestGenome != null) { if (NeighborhoodBestGenome.Length != i.NeighborhoodBestGenome.Length) { return(false); } for (int j = 0; j < NeighborhoodBestGenome.Length; j++) { if (!NeighborhoodBestGenome[j].Equals(i.NeighborhoodBestGenome[j])) { return(false); // Not Equal } } } if ((NeighborhoodBestFitness == null && i.NeighborhoodBestFitness != null) || (NeighborhoodBestFitness != null && i.NeighborhoodBestFitness == null)) { return(false); } if (NeighborhoodBestFitness != null) { if (!NeighborhoodBestFitness.Equals(i.NeighborhoodBestFitness)) { return(false); } } if ((PersonalBestGenome == null && i.PersonalBestGenome != null) || (PersonalBestGenome != null && i.PersonalBestGenome == null)) { return(false); } if (PersonalBestGenome != null) { if (PersonalBestGenome.Length != i.PersonalBestGenome.Length) { return(false); } for (int j = 0; j < PersonalBestGenome.Length; j++) { if (!PersonalBestGenome[j].Equals(i.PersonalBestGenome[j])) { return(false); // Not Equal } } } if ((PersonalBestFitness == null && i.PersonalBestFitness != null) || (PersonalBestFitness != null && i.PersonalBestFitness == null)) { return(false); } if (PersonalBestFitness != null) { if (!PersonalBestFitness.Equals(i.PersonalBestFitness)) { return(false); } } return(true); }