[Quantproject-developers] QuantProject/b1_ADT/Optimizing/BruteForce BestParametersManager.cs, 1.1,
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-03-30 15:33:48
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/BruteForce In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv968/b1_ADT/Optimizing/BruteForce Modified Files: BestParametersManager.cs Log Message: - a IBruteForceOptimizableParametersManager is now given to the constructor, to avoid adding equivalent parameters to the TopBestParameters list - the public method SortTopBestParametersDescending() has been added (the name is self explaining) Index: BestParametersManager.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/BruteForce/BestParametersManager.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** BestParametersManager.cs 26 Mar 2008 00:32:59 -0000 1.1 --- BestParametersManager.cs 30 Mar 2008 15:33:39 -0000 1.2 *************** *** 34,37 **** --- 34,39 ---- { private int numberOfTopBestParametersToBeReturned; + private IBruteForceOptimizableParametersManager + bruteForceOptimizableParametersManager; private int numberOfNonNullItemsInTopBestParameters; *************** *** 44,48 **** get { - this.sortTopBestParameters(); return this.topBestParameters; } --- 46,49 ---- *************** *** 56,83 **** /// BruteForceOptimizableParameters that are going to be returned, as /// the ones with the highest fitness</param> ! public BestParametersManager( int numberOfTopBestParametersToBeReturned ) { this.numberOfTopBestParametersToBeReturned = numberOfTopBestParametersToBeReturned; this.topBestParameters = new BruteForceOptimizableParameters[ numberOfTopBestParametersToBeReturned ]; - this.numberOfNonNullItemsInTopBestParameters = 0; this.indexForTheCurrentCandidateToBeSwappedOut = 0; } - private void sortTopBestParameters() - { - // comment out the following three lines if you have an exception - // in the Sort method and you want to break to the proper line - // double fitness; - // for ( int i = 0 ; i < this.topBestParameters.Length ; i++ ) - // fitness = ((BruteForceOptimizableParameters) this.topBestParameters[ i ]).Fitness; - - FitnessComparer fitnessComparer = new FitnessComparer(); - Array.Sort( this.topBestParameters , fitnessComparer ); - } - #region Analize private bool isBetterThanTheCurrentCandidateToBeSwappedOut( BruteForceOptimizableParameters bruteForceOptimizableParameters ) --- 57,79 ---- /// BruteForceOptimizableParameters that are going to be returned, as /// the ones with the highest fitness</param> ! public BestParametersManager( ! int numberOfTopBestParametersToBeReturned , ! IBruteForceOptimizableParametersManager ! bruteForceOptimizableParametersManager ) { this.numberOfTopBestParametersToBeReturned = numberOfTopBestParametersToBeReturned; + this.bruteForceOptimizableParametersManager = + bruteForceOptimizableParametersManager; + this.topBestParameters = new BruteForceOptimizableParameters[ numberOfTopBestParametersToBeReturned ]; this.numberOfNonNullItemsInTopBestParameters = 0; this.indexForTheCurrentCandidateToBeSwappedOut = 0; } #region Analize + + #region hasToBeInsertedIntoTheTopBestParameters private bool isBetterThanTheCurrentCandidateToBeSwappedOut( BruteForceOptimizableParameters bruteForceOptimizableParameters ) *************** *** 94,97 **** --- 90,119 ---- return isBetter; } + private bool isEquivalentAlreadyInTopBestParameters( + BruteForceOptimizableParameters bruteForceOptimizableParameters ) + { + int indexForCurrentTopBestParameters = 0; + while ( + indexForCurrentTopBestParameters < this.numberOfNonNullItemsInTopBestParameters && + !this.bruteForceOptimizableParametersManager.AreEquivalentAsTopBestParameters( + bruteForceOptimizableParameters.Meaning , + this.topBestParameters[ indexForCurrentTopBestParameters ].Meaning ) ) + indexForCurrentTopBestParameters++; + bool isEquivalentAlreadyPresent = + ( indexForCurrentTopBestParameters < this.numberOfNonNullItemsInTopBestParameters ); + return isEquivalentAlreadyPresent; + } + private bool hasToBeInsertedIntoTheTopBestParameters( + BruteForceOptimizableParameters bruteForceOptimizableParameters ) + { + bool hasToBeInserted = + ( this.isBetterThanTheCurrentCandidateToBeSwappedOut( + bruteForceOptimizableParameters ) && + !this.isEquivalentAlreadyInTopBestParameters( + bruteForceOptimizableParameters ) ); + return hasToBeInserted; + } + #endregion hasToBeInsertedIntoTheTopBestParameters + // true iif we are still in the initialization phase, when the // first this.numberOfTopBestParametersToBeReturned items have already *************** *** 189,210 **** bruteForceOptimizableParameters ) { ! if ( this.isBetterThanTheCurrentCandidateToBeSwappedOut( ! bruteForceOptimizableParameters ) ) this.replaceTheCurrentCandidateToBeSwappedOut( bruteForceOptimizableParameters ); - // if ( this.numberOfMeaningfulItemsInTheArray < this.numberOfTopBestParametersToBeKept ) - // // this.topBestParameters still contains less than - // // this.numberOfTopBestParametersToBeReturned items. It means that we are still in - // // the initializing phase, when the first this.numberOfTopBestParametersToBeReturned - // // items are to be added to the topBestParameters - // this.addWithoutSwappingOut( bruteForceOptimizableParameters ); - // else - // // the initializing phase is complete, that is the first - // // this.numberOfTopBestParametersToBeReturned has already been added to - // // this.topBestParameters - // if ( this.lowestFitness() < bruteForceOptimizableParameters.Fitness ) - // this.addSwappingOutTheLastOne( bruteForceOptimizableParameters ); } #endregion Handle } } --- 211,236 ---- bruteForceOptimizableParameters ) { ! if ( this.hasToBeInsertedIntoTheTopBestParameters( bruteForceOptimizableParameters ) ) this.replaceTheCurrentCandidateToBeSwappedOut( bruteForceOptimizableParameters ); } #endregion Handle + + /// <summary> + /// Sortes the top best parameters descending. To be invoked when + /// all BruteForceOptimizableParameters have been analyzed + /// </summary> + public void SortTopBestParametersDescending() + { + // comment out the following three lines if you have an exception + // in the Sort method and you want to break to the proper line + // double fitness; + // for ( int i = 0 ; i < this.topBestParameters.Length ; i++ ) + // fitness = ((BruteForceOptimizableParameters) this.topBestParameters[ i ]).Fitness; + + FitnessComparer fitnessComparer = new FitnessComparer(); + Array.Sort( this.topBestParameters , fitnessComparer ); + Array.Reverse( this.topBestParameters ); + } } } |