Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-08 09:16:49

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
0026 //! mesh). 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
0030 //! supported.
0031 //!
0032 //! The search box can be split into smaller boxes by discontinuity criteria.
0033 //! This functionality is covered by SetGlobalParams and SetLocalParams API.
0034 //!
0035 //! It is possible to set continuity of the local boxes.
0036 //! Such option can forcibly change local extrema search.
0037 //! In other words if theFunc can be casted to the function with Hessian but, continuity is set to 1
0038 //! Gradient based local optimization method will be used, not Hessian based method.
0039 //! This functionality is covered by SetContinuity and GetContinuity API.
0040 //!
0041 //! It is possible to freeze Lipschitz const to avoid internal modifications on it.
0042 //! This functionality is covered by SetLipConstState and GetLipConstState API.
0043 //!
0044 //! It is possible to perform single solution search.
0045 //! This functionality is covered by first parameter in Perform method.
0046 //!
0047 //! It is possible to set / get minimal value of the functional.
0048 //! It works well together with single solution search.
0049 //! This functionality is covered by SetFunctionalMinimalValue and GetFunctionalMinimalValue API.
0050 class math_GlobOptMin
0051 {
0052 public:
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, const math_Vector& theLocalB);
0084 
0085   //! Method to set tolerances.
0086   //! @param theDiscretizationTol - parameter space discretization tolerance.
0087   //! @param theSameTol - functional value space indifference tolerance.
0088   Standard_EXPORT void SetTol(const Standard_Real theDiscretizationTol,
0089                               const Standard_Real theSameTol);
0090 
0091   //! Method to get tolerances.
0092   //! @param theDiscretizationTol - parameter space discretization tolerance.
0093   //! @param theSameTol - functional value space indifference tolerance.
0094   Standard_EXPORT void GetTol(Standard_Real& theDiscretizationTol, Standard_Real& theSameTol);
0095 
0096   //! @param isFindSingleSolution - defines whether to find single solution or all solutions.
0097   Standard_EXPORT void Perform(const Standard_Boolean isFindSingleSolution = Standard_False);
0098 
0099   //! Return solution theIndex, 1 <= theIndex <= NbExtrema.
0100   Standard_EXPORT void Points(const Standard_Integer theIndex, math_Vector& theSol);
0101 
0102   //! Set / Get continuity of local borders splits (0 ~ C0, 1 ~ C1, 2 ~ C2).
0103   inline void SetContinuity(const Standard_Integer theCont) { myCont = theCont; }
0104 
0105   inline Standard_Integer GetContinuity() const { return myCont; }
0106 
0107   //! Set / Get functional minimal value.
0108   inline void SetFunctionalMinimalValue(const Standard_Real theMinimalValue)
0109   {
0110     myFunctionalMinimalValue = theMinimalValue;
0111   }
0112 
0113   inline Standard_Real GetFunctionalMinimalValue() const { return myFunctionalMinimalValue; }
0114 
0115   //! Set / Get Lipchitz constant modification state.
0116   //! True means that the constant is locked and unlocked otherwise.
0117   inline void SetLipConstState(const Standard_Boolean theFlag) { myIsConstLocked = theFlag; }
0118 
0119   inline Standard_Boolean GetLipConstState() const { return myIsConstLocked; }
0120 
0121   //! Return computation state of the algorithm.
0122   inline Standard_Boolean isDone() const { return myDone; }
0123 
0124   //! Get best functional value.
0125   inline Standard_Real GetF() const { return myF; }
0126 
0127   //! Return count of global extremas.
0128   inline Standard_Integer NbExtrema() const { return mySolCount; }
0129 
0130 private:
0131   //! Class for duplicate fast search. For internal usage only.
0132   class NCollection_CellFilter_Inspector
0133   {
0134   public:
0135     //! Points and target type
0136     typedef math_Vector Point;
0137     typedef math_Vector Target;
0138 
0139     NCollection_CellFilter_Inspector(const Standard_Integer theDim, 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) { return thePnt(i + 1); }
0149 
0150     //! Auxiliary method to shift point by each coordinate on given value;
0151     //! useful for preparing a points range for Inspect with tolerance.
0152     void Shift(const Point&                             thePnt,
0153                const NCollection_Array1<Standard_Real>& theTol,
0154                Point&                                   theLowPnt,
0155                Point&                                   theUppPnt) const
0156     {
0157       for (Standard_Integer anIdx = 1; anIdx <= Dimension; anIdx++)
0158       {
0159         theLowPnt(anIdx) = thePnt(anIdx) - theTol(anIdx - 1);
0160         theUppPnt(anIdx) = thePnt(anIdx) + theTol(anIdx - 1);
0161       }
0162     }
0163 
0164     void ClearFind() { myIsFind = Standard_False; }
0165 
0166     Standard_Boolean isFind() { return myIsFind; }
0167 
0168     //! Set current point to search for coincidence
0169     void SetCurrent(const math_Vector& theCurPnt) { myCurrent = theCurPnt; }
0170 
0171     //! Implementation of inspection method
0172     NCollection_CellFilter_Action Inspect(const Target& theObject)
0173     {
0174       Standard_Real aSqDist = (myCurrent - theObject).Norm2();
0175 
0176       if (aSqDist < myTol)
0177       {
0178         myIsFind = Standard_True;
0179       }
0180 
0181       return CellFilter_Keep;
0182     }
0183 
0184   private:
0185     Standard_Real    myTol;
0186     math_Vector      myCurrent;
0187     Standard_Boolean myIsFind;
0188     Standard_Integer Dimension;
0189   };
0190 
0191   // Compute cell size.
0192   void initCellSize();
0193 
0194   // Compute initial solution
0195   void ComputeInitSol();
0196 
0197   math_GlobOptMin& operator=(const math_GlobOptMin& theOther);
0198 
0199   Standard_Boolean computeLocalExtremum(const math_Vector& thePnt,
0200                                         Standard_Real&     theVal,
0201                                         math_Vector&       theOutPnt);
0202 
0203   void computeGlobalExtremum(Standard_Integer theIndex);
0204 
0205   //! Check possibility to stop computations.
0206   //! Find single solution + in neighborhood of best possible solution.
0207   Standard_Boolean CheckFunctionalStopCriteria();
0208 
0209   //! Computes starting value / approximation:
0210   //! myF - initial best value.
0211   //! myY - initial best point.
0212   //! myC - approximation of Lipschitz constant.
0213   //! to improve convergence speed.
0214   void computeInitialValues();
0215 
0216   //! Check that myA <= thePnt <= myB
0217   Standard_Boolean isInside(const math_Vector& thePnt);
0218 
0219   //! Check presence of thePnt in GlobOpt sequence.
0220   Standard_Boolean isStored(const math_Vector& thePnt);
0221 
0222   //! Check and add candidate point into solutions.
0223   //! @param thePnt   Candidate point.
0224   //! @param theValue Function value in the candidate point.
0225   void checkAddCandidate(const math_Vector& thePnt, const Standard_Real theValue);
0226 
0227   // Input.
0228   math_MultipleVarFunction* myFunc;
0229   Standard_Integer          myN;
0230   math_Vector               myA;       // Left border on current C2 interval.
0231   math_Vector               myB;       // Right border on current C2 interval.
0232   math_Vector               myGlobA;   // Global left border.
0233   math_Vector               myGlobB;   // Global right border.
0234   Standard_Real             myTol;     // Discretization tolerance, default 1.0e-2.
0235   Standard_Real             mySameTol; // points with ||p1 - p2|| < mySameTol is equal,
0236                                        // function values |val1 - val2| * 0.01 < mySameTol is equal,
0237                                        // default value is 1.0e-7.
0238   Standard_Real    myC;                // Lipchitz constant, default 9
0239   Standard_Real    myInitC;            // Lipchitz constant initial value.
0240   Standard_Boolean myIsFindSingleSolution;   // Default value is false.
0241   Standard_Real    myFunctionalMinimalValue; // Default value is -Precision::Infinite
0242   Standard_Boolean myIsConstLocked;          // Is constant locked for modifications.
0243 
0244   // Output.
0245   Standard_Boolean                    myDone;
0246   NCollection_Sequence<Standard_Real> myY;        // Current solutions.
0247   Standard_Integer                    mySolCount; // Count of solutions.
0248 
0249   // Algorithm data.
0250   Standard_Real myZ;
0251   Standard_Real myE1{}; // Border coefficient.
0252   Standard_Real myE2{}; // Minimum step size.
0253   Standard_Real myE3{}; // Local extrema starting parameter.
0254 
0255   math_Vector   myX;          // Current modified solution.
0256   math_Vector   myTmp;        // Current modified solution.
0257   math_Vector   myV;          // Steps array.
0258   math_Vector   myMaxV;       // Max Steps array.
0259   Standard_Real myLastStep{}; // Last step.
0260 
0261   NCollection_Array1<Standard_Real>                        myCellSize;
0262   Standard_Integer                                         myMinCellFilterSol;
0263   Standard_Boolean                                         isFirstCellFilterInvoke{};
0264   NCollection_CellFilter<NCollection_CellFilter_Inspector> myFilter;
0265 
0266   // Continuity of local borders.
0267   Standard_Integer myCont;
0268 
0269   Standard_Real myF; // Current value of Global optimum.
0270 };
0271 
0272 #endif