This section describes how to develop the class for data exchange between a user program and PSG Solver. If you just starting learning PSG C++ Environment, just copy the project from folder ...Aorda\PSG\CPP\CBITestConSolverHelperImpl and attach it to your solution (Project->Add->Existing Project). However we strongly recommend you to read this section to understand the idea and technique of working with PSG C++ Environment.

 

To implement CImplSolverHelperFile class perform the following steps:

 

1.Create x64(Win32) Console Application DLL project with the name "CBITestConSolverHelperImpl" in the solution named CBITestCon.
2.Add to the project CBITestConSolverHelperImpl exported class with virtual destructor inheriting the class IPSGSolverHelper.
3.Create Lib\x64 folder and put to it the following libraries: IPSGSolverHelper_x64.lib, portf_optim_DAO_x64.lib, and PSGSolver_x64.lib (in the case of x64 project).
4.Create Include folder and put to it the following header files: IPSGSolverHelper.h, portf_optim.h, PSGSolver.h.
5.Configure the project properties:
CBITestConSolverHelperImplProperty Pages->C/C++->General->Additional Include Directories->”..\Include”
CBITestConSolverHelperImplProperty Pages->C/C++->Preprocessor->Preprocessor Definitions = WIN32, NDEBUG, _WINDOWS, _USRDLL, CBITESTCONSOLVERHELPERIMPL_EXPORTS
CBITestConSolverHelperImplProperty Pages->Linker->Input->Additional Dependencies = ..\lib\x64\IPSGSolverHelper_x64.lib;..\lib\x64\portf_optim_DAO_x64.lib

 

Remark    Keep other properties defined by default.

 

6.Create and add the C++ class CImplSolverHelperFile inherited as public from IPSGSolverHelper pure virtual class which is defined in IPSGSolverHelper.h.
7.Implement the constructor of CImplSolverHelperFile class CImplSolverHelperFile(PCSTR szRootPath, PCSTR szProblemFile, PCSTR szInitPointFile, PCSTR szErrorFile, PCSTR szSolutionFile, PCSTR szLogFile).
8.Add and implement in the CCBITestConSolverHelperImpl class the following virtual functions:

 

virtual int Init();
virtual void End();
virtual int GetProblemDescription(LPTSTR& pBuffer);
virtual int GetPoint(LPCTSTR szPointName, LPTSTR& pBuffer);
virtual int  GetMatrixInfo(LPCTSTR lpszMatrixName, LPTSTR& pHeaderBuf, int* rows, int* cols);
virtual int  GetMatrixInfoSp(LPCTSTR lpszMatrixName, LPTSTR& pHeaderBuf, int* rows, int* cols, int* coefs) {return 1;}
virtual BOOL GetMatrixData(LPCTSTR lpszMatrixName, double*& pData,  int rows, int cols);
virtual BOOL GetMatrixDataSp(LPCTSTR lpszMatrixName, double*& pData, int*& prows, int*& pcols, int coeffs) {return TRUE;}
virtual void ReleaseBuffer(void* pBuffer);
virtual void ReleaseMatrix(double* pMatrix);
virtual BOOL SaveObjective(TCHAR szObjectiveName[max_objectname_len], double value);
virtual BOOL SaveVars(TCHAR szVarsName[][max_name_len], double values[], unsigned nCount);
virtual BOOL SaveVector(LPCTSTR szVectorName, double varvalues[], unsigned nCount);
virtual BOOL SaveObjs(TCHAR szObjName[][max_objectname_len], double val[],TCHAR szConstrName[][max_name_len], TCHAR type[],unsigned nCount);
virtual BOOL SaveConstraints(TCHAR szConstrName[][max_name_len], double val[], TCHAR type[], unsigned nCount );
virtual BOOL SaveConstraintsSlack(TCHAR szConstrName[][max_name_len], double val[], TCHAR type[], unsigned nCount ) {return TRUE;}
virtual BOOL SaveStatus(LPCTSTR szStatus);
virtual void OnMessage(int nMsgCode, LPTSTR szMessage);
virtual void OnError(int nErrCode, LPCTSTR lpszObject, LPCTSTR lpszDescription);
virtual void OnWarning(int nWarnCode, LPCTSTR lpszObject, LPCTSTR lpszDescription);
virtual int  OnCancel(void);
virtual int OnFinish(void);
virtual int OnCycleFinish(void) {return 1;}
virtual void GetRootPath(LPTSTR pBuffer);
virtual int GetLogParam( LPTSTR pBuffer ) {return 1;}
virtual int SaveProblemName(LPTSTR problemname)  {return 1;}
virtual int SaveProblemStatement(LPTSTR problemstatement) {return 1;}
virtual BOOL SaveMatrix(LPCTSTR szMatrixName, TCHAR szVarsName[][max_name_len], double matrixdata[], int numcol, int numrow) {return TRUE;}
virtual BOOL AddPoint(LPCTSTR szPointName, TCHAR szVarsName[][max_name_len], double values[], unsigned nCount);
virtual BOOL AddVector(LPCTSTR szVectorName, double varvalues[], unsigned nCount) {return TRUE;}
virtual BOOL AddMatrix(LPCTSTR szMatrixName, TCHAR szVarsName[][max_name_len], double matrixdata[], int numcol, int numrow) {return TRUE;}

 

In the constructor input parameters:

szRootPath – path to the main problem data folder;

szProblemFile – short path to the problem description file;

szInitPointFile – short path to the problem initial point file;

szErrorFile – short path to the problem error file;

szSolutionFile – short path to the problem solution file;

szLogFile – short path to the problem log file;

 

Virtual functions are called by PSG Solver and are designed for data exchange between user program and PSG Solver.

PSG Environment should be initialized before using and closed after using. Init function is designed for initialization of the PSG Environment. Actually only IPSGSolverHelper::Init() call is mandatory  in the body of this virtual function. You can add your own initialization and verification code here.

The same considerations are applied for End function. Only IPSGSolverHelper::End() call is mandatory here.

The following virtual functions are grouped into 3 groups:

 

1.Functions providing data exchange between user program and PSG Solver.

 

GetProblemDescription function is called by PSG Solver and must to provide the text of Problem Statement in General (Text) Format.
GetPoint function is called by PSG Solver and must to provide the PSG point named *szPointName data as PSG Point in General (Text) Format.
GetMatrixInfo function is called by PSG Solver and must to provide tab separated names of variables, number of rows and number of columns of matrix with name from lpszMatrixName. Number of columns has be equal to the number of variables. See PSG Matrix in General (Text) Format.
GetMatrixData function is called by PSG Solver and must to provide the numerical one row (by rows) data of matrix (named lpszMatrixName). See PSG Matrix in General (Text) Format.
ReleaseBuffer function releases allocated memory.
ReleaseMatrix function releases memory, allocated in GetMatrixData function.
OnCancel is a callback function allowing to stop running of the PSG Solver (in this case OnCancel function must return 1). In the current example this function is stubbed (returned value always equals 0).

Remark You should  allocate the necessary memory for all objects (text or numerical) yourself. PSG Solver will free this memory after using itself by calling ReleaseBuffer and ReleaseMatrix.

 

2.Communicating functions for transferring information from PSG Solver to user program.

 

OnMessage function transfers message code and message body from PSG Solver to the user program.
OnError function provides notification of user program by the PSG Solver about errors during performing of the operations. The error code, the name of the internal object, in which run time error has been occurred, and error body are transferred. After completion of this function the PSG Solver is stopped.
OnWarning function transfers to user program  warnings  from PSG Solver during operations. The warning code, the name of the internal object, in which warning has been occurred, and the error body are transferred. After completion of this function PSG Solver is not stopped.

 

3.Functions transferring optimization problem solution from PSG Solver into user program.

 

SaveStatus function transfers problem solving status (optimal, feasible, unfeasible, unbounded) as argument.
SaveObjective function transfers the name of problem Objective and its value obtained as a result of optimization.
SaveVars function transfers values of the optimization variables, obtained during optimization. Values array is arranged accordingly to the order of the variables names in array szVarsName.
SaveObjs function transfers the values of the functions presented in the optimization problem, which are calculated on the values of the variables, obtained as result of the optimization. Function descriptions are transferred to to the szObjName; val – values of the functions arranged accordingly to the order of the function names in the array szObjName; type – function type (char: 'S' - scalar, 'V' - vector). As long as functions can return either scalar value or vector value you must monitor their type. For the vector functions val (double) includes only the value of the first component. The complete resulting vector could be find using the name of the constraint generated by the given function (corresponding value of the szConstrName). As long as the vector function couldn't be presented in the objective, and should be single in the constraint, its resulting vector is generated using name vector_nameofconstraint, where the nameofconstraint – the name of the constraint including the given vector function. Vectors are transferred by the SaveVector function.
SaveConstraints function transfers the values of the constraints, presented in the optimization problem, which are calculated on the values of the variables, obtained as the result of the optimization. Constraints names are transferred to the szConstrName; val – values of the constraints arranged accordingly to the order of the constraints names in the array szConstrName; type – function type (char: 'S' - scalar, 'V' - vector,  'E' - empty). For the vector constraints val (double) includes only the value of the first component. The complete resulting vector is generated using the name vector_nameofconstraint, where the nameofconstraint = *szConstrName. Vectors are transferred by the SaveVector function.
SaveVector function transfers the resulting values vectors of the vector constraints (i.e. constraints generated by the vector functions). szVectorName – name of the vector; varvalues - values arranged accordingly to the id of the generating function.
OnFinish function is invoked after completion of solving of the optimization problem and could be used to perform the finalizing operations.

 

 

9. Build the DLL.

 

 Remark

Implemented class should be designed as DLL and could be used not only for current example but also for implementation other (not too large-scale) problems. At the same time it seems you have to rewrite the implementation of some functions.

 

For quick start see example Problem Optimization in C++ .