[Quantproject-developers] QuantProject/b1_ADT/Optimizing/BruteForce BestParametersManager.cs, NONE,
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-03-26 00:34:06
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/BruteForce In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv3387/b1_ADT/Optimizing/BruteForce Added Files: BestParametersManager.cs Log Message: Keeps and manages the array of BruteForceOptimizableParameters that have the highest fitness --- NEW FILE: BestParametersManager.cs --- /* QuantProject - Quantitative Finance Library BestParametersManager.cs Copyright (C) 2006 Glauco Siliprandi This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using QuantProject.ADT.Optimizing.Fitness; namespace QuantProject.ADT.Optimizing.BruteForce { /// <summary> /// Keeps and manages the array of BruteForceOptimizableParameters that /// have the highest fitness /// </summary> public class BestParametersManager { private int numberOfTopBestParametersToBeReturned; private int numberOfNonNullItemsInTopBestParameters; private int indexForTheCurrentCandidateToBeSwappedOut; private BruteForceOptimizableParameters[] topBestParameters; public BruteForceOptimizableParameters[] TopBestParameters { get { this.sortTopBestParameters(); return this.topBestParameters; } } /// <summary> /// Keeps and manages the array of BruteForceOptimizableParameters that /// have the highest fitness /// </summary> /// <param name="numberOfTopBestParametersToBeReturned">number of /// 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 ) { bool isBetter = true; BruteForceOptimizableParameters currentCandidateToBeSwappedOut = this.topBestParameters[ this.indexForTheCurrentCandidateToBeSwappedOut ]; if ( currentCandidateToBeSwappedOut != null ) // the initialization phase has been complete: the first // this.numberOfTopBestParametersToBeKept items has been added to // this.topBestParameters isBetter = ( bruteForceOptimizableParameters.Fitness > currentCandidateToBeSwappedOut.Fitness ); return isBetter; } // true iif we are still in the initialization phase, when the // first this.numberOfTopBestParametersToBeReturned items have already // been added to this.topBestParameters private bool isStillTheInitializationPhase() { bool isStillInitialization = ( this.numberOfNonNullItemsInTopBestParameters < this.numberOfTopBestParametersToBeReturned ); return isStillInitialization; } private void update_numberOfNonNullItemsInTopBestParameters() { if ( this.isStillTheInitializationPhase() ) this.numberOfNonNullItemsInTopBestParameters++; } #region updateIndexForTheCurrentCandidateToBeSwappedOut private void updateIndexForTheCurrentCandidateToBeSwappedOut_ifTheCase( int indexForCurrentItemInTopBestParameters ) { BruteForceOptimizableParameters currentItemInTopBestParametes = this.topBestParameters[ indexForCurrentItemInTopBestParameters ]; if ( !this.isBetterThanTheCurrentCandidateToBeSwappedOut( currentItemInTopBestParametes ) ) // the current candidate to be swapped out is better (or equally good) // than the current item in this.topBestParameters // the current item becomes the candidate to be swapped out this.indexForTheCurrentCandidateToBeSwappedOut = indexForCurrentItemInTopBestParameters; } private int getIndexForTheWorstAmongTheBestParameters() { int indexForTheWorstAmongTheBestParameters = 0; for ( int indexForTheCurrentItemInTopBestParameters = 1 ; indexForTheCurrentItemInTopBestParameters < this.topBestParameters.Length ; indexForTheCurrentItemInTopBestParameters++ ) { BruteForceOptimizableParameters currentWorstAmongTheBestParameters = this.topBestParameters[ indexForTheWorstAmongTheBestParameters ]; BruteForceOptimizableParameters currentItemInTopBestParametes = this.topBestParameters[ indexForTheCurrentItemInTopBestParameters ]; if ( currentItemInTopBestParametes.Fitness < currentWorstAmongTheBestParameters.Fitness ) indexForTheWorstAmongTheBestParameters = indexForTheCurrentItemInTopBestParameters; } return indexForTheWorstAmongTheBestParameters; } private void updateIndexForTheCurrentCandidateToBeSwappedOut_afterTheInitializationPhase() { this.indexForTheCurrentCandidateToBeSwappedOut = this.getIndexForTheWorstAmongTheBestParameters(); } private void updateIndexForTheCurrentCandidateToBeSwappedOut() { // this.indexForTheCurrentCandidateToBeSwappedOut = 0; // int indexForCurrentItemInTopBestParameters = 1; // int indexForTheFirstNullItemInTopBestParameters = -1; if ( this.isStillTheInitializationPhase() ) this.indexForTheCurrentCandidateToBeSwappedOut = this.numberOfNonNullItemsInTopBestParameters; else // the initialization phase is complete: the first // this.numberOfTopBestParametersToBeReturned items have already // been added to this.topBestParameters this.updateIndexForTheCurrentCandidateToBeSwappedOut_afterTheInitializationPhase(); } #endregion updateIndexForTheCurrentCandidateToBeSwappedOut /// <summary> /// replaces the current candidate to be swapped out with the given /// bruteForceOptimizableParameters /// </summary> /// <param name="bruteForceOptimizableParameters"></param> private void replaceTheCurrentCandidateToBeSwappedOut( BruteForceOptimizableParameters bruteForceOptimizableParameters ) { this.topBestParameters[ this.indexForTheCurrentCandidateToBeSwappedOut ] = bruteForceOptimizableParameters; // this.topBestParameters[ 0 ] = // bruteForceOptimizableParameters; this.update_numberOfNonNullItemsInTopBestParameters(); this.updateIndexForTheCurrentCandidateToBeSwappedOut(); } /// <summary> /// Checks if bruteForceOptimizableParameters needs to be added to /// this.topBestParameters /// If it has to be added, then this.topBestParameters is managed /// accordingly /// </summary> /// <param name="bruteForceOptimizableParameters"></param> public void Analize( BruteForceOptimizableParameters 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 } } |