45
The program of search minimum scalar function,
dependent from several independent arguments

 

First of all, excuse me for bad English. In order to not practice in language constructions, by places, I specially has supposed inaccuracies, in the description of attributes of classes. Lets will forgive me the programmers. And nevertheless, I hope, that the main sense I has managed to transmit

Assignment:
go top

The modules, offered to your attention, are intended for a solution of a problem of definition minimum of scalar function f(x1,…,xn) several independent scalar variables, which range is n-dimensional parallelogram (xmin <= x <= xmax)

Algorithms:
go top

The brief description of components:

The module ChkRSearcher.exe - executes search minimum value of the user's function, loaded through dll (Win32 API Dynamic Link Library)
go top

rsrchr1.jpg (35032 bytes)

It allows:

Library MyFunction.dll – containing user's function for minimizing

General information:
go top

As it is visible from the description ChkRSearcher.exe, to take advantage of this module, it is necessary, that the user's function for minimizing was made as dynamic link library (Win32 API Dynamic Link Library), it is expected, that this library will contain only two functions: factor() and initargs():

Example of functions:

#ifdef PARABOLA_EXPORTS
#define PARABOLA_API __declspec(dllexport)
#else
#define PARABOLA_API __declspec(dllimport)
#endif

extern "C" PARABOLA_API double factor(double args[], int nsize)
{
   double f1, f2;
   f1 = args[0]-args[1];
   f2 = args[0]+args[1]-10.0;
   return f1*f1+f2*f2/9.0;
}

extern "C" PARABOLA_API void initargs(CRSearcher* pRSearcher)
{
   pRSearcher->AddArg(-10.0, 10.0);
   pRSearcher->AddArg(-9.0, 11.0);
   pRSearcher->m_lpszComment = _T("(x1-x2)**2+(x1+x2-10)**2/9");
}

Here lines

   pRSearcher->AddArg(-10.0, 10.0);
   pRSearcher->AddArg(-9.0, 11.0);

in function initargs() means, that the function depends on two independent arguments, and the allowable significances first are in an interval [-10.0, 10.0], the second in [-9.0, 11.0], and the average point of these intervals will be selected as an initial testing point.

The line:

   pRSearcher->m_lpszComment = _T("(x1-x2)**2+(x1+x2-10)**2/9");

defines the comment for your function, which is recorded in the file of resalts and will allow you to distinguish one functions from other.

How to create dll-library, containing the functions: factor() and initargs()
go top

For creation dll, containing you functions is necessary to do the following:

In Microsoft Visual C++ 6.0 create the new project (menu item File\New… on tab Projects enter name yours future dll (field Project name) type Win32 Dynamic-Link Library and locate it in the subdirectory (field Location) …\RandomSearcher\Functions\ (on step 1 from 1 check: A DLL that exports some symbols)

Being in the created project, select tab FileView and do the following:

In the stdafx.h file replace a line

// TODO: reference additional headers your program requires here

On

#include "..\..\RSearcher\Common.h"
#include "..\..\RSearcher\RFileLog.h"
#include "..\..\RSearcher\RArg.h"
#include "..\..\RSearcher\RPoint.h"
#include "..\..\RSearcher\RGradient.h"
#include "..\..\RSearcher\RMinOnLine.h"
#include "..\..\RSearcher\RSearcher.h"

In your MyDllName.h file delete the text after a line

// This class is exported from the MyDllName.dll

and insert there following:

extern "C" MYDLLNAME_API double factor(double args[], int nsize);
extern "C" MYDLLNAME_API void initargs(CRSearcher* pRSearcher);

here, certainly, the constant MYDLLNAME_API is necessary for replacing with your constant, determined in the beginning of your MyDllName.h file

In your MyDllName.cpp file delete the text after a line

// This is an example of an exported variable

and insert there following:

// This are an exported functions:

extern "C" MYDLLNAME_API double factor(double args[], int nsize)
{
   /*
     double f1, f2;
     f1 = args[0]-args[1];
     f2 = args[0]+args[1]-10.0;
     return f1*f1+f2*f2/9.0;
   */
}

extern "C" MYDLLNAME_API void initargs(CRSearcher* pRSearcher)
{
   /*
     pRSearcher->AddArg(-10.0, 10.0);
     pRSearcher->AddArg(-9.0, 11.0);
     pRSearcher->m_lpszComment = _T("(x1-x2)**2+(x1+x2-10)**2/9");
   */
}

here, the constant MYDLLNAME_API also is necessary for replacing with your constant, determined in the beginning of the MyDllName.h file

Further, delete the comment in body of functions factor(), initargs() and write instead of it their your definitions.

Attention: an amount of the pRSearcher->AddArg(MyMinArgValue, MyMaxArgValue); in function initargs() and it's order should correspond to an amount of arguments (array elements args[] in your function factor() and it's order in an array args[]).

By using menu item Project\Insert Project into WorkSpace... add in your project, project named RSearcher (..\..\Rsearcher\RSearcher.dsp) and further, by taking advantage the menu item Project\Dependence... is necessary set dependence of your project on the project RSearcher (mark it for your project)

Set the folder for yours DLL. For this purpose select in a tree objects your project, press the right mouse button and from the arisen menu select item Set as Active Project. Further, by using the menu item Project\Settings…, in the arisen dialogue be switched on your project, select a tab Link and for Category General in a field Output file name enter the following ..\..\Bin\MyDllName.dll, that will allow to receive created yours Dll directly in the folder …\RandomSearcher\Bin\

After removal of all syntactical errors, as I hope, you receive the dll-file containing yours function for minimizing, and capable dynamically load it from the program ChkRSearcher.exe

The library RSearcher.dll – contains the following classes:
go top

Class Assignment
CRSearcher - realize algorithms of search
CRArg - contains necessary properties and methods of argument of function
CRPoint - ensures a storage a testing point
CRGradient - allows calculate and store a vector-gradient set of testining points
CRMinOnLine - realizes search minimum of function one scalar argument
CRFileLog - ensures store information to a disk file

The class CRArg – contains argument of function
go top

Name Type Description
CRArg(double min, double max) Method Contructor
CRArg(double min, double max, double cur) Method Contructor
void ResetArg(double min, double max) Method Reset boundary values of argument
Double m_min Property Minimum value
Double m_max Property Maximum value
Double m_cur Property The Centre of a casual scatter of testing points
Double m_width Property The Range of a casual scatter of testing points
Double m_MinWidth Property The minimum significance m_width, after which is considered to a senseless scatter of testing points for argument
BOOL ResetWidth(double factor, double curvalue) Method Resets values m_width and m_cur
void ResetCurValue(double newcurvalue) Method Reset value m_cur
Double GetRandArg() Method Returns casual value of argument, using the random-number generator, m_cur and m_width

The class CRPoint – contains a testing point
go top

Name Type Description
CRPoint(double args[], int nsize, double value) Method Contructor
CRPoint(int nsize) Method Contructor
CRPoint& operator=(const CRPoint& point) Operator =
CRPoint& operator=(const CRPoint* point) Operator =
Bool operator<(const CRPoint& point) Operator <
Bool operator==(const CRPoint& point) Operator ==
Double* m_pArgs Property The reference to array of arguments for a point
Double m_value Property Value function for a point
Int m_nSize Property Number of elements in array m_pArgs

The class CRGradient – calculates a vector-gradient of function in a testing point
go top

Name Type Description
Double* m_pGradient Property The reference to an array of elements of a vector-gradient
Int m_nSize Property The number of elements in array m_pGradient
CRPoint m_MinPoint Property The testing point, relative to which is calculated a vector-gradient m_pGradient
BOOL Calculate(CRPoint* pMinPoint, vector<CRPoint*>* paPoints) Method For a set of testing points paPoints concerning a point pMinPoint calculates elements of array m_pGradient

The class CRMinOnLine – searches value of argument, for which the function accepts minimum value
go top

Name Type Description
BOOL Init(vector<CRArg*>* paArgs, CRGradient* pGradient, PFUNC pFunction) Method For set of arguments in paArgs, vector-gradient pGradient and function pFunction, prepares datas for search of minimum of function
BOOL CalcRangeArg(vector<CRArg*>* paArgs, CRGradient* pGradient) Method On a set of arguments in paArgs and for a vector-gradient pGradient finds a range(m_MinArg, m_MaxArg) variation of an explanatory variable of a direct line, such, that for any point from this range we remain in a range of definition of function m_pFunction
Double Find() Method Organize search value of argument, for which the function accepts minimum value. Returns the calculated value of function m_pFunction in the found point
PFUNC m_pFunction Property The reference on minimizing function
Double fun(double x) Method Returns the calculated value of function m_pFunction for independent argument x of a direct line (which is changed in a range [m_MinArg, m_MaxArg])
Double FindMin(double ax, double bx, double tol) Method Realizes search value of independent argument of a direct line in an interval [ax, bx], with length of an interval of indeterminacy tol, for which the function accepts minimum value. Returns value of independent argument of a direct line, for which functions m_pFunction accepts minimum value
Double m_tol Property Desirable length of an interval of indeterminacy of an eventual result for independent argument of a direct line x (>= 0.0) (typical significance 1.0e-5)
Double* m_pArgs Property The reference to an array of values of arguments, for which the function has minimum value on direct line, in the given range
Long m_nCountCallsFn Property The number of calculations minimizing function during search
Double m_MinArg Property The left boundary of an explanatory variable direct line, for which the arguments of minimizing function remain in a range of definition
Double m_MaxArg Property The right boundary of an explanatory variable direct line, for which the arguments of minimizing function remain in a range of definition
Double m_Value Property The calculated value of function pFunction in the found point
CRGradient* m_pGradient Property The reference to an instance of a class CRGradient, for which the calculations are made
Int m_nSize Property The number of elements in array m_pArgs

The class CRFileLog - writes datas in a disk file
go top

Name Type Description
CRFileLog(LPCTSTR lpszLogFile) Conctructor lpszLogFile the reference to a name of the file
BOOL Open() Method The opening file
LPCTSTR m_lpszLogFile Property The name of file
FILE* m_pStream Property The reference to the sream
BOOL PutStr(LPCTSTR lpszStr) Method Write lpszStr into file. Returns TRUE, if there are no errors.
BOOL PutVal(LPCTSTR lpszStr, double val) Method Write lpszStr and val into file. Returns TRUE, if there are no errors.
BOOL PutVal(LPCTSTR lpszStr, double aVal[], int nsize) Method Write lpszStr and array aVal of dimensionality nsize into file. Returns TRUE, if there are no errors.

The class CRSearcher – by using the above described classes, realize algorithms of search
go top

Name Type Description
Enum Scenario Property The enumeration of the realized algorithms
Void ClearAll() Method Deletes (reset to initial state) values of all properties of the class
BOOL Find(pstThreadInfo pThreadInfo) Method For datas transmitted through the reference pThreadInfo, will organize search minimum value of function. Returns TRUE, if the search was finished successfully.
BOOL InitPoints(pstThreadInfo pThreadInfo) Method For datas transmitted through the reference pThreadInfo, set to initial values for all properties of a class. Returns TRUE, if the installation is made successfully.
BOOL ResetPoints(pstThreadInfo pThreadInfo) Method For datas transmitted through the reference pThreadInfo, executes one step of minimization. Returns TRUE, if the step is executed successfully, and the process of minimization can be continued.
BOOL ChangeRangeArgs(BOOL bIsMin, BOOL bCurValOnly = FALSE) Method Change centres of random scatters of testing points for all arguments. The first parameter bIsMin signals that on the previous step the minimum was found, second bCurValOnly, - requires only displacement of centre of a scatter (without a modification of an interval of a scatter).
BOOL AddArg(double min, double max) Method Adds new argument in a vector of arguments m_aArgs. Returns TRUE, if the adding is executed successfully.
Void WriteResalt(pstThreadInfo pThreadInfo, LPCTSTR lpszFile) Method For datas transmitted through the reference pThreadInfo, write into file lpszFile the final results of minimization
PFUNC m_pFunction Property The reference on minimizing function
CRPoint m_MinPoint Property The best testing point in a sense of minimum of function m_pFunction
Double m_MinValue Property The value m_pFunction function in point m_MinPoint
Vector<CRArg*> m_aArgs Property The vector of the references on independent arguments of function m_pFunction
Vector<CRPoint*> m_aPoints Property The vector of the references on a set of testing points
CRGradient m_Gradient Property The instance of class CRGradient
CRMinOnLine m_MinOnLine Property The instance of class CRMinOnLine
Long m_nCurStep Property The current count of steps of process minimization
Long m_nMaxSteps Property The ordered amount of steps of process minimization
Long m_nMaxPoints Property The ordered amount of testing points on step
Long m_nCountCallsFn Property The count of calculations minimizing function during search
Scenario m_Scenario Property The ordered algorithm of minimization
Double m_DescendingFactor Property Factor of a diminution of area of a scatter of testing points for arguments
Double m_IncreasingFactor Property Factor of magnification of area of a scatter of testing points for arguments
Static double m_eps Property The value describing a relative machine error
LPCTSTR m_lpszLogFile Property The name log file
LPCTSTR m_lpszComment Property The comment minimizing function
BOOL m_bOutLog Property The Indication output data to log file
CRFileLog m_FileLog Property The instance of class CRFileLog

Download source code [458KB]
Download examples [442KB]

 
 
Hosted by uCoz