This repository has been archived on 2021-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
NC/mp6/Project_6_Maggioni_Claudio/standardize.m

56 lines
1.9 KiB
Mathematica
Raw Permalink Normal View History

2020-12-18 14:43:56 +00:00
function [A_aug,h,c_aug] = standardize(type,A,h,c,m,sign)
% Input arguments:
% type = 'max' for maximization, 'min' for minimization
% A = matrix holding the constraints coefficients
% h = coefficients of the constraints inequalities (rhs vector)
% c = coefficients of the objective function
% m = size(A, 1)
% sign = vector holding information about the constraints if the system
2020-12-19 22:27:43 +00:00
% needs to be standardized (-1: less or equal, 0: equal, 1: greater or equal)
2020-12-18 14:43:56 +00:00
% return arguments are:
% (1) A_aug = augmented matrix A, containing also the surplus and slack variables
% (2) c_aug = augmented coefficients vector c (check compatibility of dimensions with A)
% (3) h, right hand side vector (remember to flip the signs when changing the inequalities)
aug_matrix = eye(m); % matrix corresponding to the slack/surplus variables
if strcmp(type, 'max')
for i = 1:m
if sign(i) == 1
% Using a surplus instead of a slack variable
aug_matrix(i,:) = -aug_matrix(i,:);
end
end
elseif strcmp(type, 'min')
for i = 1:m
if sign(i) == -1
% Using a slack instead of a surplus variable
aug_matrix(i,:) = -aug_matrix(i,:);
end
end
else
error('Incorrect type specified. Choose either a maximisation (max) or minimisation (min) problem.')
end
% Correction on the sign of h for auxiliary problem (we want to
% ensure that h>=0, but we need to flip all the signs)
for i = 1:m
if h(i) < 0
A(i,:) = -A(i,:);
h(i,:) = -h(i,:);
aug_matrix(i,:) = -aug_matrix(i,:);
end
end
c_aug = [c, zeros(1,m)];
if strcmp(type,'max')
2020-12-19 22:27:43 +00:00
A_aug = [A aug_matrix];
% Extend matrix A by adding the slack variables
2020-12-18 14:43:56 +00:00
elseif strcmp(type,'min')
2020-12-19 22:27:43 +00:00
A_aug = [A -aug_matrix];
2020-12-18 14:43:56 +00:00
% Extend matrix A by adding the surplus variables
else
error('Incorrect type specified. Choose either a maximisation (max) or minimisation (min) problem.')
end
end