This sections describes how to compose a problem, load required data, solve a problem and get results using example from previous section Problem Description for CPP Environment.

We suppose that CBITestConSolverHelperImpl class is completely designed and CBITestConSolverHelperImpl.dll is created (see the section Class CBITestConSolverHelperImpl). In addition, the console application will be constructed as the project included to the common CBITestCon solution.

 

To build the project for example problem perform the following steps:

 

1. Create x64 (or Win32) Console Application project with the name " CBITestCon " in the solution named CBITestCon.

2. Configure the project properties:

 

CBITestCon Property Pages->C/C++->General->Additional Include Directories->”..\Include”
CBITestCon Property Pages->Linker-> Input-> Additional Dependencies ->”..\lib\x64\PSGSolver_x64.lib”

 

Keep other properties defined by default.

3. Set CBITestCon project as depended on the CBITestConSolverHelperImpl project (Project->Project Dependencies->Dependencies tab). Also make sure that CBITestCon project is built after CBITestConSolverHelperImplImpl (Project->Project Dependencies->Build Order tab).

4. Add to the header files list of the CBITestCon.C++ the following lines:

 

#include "../CBITestConSolverHelperImpl/ImplSolverHelperFile.h"

#include "../Include/PSGSolver.h"

#include <string>

#include <iostream>

#include <iomanip>

 

using namespace std;

 

 

5. Implement the function of the initial dialog (program start helper)

 

void ShowUsage()
{
       cout << "USAGE:" << endl;
       cout << "\tCBITestCon.exe \n"
               "\t\t-r \"path to data folder\" \n"
               "\t\t-p \"problem file\" \n"
               "\t\t-i \"initial point file\"\t(optional, default empty)\n"
               "\t\t-a Accuracy\t\t(optional, default 12)\n"
               "\t\t-s Solver\t\t(optional [VAN | CAR | BULDOZER], default VAN)\n"
               "\t\t-t Stages\t\t(optional, default 10)" << endl;
}

6. Write the initial dialog to define the start parameters:

 

 if(argc == 1)

 {

         ShowUsage();

         return 0;

 }

 string strRootPath;

 string strProblemFile;

 string strInitpoint;

 double Accuracy = 2.0;

 string sSolver("VAN");

 int solver = 0;

 int stages = 0;

 string arg;

 for(int i = 1; i < argc; ++i)

 {

         arg = argv[i];

         if(arg == "-r")

                 strRootPath = argv[++i];

         else if(arg == "-p")

                 strProblemFile = argv[++i];

         else if(arg == "-i")

                 strInitpoint = argv[++i];

         else if(arg == "-a")

                 Accuracy = atof(argv[++i]);

         else if(arg == "-s")

                 sSolver = argv[++i];

         else if(arg == "-t")

                 stages = atoi(argv[++i]);

         else

         {

                 cout << "Invalid parameter " << endl;

                 return -1;

         }

 }

 if(sSolver == "VAN")

         solver = 1;

 else if(sSolver == "CAR")

         solver = 2;

 else if(sSolver == "BULDOZER")

         solver = 3;

 else if(sSolver == "TANK")

         solver = 4;

 else

 {

         cout << "Invalid solver " << sSolver << " is specified" << endl;

         return -1;

 }

 

 if(Accuracy < 1.0 || Accuracy > 12.0)

 {

         cout << "Invalid accuracy " << Accuracy << " is specified. Accuracy must be in range [1, 12]" << endl;

         return -1;

 }

7. Define name of output file with problem solution:

 

 string solFileName = "solution_" + strProblemFile;

 

8. Create and initialize the CBITestConSolverHelperImpl implementation:

CImplSolverHelperFile hlp(

         strRootPath.c_str(),

         strProblemFile.c_str(),

         strInitpoint.c_str(),

         "ErrorFile.txt",

         solFileName.c_str(),

         "LogFile.txt"

         );

hlp.Init();

9. Create the implementation of PSG Solver:

 

 CPSGSolver solvEngine;

 

10. Start the problem solving process:

 

 if(solvEngine.Start(&hlp, solver, stages, Accuracy, strInitpoint.c_str()) >= 100)

 {

         hlp.End();

         cout << "Solver returns error." << endl;

         return -1;

 }

 

11. All results will be stored in output files (see the section Class CBITestConSolverHelperImpl). If you want to display the results on screen, add the next clear code:

 

 string objective_name;

 double objective_val = 0;

 cout << "\n\n==================================================================" << endl;

 cout << _T("Problem:\t\t\t") << hlp.GetSolution() << endl;

 hlp.GetObjective(objective_name, objective_val);

 if(objective_name.empty())        {

         hlp.End();

         cout << "ERROR: Can not get objective name." << endl;

         return -1;

 }

 cout << objective_name << _T("\t\t\t") << setprecision(12) << objective_val << endl;

 cout << _T("Constraints_list. Constraints_number = ")<<hlp.GetConstraints().size()<<endl;

 for(ArrOfNamedVals::const_iterator it = hlp.GetConstraints().begin(); it != hlp.GetConstraints().end(); ++it)

 {

         TCHAR ctype = (*it).m_type;

         switch(ctype)

         {

         case 'S':

                 cout << setw(20) << (*it).m_name << _T("\t") << scientific << setw(12) << setprecision(12) << (*it).m_values << endl;

                 break;

         case 'V':

                 cout << setw(20) << (*it).m_name << _T("\t") << setw(12) << _T("vector_") << (*it).m_name << endl;

                 break;

         case 'E':

                 cout << setw(20) << (*it).m_name << _T("\t") << setw(12) << _T("Empty") << endl;

                 break;

         default:

                 cout << setw(20) << (*it).m_name << _T("\t") << setw(12) << _T("Incorrect type '") << ctype << _T("'") << endl;

         }

 }

 cout << _T("Variables_list") << endl;

 int id = 1;

 cout << setw(6) << _T("ID") << setw(20) << _T("Name") << _T("\t") << setw(12) << _T("Value") << endl;

 for(ArrOfNamedVals::const_iterator it = hlp.GetVariables().begin(); it != hlp.GetVariables().end(); ++it )

         cout << setw(6) << id++ << setw(20) << (*it).m_name << _T("\t") << scientific << setw(12) << setprecision(12) << (*it).m_values << endl;

 cout << _T("Objects_list") << endl;

 for(ArrOfNamedVals::const_iterator it = hlp.GetObjects().begin(); it != hlp.GetObjects().end(); ++it )

 {

         TCHAR ctype = (*it).m_type;

         switch(ctype)

         {

         case 'S':

                 cout << setw(40) << left << (*it).m_name << _T("\t") << scientific << setw(12) << setprecision(12) << (*it).m_values << endl;

                 break;

         case 'V':

                 cout << setw(40) << left << (*it).m_name << _T("\t") <<  setw(12) << (*it).m_constr << endl;

                 break;

         }

 }

 for(ArrOfNamedVectors::const_iterator it = hlp.GetVectors().begin(); it != hlp.GetVectors().end(); ++it)

 {

         if((*it).m_name.empty())

         {

                 hlp.End();

                 cout <<  _T("Empty vector name") << endl;

                 return -1;

         }

 

         cout << setw(6) << _T("ID") << _T("\t") << setw(12) << _T("Value") << endl;

         id = 1;

         for(vector<double>::const_iterator vecIt = (*it).m_values.begin(); vecIt != (*it).m_values.end(); ++vecIt)

                 cout << setw(6) << id++ << _T("\t") << scientific << setw(12) << setprecision(12) << (*vecIt) << endl;

 }

 

12. If you need the functions values, increments and sensitivities, add the code bellow, to calculate, show on screen and store in files these information:

// functions values

 ofstream FunctionsValuesFile;

 string strFunctionsValuesFile = hlp.GetProblemFile();

 if (strFunctionsValuesFile.find(_T("problem_")) == string::npos) {

         strFunctionsValuesFile = _T("FunctionsValues.txt");

 }

 else {

         strFunctionsValuesFile = _T("Functions_") + strFunctionsValuesFile.substr(8, strFunctionsValuesFile.length()-7);

 }

 strFunctionsValuesFile = hlp.GetRootPath() + _T("\\") + strFunctionsValuesFile;

 string strPointName = hlp.GetOptimalPointFile();

 strPointName = strPointName.substr(0, strPointName.length()-4);

 double thisFunctionValue;

 long bufferlength;

 cout << _T("Functions:") << endl;

 FunctionsValuesFile.open(strFunctionsValuesFile.c_str(), ios_base::trunc);

 if(!FunctionsValuesFile.fail()) {

         for(ArrOfNamedVals::const_iterator it = hlp.GetObjects().begin(); it != hlp.GetObjects().end(); ++it )        {

                 if (hlp.GetThisFunctionValue((*it).m_name.c_str(), strPointName.c_str(), &thisFunctionValue) != 1) {

                         FunctionsValuesFile << setw(40) << left << (*it).m_name << _T("\t") << scientific << setw(12) << setprecision(12) << thisFunctionValue << endl;

                         cout << setw(40) << left << (*it).m_name << _T("\t") << scientific << setw(12) << setprecision(12) << thisFunctionValue << endl;

                 }

         }

 }

 FunctionsValuesFile.close();

// functions increments and sensitivities

 strFunctionsValuesFile = hlp.GetProblemFile();

 if (strFunctionsValuesFile.find(_T("problem_")) == string::npos) {

         strFunctionsValuesFile = _T("Increments_Sensitivities.txt");

 }

 else {

         strFunctionsValuesFile = _T("Increments_Sensitivities_") + strFunctionsValuesFile.substr(8, strFunctionsValuesFile.length()-7);

 }

 strFunctionsValuesFile = hlp.GetRootPath() + _T("\\") + strFunctionsValuesFile;

 std::vector<std::string>Variables_vec;

 std::vector<double> IncrValues_vec, SensValues_vec;

 cout << _T("Increments and Sensitivities") << endl;

 FunctionsValuesFile.open(strFunctionsValuesFile.c_str(), ios_base::trunc);

 if(!FunctionsValuesFile.fail()) {

         for(ArrOfNamedVals::const_iterator it = hlp.GetObjects().begin(); it != hlp.GetObjects().end(); ++it )        {

                 double func_value;

                 if (hlp.GetThisFunctionIncrement((*it).m_name.c_str(), strPointName.c_str(), &func_value, Variables_vec, IncrValues_vec) == 1) {

                         continue;

                 }

                 if (hlp.GetThisFunctionSensitivity((*it).m_name.c_str(), strPointName.c_str(), &func_value, Variables_vec, SensValues_vec) == 1) {

                         continue;

                 }

                 FunctionsValuesFile << _T("Function: ") << _T("\t") << (*it).m_name << endl;

                 FunctionsValuesFile << setw(40) << left << _T("Component_Name") << setw(20) << left << _T("\tIncrements")<< setw(20) << left << _T("\tSensitivities") << endl;

 

                 cout << _T("Function: ") << _T("\t") << (*it).m_name << endl;

                 cout << setw(40) << left << _T("Component_Name") << setw(20) << left << _T("\tIncrements")<< setw(20) << left << _T("\tSensitivities") << endl;

 

                 for (long i=0; i<Variables_vec.size(); i++) {

                         FunctionsValuesFile << setw(40) << left << Variables_vec[i] <<

                                 _T("\t") << scientific << setw(20) << right << setprecision(12) << IncrValues_vec[i] <<

                                 _T("\t") << scientific << setw(20) << right << setprecision(12) << SensValues_vec[i] <<

                                 endl;

                         cout << setw(40) << left << Variables_vec[i] <<

                                 _T("\t") << scientific << setw(20) << right << setprecision(12) << IncrValues_vec[i] <<

                                 _T("\t") << scientific << setw(20) << right << setprecision(12) << SensValues_vec[i] <<

                                 endl;

                 }

                 Variables_vec.clear();

                 FunctionsValuesFile << endl;

                 cout << endl;

         }

 }

 FunctionsValuesFile.close();

 

13. Unload and clear CBITestConSolverHelperImpl implementation:

hlp.End();

 

Remark. This code allows to solve any correct problem, presented in Run-File format with data (Problem statement file, Matrices, Points and Vectors files) located in "path to data folder". Problem statement is reading from "problem file". Results are being stored in files (solution_problem_cvar.txt, point_problem_cvar.txt, Functions_cvar.txt, Increments_Sensitivities_cvar.txt) in initial data folder. Log and Error information is being stored in LogFile.txt and ErrorFile.txt. For example, to solve the problem_cvar (problem description – in file problem_cvar.txt) with working directory Data (where matrix_budget.txt, matrix_scenarios.txt, point_lowerbounds.txt are located) you can use command line like:

Startfolder\CBITestCon.exe -r ".\Data" -p "problem_variance.txt"

Do not forget to write libraries:

IPSGSolverHelper_x64.dll
MatLabSolverHelper_x64.dll
Portf_Optim_DAO_x64.dll
Portf_Optim_Startup_x64.dll
PSGAnalizer.dll
PSGSolver_x64.dll
vk_solver_mdb_x64.dll
CBITestConSolverHelperImpl.dll

to the current start folder.