summaryrefslogtreecommitdiff
path: root/code/linprog/Blending_problem.sce
blob: 24b80ad8e2a38fd8991fbb2dba04803e4ff53c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// A practcal problem of blending of different products to acheive designed performance levels.
// This is an example from the class of allocation problems.
// Ref:H.A. Eiselt and C.-L.SAndblom,"Linear Programming and its Applications",Springer-Verlag Berlin Heidelberg 2007,chapter 2.7
//Example:
//A firm faces the problem of blending three raw materials into two final
//products. The required numerical information is provided in Table.
//
//-----------------------------------------------------------------------
//  Raw                 Products          Amount available     Unit Cost 
//Materials             1       2             (in tons)          ($)
//-----------------------------------------------------------------------
//     1             [0.4;0.6][0.5;0.6]        2000               1
//     2             [0.1;0.2][0.1;0.4]        1000               1.5
//     3             [0.2;0.5][0.2;0.3]        500                3
//-----------------------------------------------------------------------
//quantity required     600      700
//(in tons)
//-----------------------------------------------------------------------
//unit selling price    10        8
//($) 
//-----------------------------------------------------------------------
//we assume that raw materials blend linearly, meaning that taking,
//say, α units of raw material A and β units of raw material B, then
//the resulting blend C has features that are proportional to the
//quantities of A and B that C is made of. As an example, take 3 gallons
//of 80º water and 2 gallons of 100º water,then the result would be 5
//gallons of water, whose temperature is [3(80) + 2(100)]/5 = 88º.
//The parameters max and min indicate the smallest and largest proportion
//of rawmaterial that is allowed in the final product.Determine the
//optimal transportation plan to maximize profit.

// Copyright (C) 2018 - IIT Bombay - FOSSEE
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
// Author:Debasis Maharana
// Organization: FOSSEE, IIT Bombay
// Email: toolbox@scilab.in
//======================================================================
clc;
Nprod = 2;
Nraw = 3;
demand = [600 700];
cost = [10 8];
con(1,:,:) = [0.4 0.6;0.1 0.2;0.2 0.5];
con(2,:,:) = [0.5 0.6;0.1 0.4;;0.2 0.3];
raw = [2000 1000 500];
RawC = [1 1.5 3];
mprintf('Received Data')
mprintf('Number of products %d',Nprod);
mprintf('number of raw materials %d',Nraw);
disp(demand,'Demand of products');
disp(cost,'Unit cost of products');

product = [];Min_Max = [];
for i = 1:Nprod
    product = [product,['Min-Max',string(i)]];
    for j = 1:Nraw
        MM(j,:) = con(i,j,:);
    end
    Min_Max = [Min_Max string(MM)];
end
Row1 = ['Raw Material',product,'Amount_avail','unit_Cost']
RawMaterial = string([1:Nraw]');
Amount_avail = string(raw(:));
unit_C = string(RawC(:));    
Table = [Row1;[RawMaterial Min_Max Amount_avail unit_C]];
disp(Table);
input('Press Enter to proceed ')
A1 = zeros(Nraw,Nraw*Nprod);
for i = 1:Nraw
    A1(i,i:Nraw:Nraw*Nprod)=1;
    b1(i) = raw(i);
end
A2 = zeros(Nprod*Nraw*2,Nprod*Nraw);b2 = zeros(Nprod*Nraw*2,1);
A3 = zeros(Nprod,Nprod*Nraw);
for i = 1:Nprod
    Areq = ones(2*Nraw,Nraw);
    Areq(Nraw+1:2*Nraw,:) = -1;
    for j =1:Nraw
        Areq(j,:) = con(i,j,1)* Areq(j,:);
        Areq(j,j) = Areq(j,j)-1;
        Areq(Nraw+j,:) = Areq(Nraw+j,:)*con(i,j,2);
        Areq(Nraw+j,j) = 1 + Areq(Nraw+j,j);
    end
    A2((i-1)*2*Nraw+1:i*2*Nraw,(i-1)*Nraw+1:i*Nraw) = Areq;
    A3(i,(i-1)*Nraw+1:i*Nraw)= ones(1,Nraw);
    b3(i) = demand(i);
end

A = [A1;A2];b = [b1;b2];
Aeq = A3;beq = b3;
C = [];
for i = 1:Nprod
    C1 =  -cost(i)*ones(1,Nraw) + RawC;   
    C = [C ;C1'];
end

lb = zeros(1,Nprod*Nraw);
intcon = 1:Nprod*Nraw;
[xopt,fopt,exitflag,output,lambda] = linprog(C,A,b,Aeq,beq,lb,[]);
clc
select exitflag
case 0 then
    mprintf('Optimal Solution Found')
    input('Press enter to view results')
    //Display results
    mprintf('Total profit is %d\n',-fopt);

    A1 = [];product = [];
    for i=1:Nprod
        Raw_M(:,i) = xopt((i-1)*Nraw+1:i*Nraw) ;
        product(i) = string(sum(Raw_M(:,i)));
        A1 = [A1 strcat(['Product' string(i)])];
    end

    table = [['RawMaterial', A1];[RawMaterial string(Raw_M)];['Total Product' product']];
    disp(table);
case 1 then
    mprintf('Primal Infeasible')
case 2 then
    mprintf('Dual Infeasible')
case 3
    mprintf('Maximum Number of Iterations Exceeded. Output may not be optimal')
case 4
    mprintf('Solution Abandoned')
case 5
    mprintf('Primal objective limit reached')
else
    mprintf('Dual objective limit reached')
end