[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination GenomeRepre
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2006-08-03 21:26:13
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv18567/b7_Scripts/WalkForwardTesting/LinearCombination Modified Files: GenomeRepresentation.cs Log Message: Implemented ISerializable interface for managing serialization / deserialization Index: GenomeRepresentation.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/GenomeRepresentation.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** GenomeRepresentation.cs 2 Jul 2006 19:22:12 -0000 1.5 --- GenomeRepresentation.cs 3 Aug 2006 21:26:10 -0000 1.6 *************** *** 22,25 **** --- 22,27 ---- using System; + using System.Reflection; + using System.Runtime.Serialization; using QuantProject.ADT; *************** *** 34,45 **** /// </summary> [Serializable] ! public class GenomeRepresentation { private double fitness; private string signedTickers; private int generationCounter; private DateTime firstOptimizationDate; private DateTime lastOptimizationDate; ! public string SignedTickers { --- 36,49 ---- /// </summary> [Serializable] ! public class GenomeRepresentation : ISerializable { private double fitness; private string signedTickers; + private string weights; private int generationCounter; private DateTime firstOptimizationDate; private DateTime lastOptimizationDate; ! private int eligibleTickers = -1; ! public string SignedTickers { *************** *** 49,52 **** --- 53,72 ---- } } + + public int EligibleTickers + { + get + { + return this.eligibleTickers; + } + } + + public string WeightsForSignedTickers + { + get + { + return this.weights; + } + } public double Fitness { *************** *** 75,78 **** --- 95,110 ---- // } + public static double[] GetWeightsArray( string weightsWithSeparator ) + { + double[] returnValue; + string[] returnValueString = weightsWithSeparator.Split( ";".ToCharArray()); + returnValue = new double[returnValueString.Length]; + for(int i = 0; i<returnValue.Length; i++) + { + returnValue[i] = Convert.ToDouble(returnValueString[i]); + } + return returnValue; + } + public static string[] GetSignedTickers( string signedTickersWithWeights ) { *************** *** 91,95 **** return returnValue; } ! public static double[] GetWeightsForSignedTickers( string signedTickersWithWeights ) { --- 123,127 ---- return returnValue; } ! public static double[] GetWeightsForSignedTickers( string signedTickersWithWeights ) { *************** *** 142,172 **** return signedTickers; } ! private string getSignedTickersWithWeights( Genome genome ) { ! string signedTickersWithWeights = ""; ! for ( int i = 0; i<((GenomeMeaning)genome.Meaning).Tickers.Length; i++ ) { ! signedTickersWithWeights += ! ((GenomeMeaning)genome.Meaning).Tickers[i] + ! ConstantsProvider.SeparatorForWeights + ! ((GenomeMeaning)genome.Meaning).TickersPortfolioWeights[i] + ! ConstantsProvider.SeparatorForTickers; } ! signedTickersWithWeights = signedTickersWithWeights.Substring( 0 , ! signedTickersWithWeights.Length - 1 ); ! ! return signedTickersWithWeights; } private void genomeRepresentation( Genome genome , DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter ) { this.fitness = genome.Fitness; ! this.signedTickers = this.getSignedTickersWithWeights( genome ); ! //this.signedTickers = this.getSignedTickers( genome ); ! this.firstOptimizationDate = firstOptimizationDate; this.lastOptimizationDate = lastOptimizationDate; this.generationCounter = generationCounter; } public GenomeRepresentation( Genome genome , --- 174,264 ---- return signedTickers; } ! ! private string getWeights( Genome genome ) ! { ! string weights = ""; ! foreach ( double weight in ((GenomeMeaning)genome.Meaning).TickersPortfolioWeights ) ! weights += weight.ToString() + ";"; ! weights = weights.Substring( 0, weights.Length - 1 ); ! return weights; ! } ! ! // private string getSignedTickersWithWeights( Genome genome ) ! // { ! // string signedTickersWithWeights = ""; ! // for ( int i = 0; i<((GenomeMeaning)genome.Meaning).Tickers.Length; i++ ) ! // { ! // signedTickersWithWeights += ! // ((GenomeMeaning)genome.Meaning).Tickers[i] + ! // ConstantsProvider.SeparatorForWeights + ! // ((GenomeMeaning)genome.Meaning).TickersPortfolioWeights[i] + ! // ConstantsProvider.SeparatorForTickers; ! // } ! // signedTickersWithWeights = signedTickersWithWeights.Substring( 0 , ! // signedTickersWithWeights.Length - 1 ); ! // ! // return signedTickersWithWeights; ! // } ! ! private void genomeRepresentation_synchronizeOldWithNew() ! { ! if(this.weights == null) ! //for old genomes saved to disk not having "weights" field ! { ! foreach(double weight in GenomeRepresentation.GetWeightsForSignedTickers(this.signedTickers)) ! this.weights += weight.ToString() + ";"; ! this.weights = this.weights.Substring( 0, this.weights.Length - 1 ); ! // ! string newRepresentationForSignedTickers = null; ! foreach(string ticker in GenomeRepresentation.GetSignedTickers(this.signedTickers)) ! newRepresentationForSignedTickers += ticker + ";"; ! this.signedTickers = newRepresentationForSignedTickers.Substring(0, newRepresentationForSignedTickers.Length -1); ! } ! ! } ! ! /// <summary> ! /// This constructor allows custom deserialization (see the ISerializable ! /// interface documentation) ! /// </summary> ! /// <param name="info"></param> ! /// <param name="context"></param> ! protected GenomeRepresentation( SerializationInfo info , StreamingContext context ) { ! // get the set of serializable members for this class and its base classes ! Type thisType = this.GetType(); ! MemberInfo[] mi = FormatterServices.GetSerializableMembers( ! thisType , context); ! ! // deserialize the fields from the info object ! for (Int32 i = 0 ; i < mi.Length; i++) { ! FieldInfo fieldInfo = (FieldInfo) mi[i]; ! ! // set the field to the deserialized value ! try{ ! fieldInfo.SetValue( this , ! info.GetValue( fieldInfo.Name, fieldInfo.FieldType ) ); ! } ! catch(Exception ex) ! {ex = ex;} } ! ! this.genomeRepresentation_synchronizeOldWithNew(); ! } private void genomeRepresentation( Genome genome , DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers ) { this.fitness = genome.Fitness; ! //this.signedTickers = this.getSignedTickersWithWeights( genome ); ! this.signedTickers = this.getSignedTickers( genome ); ! this.weights = this.getWeights( genome ); ! this.firstOptimizationDate = firstOptimizationDate; this.lastOptimizationDate = lastOptimizationDate; this.generationCounter = generationCounter; + this.eligibleTickers = eligibleTickers; } public GenomeRepresentation( Genome genome , *************** *** 174,178 **** { this.genomeRepresentation( genome , ! firstOptimizationDate , lastOptimizationDate , -1 ); } public GenomeRepresentation( Genome genome , --- 266,270 ---- { this.genomeRepresentation( genome , ! firstOptimizationDate , lastOptimizationDate , -1, -1 ); } public GenomeRepresentation( Genome genome , *************** *** 181,186 **** { this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter ); } } } --- 273,308 ---- { this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, -1 ); } + + public GenomeRepresentation( Genome genome , + DateTime firstOptimizationDate , DateTime lastOptimizationDate , + int generationCounter, int eligibleTickers ) + { + this.genomeRepresentation( genome , firstOptimizationDate , + lastOptimizationDate , generationCounter, eligibleTickers ); + } + + #region GetObjectData + /// <summary> + /// serialize the set of serializable members for this class and base classes + /// </summary> + /// <param name="info"></param> + /// <param name="context"></param> + void ISerializable.GetObjectData( + SerializationInfo info, StreamingContext context) + { + // get the set of serializable members for this class and base classes + Type thisType = this.GetType(); + MemberInfo[] mi = + FormatterServices.GetSerializableMembers( thisType , context); + + // serialize the fields to the info object + for (Int32 i = 0 ; i < mi.Length; i++) + { + info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this)); + } + } + #endregion } } |