Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:14

0001 // Created on: 2014-01-20
0002 // Created by: Alexaner Malyshev
0003 // Copyright (c) 2014-2015 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _math_GlobOptMin_HeaderFile
0017 #define _math_GlobOptMin_HeaderFile
0018 
0019 #include <gp_Pnt.hxx>
0020 #include <NCollection_CellFilter.hxx>
0021 #include <math_MultipleVarFunction.hxx>
0022 #include <NCollection_Sequence.hxx>
0023 
0024 //! This class represents Evtushenko's algorithm of global optimization based on non-uniform mesh.
0025 //! Article: Yu. Evtushenko. Numerical methods for finding global extreme (case of a non-uniform mesh).
0026 //! U.S.S.R. Comput. Maths. Math. Phys., Vol. 11, N 6, pp. 38-54.
0027 //!
0028 //! This method performs search on non-uniform mesh. The search space is a box in R^n space.
0029 //! The default behavior is to find all minimums in that box. Computation of maximums is not supported.
0030 //!
0031 //! The search box can be split into smaller boxes by discontinuity criteria.
0032 //! This functionality is covered by SetGlobalParams and SetLocalParams API.
0033 //!
0034 //! It is possible to set continuity of the local boxes.
0035 //! Such option can forcibly change local extrema search.
0036 //! In other words if theFunc can be casted to the function with Hessian but, continuity is set to 1
0037 //! Gradient based local optimization method will be used, not Hessian based method.
0038 //! This functionality is covered by SetContinuity and GetContinuity API.
0039 //!
0040 //! It is possible to freeze Lipschitz const to avoid internal modifications on it.
0041 //! This functionality is covered by SetLipConstState and GetLipConstState API.
0042 //!
0043 //! It is possible to perform single solution search.
0044 //! This functionality is covered by first parameter in Perform method.
0045 //!
0046 //! It is possible to set / get minimal value of the functional.
0047 //! It works well together with single solution search.
0048 //! This functionality is covered by SetFunctionalMinimalValue and GetFunctionalMinimalValue API.
0049 class math_GlobOptMin
0050 {
0051 public:
0052 
0053   //! Constructor. Perform method is not called from it.
0054   //! @param theFunc - objective functional.
0055   //! @param theLowerBorder - lower corner of the search box.
0056   //! @param theUpperBorder - upper corner of the search box.
0057   //! @param theC - Lipschitz constant.
0058   //! @param theDiscretizationTol - parameter space discretization tolerance.
0059   //! @param theSameTol - functional value space indifference tolerance.
0060   Standard_EXPORT math_GlobOptMin(math_MultipleVarFunction* theFunc,
0061                                  const math_Vector& theLowerBorder,
0062                                  const math_Vector& theUpperBorder,
0063                                  const Standard_Real theC = 9,
0064                                  const Standard_Real theDiscretizationTol = 1.0e-2,
0065                                  const Standard_Real theSameTol = 1.0e-7);
0066 
0067   //! @param theFunc - objective functional.
0068   //! @param theLowerBorder - lower corner of the search box.
0069   //! @param theUpperBorder - upper corner of the search box.
0070   //! @param theC - Lipschitz constant.
0071   //! @param theDiscretizationTol - parameter space discretization tolerance.
0072   //! @param theSameTol - functional value space indifference tolerance.
0073   Standard_EXPORT void SetGlobalParams(math_MultipleVarFunction* theFunc,
0074                                        const math_Vector& theLowerBorder,
0075                                        const math_Vector& theUpperBorder,
0076                                        const Standard_Real theC = 9,
0077                                        const Standard_Real theDiscretizationTol = 1.0e-2,
0078                                        const Standard_Real theSameTol = 1.0e-7);
0079 
0080   //! Method to reduce bounding box. Perform will use this box.
0081   //! @param theLocalA - lower corner of the local box.
0082   //! @param theLocalB - upper corner of the local box.
0083   Standard_EXPORT void SetLocalParams(const math_Vector& theLocalA,
0084                                       const math_Vector& theLocalB);
0085 
0086   //! Method to set tolerances.
0087   //! @param theDiscretizationTol - parameter space discretization tolerance.
0088   //! @param theSameTol - functional value space indifference tolerance.
0089   Standard_EXPORT void SetTol(const Standard_Real theDiscretizationTol,
0090                               const Standard_Real theSameTol);
0091 
0092   //! Method to get tolerances.
0093   //! @param theDiscretizationTol - parameter space discretization tolerance.
0094   //! @param theSameTol - functional value space indifference tolerance.
0095   Standard_EXPORT void GetTol(Standard_Real& theDiscretizationTol,
0096                               Standard_Real& theSameTol);
0097 
0098   //! @param isFindSingleSolution - defines whether to find single solution or all solutions.
0099   Standard_EXPORT void Perform(const Standard_Boolean isFindSingleSolution = Standard_False);
0100 
0101   //! Return solution theIndex, 1 <= theIndex <= NbExtrema.
0102   Standard_EXPORT void Points(const Standard_Integer theIndex, math_Vector& theSol);
0103 
0104   //! Set / Get continuity of local borders splits (0 ~ C0, 1 ~ C1, 2 ~ C2).
0105   inline void SetContinuity(const Standard_Integer theCont) { myCont = theCont; }
0106   inline Standard_Integer GetContinuity() const {return myCont; }
0107 
0108     //! Set / Get functional minimal value.
0109   inline void SetFunctionalMinimalValue(const Standard_Real theMinimalValue)
0110   { myFunctionalMinimalValue = theMinimalValue; }
0111   inline Standard_Real GetFunctionalMinimalValue() const {return myFunctionalMinimalValue;}
0112 
0113   //! Set / Get Lipchitz constant modification state. 
0114   //! True means that the constant is locked and unlocked otherwise.
0115   inline void SetLipConstState(const Standard_Boolean theFlag) {myIsConstLocked = theFlag; }
0116   inline Standard_Boolean GetLipConstState() const { return myIsConstLocked; }
0117 
0118   //! Return computation state of the algorithm.
0119   inline Standard_Boolean isDone() const {return myDone; }
0120 
0121   //! Get best functional value.
0122   inline Standard_Real GetF() const {return myF;}
0123 
0124   //! Return count of global extremas.
0125   inline Standard_Integer NbExtrema() const {return mySolCount;}
0126 
0127 private:
0128 
0129   //! Class for duplicate fast search. For internal usage only.
0130   class NCollection_CellFilter_Inspector
0131   {
0132   public:
0133 
0134     //! Points and target type
0135     typedef math_Vector Point;
0136     typedef math_Vector Target;
0137 
0138     NCollection_CellFilter_Inspector(const Standard_Integer theDim,
0139                                      const Standard_Real theTol)
0140       : myCurrent(1, theDim)
0141     {
0142       myTol = theTol * theTol;
0143       myIsFind = Standard_False;
0144       Dimension = theDim;
0145     }
0146 
0147     //! Access to coordinate.
0148     static Standard_Real Coord (int i, const Point &thePnt)
0149     {
0150       return thePnt(i + 1);
0151     }
0152 
0153     //! Auxiliary method to shift point by each coordinate on given value;
0154     //! useful for preparing a points range for Inspect with tolerance.
0155     void Shift (const Point& thePnt,
0156                 const NCollection_Array1<Standard_Real> &theTol,
0157                 Point& theLowPnt,
0158                 Point& theUppPnt) const
0159     {
0160       for(Standard_Integer anIdx = 1; anIdx <= Dimension; anIdx++)
0161       {
0162         theLowPnt(anIdx) = thePnt(anIdx) - theTol(anIdx - 1);
0163         theUppPnt(anIdx) = thePnt(anIdx) + theTol(anIdx - 1);
0164       }
0165     }
0166 
0167     void ClearFind()
0168     {
0169       myIsFind = Standard_False;
0170     }
0171 
0172     Standard_Boolean isFind()
0173     {
0174       return myIsFind;
0175     }
0176 
0177     //! Set current point to search for coincidence
0178     void SetCurrent (const math_Vector& theCurPnt)
0179     { 
0180       myCurrent = theCurPnt;
0181     }
0182 
0183     //! Implementation of inspection method
0184     NCollection_CellFilter_Action Inspect (const Target& theObject)
0185     {
0186       Standard_Real aSqDist = (myCurrent - theObject).Norm2();
0187 
0188       if(aSqDist < myTol)
0189       {
0190         myIsFind = Standard_True;
0191       }
0192 
0193       return CellFilter_Keep;
0194     }
0195 
0196   private:
0197     Standard_Real myTol;
0198     math_Vector myCurrent;
0199     Standard_Boolean myIsFind;
0200     Standard_Integer Dimension;
0201   };
0202 
0203 
0204   // Compute cell size.
0205   void initCellSize();
0206 
0207   // Compute initial solution
0208   void ComputeInitSol();
0209 
0210   math_GlobOptMin & operator = (const math_GlobOptMin & theOther);
0211 
0212   Standard_Boolean computeLocalExtremum(const math_Vector& thePnt, Standard_Real& theVal, math_Vector& theOutPnt);
0213 
0214   void computeGlobalExtremum(Standard_Integer theIndex);
0215 
0216   //! Check possibility to stop computations.
0217   //! Find single solution + in neighborhood of best possible solution.
0218   Standard_Boolean CheckFunctionalStopCriteria();
0219 
0220   //! Computes starting value / approximation:
0221   //! myF - initial best value.
0222   //! myY - initial best point.
0223   //! myC - approximation of Lipschitz constant.
0224   //! to improve convergence speed.
0225   void computeInitialValues();
0226 
0227   //! Check that myA <= thePnt <= myB
0228   Standard_Boolean isInside(const math_Vector& thePnt);
0229 
0230   //! Check presence of thePnt in GlobOpt sequence.
0231   Standard_Boolean isStored(const math_Vector &thePnt);
0232 
0233   //! Check and add candidate point into solutions.
0234   //! @param thePnt   Candidate point.
0235   //! @param theValue Function value in the candidate point.
0236   void checkAddCandidate(const math_Vector&  thePnt,
0237                          const Standard_Real theValue);
0238 
0239 
0240   // Input.
0241   math_MultipleVarFunction* myFunc;
0242   Standard_Integer myN;
0243   math_Vector myA; // Left border on current C2 interval.
0244   math_Vector myB; // Right border on current C2 interval.
0245   math_Vector myGlobA; // Global left border.
0246   math_Vector myGlobB; // Global right border.
0247   Standard_Real myTol; // Discretization tolerance, default 1.0e-2.
0248   Standard_Real mySameTol; // points with ||p1 - p2|| < mySameTol is equal,
0249                            // function values |val1 - val2| * 0.01 < mySameTol is equal,
0250                            // default value is 1.0e-7.
0251   Standard_Real myC; //Lipchitz constant, default 9
0252   Standard_Real myInitC; // Lipchitz constant initial value.
0253   Standard_Boolean myIsFindSingleSolution; // Default value is false.
0254   Standard_Real myFunctionalMinimalValue; // Default value is -Precision::Infinite
0255   Standard_Boolean myIsConstLocked;  // Is constant locked for modifications.
0256 
0257   // Output.
0258   Standard_Boolean myDone;
0259   NCollection_Sequence<Standard_Real> myY;// Current solutions.
0260   Standard_Integer mySolCount; // Count of solutions.
0261 
0262   // Algorithm data.
0263   Standard_Real myZ;
0264   Standard_Real myE1; // Border coefficient.
0265   Standard_Real myE2; // Minimum step size.
0266   Standard_Real myE3; // Local extrema starting parameter.
0267 
0268   math_Vector myX; // Current modified solution.
0269   math_Vector myTmp; // Current modified solution.
0270   math_Vector myV; // Steps array.
0271   math_Vector myMaxV; // Max Steps array.
0272   Standard_Real myLastStep; // Last step.
0273 
0274   NCollection_Array1<Standard_Real> myCellSize;
0275   Standard_Integer myMinCellFilterSol;
0276   Standard_Boolean isFirstCellFilterInvoke;
0277   NCollection_CellFilter<NCollection_CellFilter_Inspector> myFilter;
0278 
0279   // Continuity of local borders.
0280   Standard_Integer myCont;
0281 
0282   Standard_Real myF; // Current value of Global optimum.
0283 };
0284 
0285 #endif