% Weigted Sum approach to the 
% multiobjective optimization exercise
% at the end of Section 5.

% Data:
R = [1, 1, 1, 2, 2];
T = [1, 2.2, 2, 4, 2];
C = [520, 364, 410, 150, 125];

% Continuous model of data:
pR = polyfit(1:5, R, 4);
pT = polyfit(1:5, T, 4);
pC = polyfit(1:5, C, 4);

% Optimization:
i = lsqnonlin(@wbridgefun, 1, 1, 5, [], pR, pT, pC);
i = round(i);

disp(strvcat( ...
       ' ', ...
       'Highest Priority (optimization result):', ...
       ['Bridge #', num2str(i)], ...
       ' ') ...
       );

% Sort data directly:
rR = (R-1)/(2-1);
rT = (4-T)/(4-1);
rC = (C-125)/(520-125);

F = rR.^2 + rT.^2 + rC.^2;

[F, I] = sort(F);

disp(strvcat( ...
        'Maintenance Schedule (direct sort):', ...
        ['First: Bridge #', num2str(I(1))], ...
        ['Second: Bridge #', num2str(I(2))], ...
        ['Third: Bridge #', num2str(I(3))], ...
        ['Fourth: Bridge #', num2str(I(4))], ...
        ['Fifth: Bridge #', num2str(I(5))], ...
        ' ') ...
       );
   
% Budget schedule:
B = 1000;
spent = 0;
k = 0;
while spent < B
    k = k +1;
    if C(I(k)) < B - spent
        spent = spent + C(I(k));
    else
        break
    end   
end

disp('Budget Schedule:')
for j = 1:k-1
disp(['Bridge #', num2str(I(j)), ' ', num2str(C(I(j))), ' ', 'thousand']);
end
disp(['Bridge #', num2str(I(k)), ' ', num2str(B-spent), ' ', 'thousand', ...
        ' ', '(toward cost)']);    