Search code examples
octave

how to solve maximization problem in octave


Hello everyone i have the following assignment: screen assignment

And the following code:

c = [-12; -15; -20; -10];  % Coefficients of the objective function to be maximized

A = [
    1, 1/2, 3, 7;   % Oil type 1
    4, 2, 5, 6;     % Oil type 2
    8, 1/3, 4, 1    % Oil type 3
];

b = [1000; 1500; 2000];  % RHS values of the processing time constraints

lb = [50; 0; 0; 0];      % Lower bounds on the variables
ub = [ 50; 250; 350; 10 ]; % Upper bounds on the variables

ctype = "UUU";      % Constraints are all <=
vartype = "CCCC";     % Variables are continuous

s = -1;              % Objective sense: -1 for maximization

param.msglev = 1;    % Output option: 1 -> Error and warning messages only
param.itlim = 100;   % Iteration limit

[xmax, fmax, status, extra] = glpk(c, A, b, lb, ub, ctype, vartype, s, param);

xmax  % Optimal solution vector [x1, x2, x3, x4]
fmax  % Optimal objective value (total profit per week)
status  % Status of the solver

And the following output:


   50
    0
    0
    0

fmax = -600
status = 0

Why I get a negative value? If something is not clear let me know


Solution

    • The negative value in the code s = -1 is used to indicate that the objective function should be maximized . In this case, the objective coefficients c are multiplied by -1 to convert the maximization problem into a minimization problem.

    • If you want to maximize the objective function, you can change the code s = -1 to s = 1. This will set the objective sense to 1 for maximization.