Skip to content

Commit 2da1143

Browse files
authored
Add files via upload
1 parent 6ab4c59 commit 2da1143

13 files changed

+1508
-88
lines changed

codes/chapter3/GenerateRandomLPs.m

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function GenerateRandomLPs
2+
% Filename: GenerateRandomLPs.m
3+
% Description: Custom menu for creating random LPs
4+
% Authors: Ploskas, N., & Samaras, N.
5+
%
6+
% Syntax: GenerateRandomLPs
7+
%
8+
% Input: None
9+
%
10+
% Output: files that store the generated random LPs
11+
12+
choice = 0;
13+
while choice ~= 4
14+
% show menu
15+
choice = menu('Generate Random LPs', ...
16+
'Generator Initialization', 'Dense Random LPs', ...
17+
'Sparse Random LPs', 'Exit');
18+
if choice == 1 % initialize generator
19+
seedNumber = input('Please give the seed number: ');
20+
rand('state', seedNumber);
21+
sprand('state');
22+
elseif choice == 2 % create dense random LPs
23+
m = input('Please give the number of constraints: ');
24+
n = input('Please give the number of variables: ');
25+
denseMenu(m, n);
26+
elseif choice == 3 % create sparse random LPs
27+
m = input('Please give the number of constraints: ');
28+
n = input('Please give the number of variables: ');
29+
sparseMenu(m, n);
30+
elseif choice == 4 % exit
31+
return;
32+
end
33+
end
34+
end

codes/chapter3/denseMenu.m

+88-88
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
function denseMenu(m, n)
2-
% Filename: denseMenu.m
3-
% Description: Custom menu for creating dense random LPs
4-
% Authors: Ploskas, N., & Samaras, N.
5-
%
6-
% Syntax: denseMenu(m, n)
7-
%
8-
% Input:
9-
% -- m: the number of constraints of the generated random LPs
10-
% -- n: the number of variables of the generated random LPs
11-
%
12-
% Output: files that store the generated dense random LPs
13-
14-
% read the directory to store the LPs
15-
pathName = input(['Please give the path where you want ' ...
16-
'to store the generated LPs: ']);
17-
% read the number of the LPs to generate
18-
nOfProblems = input(['Please give the number of the LPs ' ...
19-
'that you want to create: ']);
20-
% read the ranges of the values for matrix A
21-
Alu = input(['Please give the range of the values for ' ...
22-
'matrix A: ']);
23-
% read the ranges of the values for vector c
24-
clu = input(['Please give the range of the values for ' ...
25-
'vector c: ']);
26-
% read the type of optimization
27-
optType = input(['Please give the type of optimization ' ...
28-
'(0 min, 1 max, 2 random): ']);
29-
% read if all LPs will be optimal
30-
optimality = input(['Do you want all the LPs to be ' ...
31-
'optimal (1 yes, 2 no): ']);
32-
if optimality == 1 % optimal LPs
33-
% read the center of the circle
34-
center = input(['Please give the center of the circle: ']);
35-
% read the radius of the circle
36-
R = input(['Please give the radius of the circle ' ...
37-
'(must be less than the center of the circle: ']);
38-
while R >= center % read R
39-
R = input(['Please give the radius of the circle ' ...
40-
'(must be less than the center of the circle: ']);
41-
end
42-
% create nOfProblems dense random optimal LPs
43-
for i = 1:nOfProblems
44-
[A, c, b, Eqin, MinMaxLP] = denseRandomOptimal(m, n, ...
45-
optType, Alu, clu, center, R);
46-
s1 = num2str(m);
47-
s2 = num2str(n);
48-
k = num2str(i);
49-
fname = [pathName '/' 'fdata' k '_' s1 'x' s2];
50-
Name = ['fdata' k '_' s1 'x' s2];
51-
R = [];
52-
BS = [];
53-
NonZeros = nnz(A);
54-
c0 = 0;
55-
c00 = 0;
56-
% save variables in a MAT file
57-
eval(['save ' fname '.mat A c b Eqin MinMaxLP Name ' ...
58-
'R BS NonZeros c0 c00']);
59-
end
60-
else % not optimal LPs
61-
% read the ranges of the values for vector b
62-
blu = input(['Please give the range of the values for ' ...
63-
'vector b: ']);
64-
% read the ranges of the values for vector Eqin
65-
Eqinlu = input(['Please give the range of the values ' ...
66-
'for vector Eqin (0 - equality constraints, 1 - ' ...
67-
' less than or equal to inequality constraints, ' ...
68-
' 2 - greater than or equal to inequality ' ...
69-
'constraints: ']);
70-
for i = 1:nOfProblems % create nOfProblems dense random LPs
71-
[A, c, b, Eqin, MinMaxLP] = denseRandom(m, n, ...
72-
optType, Alu, clu, blu, Eqinlu);
73-
s1 = num2str(m);
74-
s2 = num2str(n);
75-
k = num2str(i);
76-
fname = [pathName '/' 'fdata' k '_' s1 'x' s2];
77-
Name = ['fdata' k '_' s1 'x' s2];
78-
R = [];
79-
BS = [];
80-
NonZeros = nnz(A);
81-
c0 = 0;
82-
c00 = 0;
83-
% save variables in a MAT file
84-
eval(['save ' fname '.mat A c b Eqin MinMaxLP Name ' ...
85-
'R BS NonZeros c0 c00']);
86-
end
87-
end
88-
end
1+
function denseMenu(m, n)
2+
% Filename: denseMenu.m
3+
% Description: Custom menu for creating dense random LPs
4+
% Authors: Ploskas, N., & Samaras, N.
5+
%
6+
% Syntax: denseMenu(m, n)
7+
%
8+
% Input:
9+
% -- m: the number of constraints of the generated random LPs
10+
% -- n: the number of variables of the generated random LPs
11+
%
12+
% Output: files that store the generated dense random LPs
13+
14+
% read the directory to store the LPs
15+
pathName = input(['Please give the path where you want ' ...
16+
'to store the generated LPs: ']);
17+
% read the number of the LPs to generate
18+
nOfProblems = input(['Please give the number of the LPs ' ...
19+
'that you want to create: ']);
20+
% read the ranges of the values for matrix A
21+
Alu = input(['Please give the range of the values for ' ...
22+
'matrix A: ']);
23+
% read the ranges of the values for vector c
24+
clu = input(['Please give the range of the values for ' ...
25+
'vector c: ']);
26+
% read the type of optimization
27+
optType = input(['Please give the type of optimization ' ...
28+
'(0 min, 1 max, 2 random): ']);
29+
% read if all LPs will be optimal
30+
optimality = input(['Do you want all the LPs to be ' ...
31+
'optimal (1 yes, 2 no): ']);
32+
if optimality == 1 % optimal LPs
33+
% read the center of the circle
34+
center = input(['Please give the center of the circle: ']);
35+
% read the radius of the circle
36+
R = input(['Please give the radius of the circle ' ...
37+
'(must be less than the center of the circle: ']);
38+
while R >= center % read R
39+
R = input(['Please give the radius of the circle ' ...
40+
'(must be less than the center of the circle: ']);
41+
end
42+
% create nOfProblems dense random optimal LPs
43+
for i = 1:nOfProblems
44+
[A, c, b, Eqin, MinMaxLP] = denseRandomOptimal(m, n, ...
45+
optType, Alu, clu, center, R);
46+
s1 = num2str(m);
47+
s2 = num2str(n);
48+
k = num2str(i);
49+
fname = [pathName '/' 'fdata' k '_' s1 'x' s2];
50+
Name = ['fdata' k '_' s1 'x' s2];
51+
R = [];
52+
BS = [];
53+
NonZeros = nnz(A);
54+
c0 = 0;
55+
c00 = 0;
56+
% save variables in a MAT file
57+
eval(['save ' fname '.mat A c b Eqin MinMaxLP Name ' ...
58+
'R BS NonZeros c0 c00']);
59+
end
60+
else % not optimal LPs
61+
% read the ranges of the values for vector b
62+
blu = input(['Please give the range of the values for ' ...
63+
'vector b: ']);
64+
% read the ranges of the values for vector Eqin
65+
Eqinlu = input(['Please give the range of the values ' ...
66+
'for vector Eqin (0 - equality constraints, 1 - ' ...
67+
' less than or equal to inequality constraints, ' ...
68+
' 2 - greater than or equal to inequality ' ...
69+
'constraints: ']);
70+
for i = 1:nOfProblems % create nOfProblems dense random LPs
71+
[A, c, b, Eqin, MinMaxLP] = denseRandom(m, n, ...
72+
optType, Alu, clu, blu, Eqinlu);
73+
s1 = num2str(m);
74+
s2 = num2str(n);
75+
k = num2str(i);
76+
fname = [pathName '/' 'fdata' k '_' s1 'x' s2];
77+
Name = ['fdata' k '_' s1 'x' s2];
78+
R = [];
79+
BS = [];
80+
NonZeros = nnz(A);
81+
c0 = 0;
82+
c00 = 0;
83+
% save variables in a MAT file
84+
eval(['save ' fname '.mat A c b Eqin MinMaxLP Name ' ...
85+
'R BS NonZeros c0 c00']);
86+
end
87+
end
88+
end

codes/chapter3/denseRandom.m

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
function [A, c, b, Eqin, MinMaxLP]= denseRandom(m, n, ...
2+
optType, Alu, clu, blu, Eqinlu)
3+
% Filename: denseRandom.m
4+
% Description: Creates dense random LPs
5+
% Authors: Ploskas, N., & Samaras, N.
6+
%
7+
% Syntax: [A, c, b, MinMaxLP, Eqin]= denseRandom(m, n, ...
8+
% minCard, Alu, clu, blu, Eqinlu)
9+
%
10+
% Input:
11+
% -- m: the number of constraints of the generated random
12+
% LPs
13+
% -- n: the number of variables of the generated random
14+
% LPs
15+
% -- optType: the type of optimization (0 min, 1 max, 2
16+
% random)
17+
% -- Alu: the ranges of the values for matrix A
18+
% (size 1 x 2)
19+
% -- clu: the ranges of the values for vector c
20+
% (size 1 x 2)
21+
% -- blu: the ranges of the values for vector b
22+
% (size 1 x 2)
23+
% -- Eqinlu: the ranges of the values for vector Eqin
24+
% (size 1 x 2)
25+
%
26+
% Output:
27+
% -- A: matrix of coefficients of the constraints
28+
% (size m x n)
29+
% -- c: vector of coefficients of the objective function
30+
% (size n x 1)
31+
% -- b: vector of the right-hand side of the constraints
32+
% (size m x 1)
33+
% -- Eqin: vector of the type of the constraints
34+
% (size m x 1)
35+
% -- MinMaxLP: the type of optimization
36+
37+
Al = Alu(1); % get the lower bound of A
38+
Au = Alu(2); % get the upper bound of A
39+
% create matrix A
40+
A = floor((Au - Al + 1) * rand(m, n)) + Al;
41+
cl = clu(1); % get the lower bound of c
42+
cu = clu(2); % get the upper bound of c
43+
% create vector c
44+
c = floor((cu - cl + 1) * rand(n, 1)) + cl;
45+
bl = blu(1); % get the upper bound of b
46+
bu = blu(2); % get the upper bound of c
47+
% create vector b
48+
b = floor((bu - bl + 1) * rand(m, 1)) + bl;
49+
eqinl = Eqinlu(1); % get the lower bound of Eqin
50+
eqinu = Eqinlu(2); % get the upper bound of Eqin
51+
% create vector Eqin
52+
Eqin = floor((eqinu - eqinl + 1) * rand(m, 1)) + eqinl;
53+
% less than or equal to inequality constraints
54+
Eqin(Eqin == 1) = -1;
55+
% greater than or equal to inequality constraints
56+
Eqin(Eqin == 2) = 1;
57+
if optType == 0 % minimization
58+
MinMaxLP = -1;
59+
elseif optType == 1 % maximization
60+
MinMaxLP = 1;
61+
else % pick random optimization type
62+
MinMaxLP = -1;
63+
if randi(2) < 2
64+
MinMaxLP = 1;
65+
end
66+
end
67+
end

codes/chapter3/denseRandomOptimal.m

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
function [A, c, b, Eqin, MinMaxLP] = ...
2+
denseRandomOptimal(m, n, optType, Alu, clu, center, R)
3+
% Filename: denseRandomOptimal.m
4+
% Description: Creates dense random optimal LPs
5+
% Authors: Ploskas, N., & Samaras, N.
6+
%
7+
% Syntax: [A, c, b, Eqin, MinMaxLP] = ...
8+
% denseRandomOptimal(m, n, optType, Alu, clu, center, R)
9+
%
10+
% Input:
11+
% -- m: the number of constraints of the generated random
12+
% LPs
13+
% -- n: the number of variables of the generated random
14+
% LPs
15+
% -- optType: the type of optimization (0 min, 1 max, 2
16+
% random)
17+
% -- Alu: the ranges of the values for matrix A
18+
% (size 1 x 2)
19+
% -- clu: the ranges of the values for vector c
20+
% (size 1 x 2)
21+
% -- center: the center of the circle
22+
% -- R: the radius of the circle
23+
24+
% Output:
25+
% -- A: matrix of coefficients of the constraints
26+
% (size m x n)
27+
% -- c: vector of coefficients of the objective function
28+
% (size n x 1)
29+
% -- b: vector of the right-hand side of the constraints
30+
% (size m x 1)
31+
% -- Eqin: vector of the type of the constraints
32+
% (size m x 1)
33+
% -- MinMaxLP: the type of optimization
34+
%
35+
cl = clu(1); % get the lower bound of c
36+
cu = clu(2); % get the upper bound of c
37+
% create vector c
38+
c = round((cu - cl + 1) * randn(n, 1)) + cl;
39+
Al = Alu(1); % get the lower bound of A
40+
Au = Alu(2); % get the upper bound of A
41+
A = zeros(m, n);
42+
b = zeros(m, 1);
43+
k = ones(1, n) * center;
44+
for i = 1:m % create matrix A and vector b
45+
% create a row of matrix A
46+
a = round((Au - Al + 1) * rand(1, n)) + Al;
47+
% calculate the hyperplane that is tangent
48+
% in a random point of the sphere
49+
y = k + r * (a / norm(a));
50+
b0 = a * y';
51+
b(i, 1) = b0; % add the point to vector b
52+
A(i, :) = a; % add the row to matrix A
53+
end
54+
% create vector Eqin
55+
Eqin(1:m, 1) = -1;
56+
if optType == 0 % minimization
57+
MinMaxLP = -1;
58+
elseif optType == 1 % maximization
59+
MinMaxLP = 1;
60+
else % pick random optimization type
61+
MinMaxLP = -1;
62+
if randi(2) < 2
63+
MinMaxLP = 1;
64+
end
65+
end
66+
end

codes/chapter3/example1_1.mps

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
NAME EXAMPLE1_1
2+
ROWS
3+
N OBJ
4+
G R1
5+
E R2
6+
L R3
7+
E R4
8+
G R5
9+
G R6
10+
L R7
11+
COLUMNS
12+
X1 R2 4
13+
X1 R3 -3
14+
X1 R4 4
15+
X1 R5 1
16+
X1 OBJ -2
17+
X2 R1 2
18+
X2 R2 -3
19+
X2 R3 2
20+
X2 OBJ 4
21+
X3 R1 3
22+
X3 R2 8
23+
X3 R4 -1
24+
X3 R6 1
25+
X3 R7 1
26+
X3 OBJ -2
27+
X4 R2 -1
28+
X4 R3 -4
29+
X4 R4 4
30+
X4 OBJ 2
31+
RHS
32+
RHS1 R1 6
33+
RHS1 R2 20
34+
RHS1 R3 -8
35+
RHS1 R4 18
36+
RHS1 R5 1
37+
RHS1 R6 2
38+
RHS1 R7 10
39+
BOUNDS
40+
FR BND1 X1
41+
FR BND1 X3
42+
ENDATA

0 commit comments

Comments
 (0)