Lesson 3. Create Problem Programmatically

This lesson shows how to create and solve optimization problem using PSG m-functions. All MATLAB operations are in the file: ./Aorda/PSG/MATLAB/Examples/Toolbox/Example_CVaR_Toolbox.m

 

Step 1. Pack data to PSG format
Step 2. Prepare Problem Statement in General (text) Format
Step 3. Solve optimization problem
Step 4. Extract solution details
Step 5. Edit problem in Toolbox

 

Step 1. Pack data to PSG format

 

Define input data in MATLAB Command Window:

 

H=[1,4,8,3; 7,5,4,6; 2,8,1,0; 0,3,4,9];

c=[0.2; 0.11; 0.6; 0.1];

Aeq=[1, 1, 1, 1];

lb=[0; 0; 0; 0];

vars={'x1', 'x2', 'x3', 'x4'};

 

Pack input data to PSG structure containing headers and data (see Main Objects of PSG MATLAB):

 

Create PSG matrix 'matrix_scenarios' (see tbpsg_matrix_pack):

 

toolboxstruc_arr(1) = tbpsg_matrix_pack('matrix_scenarios',H,vars,c);

 

Create PSG matrix 'matrix_budget' (see tbpsg_matrix_pack):

 

toolboxstruc_arr(2) = tbpsg_matrix_pack('matrix_budget',Aeq,vars);

 

Create PSG point 'point_lowerbounds' (see tbpsg_point_pack):

 

toolboxstruc_arr(3) = tbpsg_point_pack('point_lowerbounds',lb,vars);

 

Step 2. Prepare Problem Statement in General (text) Format

 

Problem statement syntax is described in Problem Statement in MATLAB.

Create the following variable in MATLAB:

 

problem_statement = sprintf('%s\n',...

'minimize',...

'  cvar_risk(0.95, matrix_scenarios)',...

'Constraint: >= 4.5',...

'  avg_g(matrix_scenarios)',...

'Constraint: == 1',...

'  linear(matrix_budget)',...

'Box: >= point_lowerbounds');

 

Step 3. Solve optimization problem

 

Solve optimization problem programmatically (see tbpsg_run):

 

>> [solution_str,outargstruc_arr] = tbpsg_run(problem_statement, toolboxstruc_arr);

 

All results will be saved to the text report "solution_str" and structure "outargstruc_arr" (see PSG Solution in MATLAB).

 

Step 4. Extract solution details

 

Convert PSG Solution to structure using tbpsg_solution_struct:

 

output_structure = tbpsg_solution_struct(solution_str, outargstruc_arr);

 

>> output_structure

 

output_structure =

 

             status: {'optimal'}

          objective: -4.350000000000000

    constraint_name: {2x1 cell}

   constraint_value: [2x1 double]

      function_name: {3x1 cell}

     function_value: [3x1 double]

               time: [0 0.180000000000000 0]

         point_data: [0 0.599999999999997 0.190000000000002 0.210000000000001]

         point_vars: {'x1'  'x2'  'x3'  'x4'}

        matrix_data: {}

        matrix_vars: {}

        vector_data: {}

 

Save values of function to the new variable using:

 

>> output = tbpsg_function_data(solution_str, outargstruc_arr)

 

output =

 

   4.5000

  -4.3500

   1.0000

 

Full list of available PSG m-functions for extracting solution details: Extract Solution Results Functions.

 

Step 5. Edit problem in Toolbox

 

Open current problem in Toolbox Window (see tbpsg_toolbox):

 

>> tbpsg_toolbox(problem_statement, toolboxstruc_arr);

 

 

The following screen will be opened:

 

Toolbox

 

You can edit this problem using Toolbox Window (change problem statement and input data). For example, delete the second constraint:

 

Toolbox_1

 

Press button "Solve" to start solving the optimization problem.

 

Toolbox_2