Skip to content

Commit 5b42d2a

Browse files
author
ytsao
committed
[ADD] CMakeLists.txt
[MODIFIED] add variable, must to increase column index.
1 parent 875f23b commit 5b42d2a

5 files changed

Lines changed: 37 additions & 24 deletions

File tree

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
cmake_policy(SET CMP0135 NEW)
3+
4+
project(simplex
5+
HOMEPAGE_URL "https://github.com/ytsao/simplex-method.git"
6+
LANGUAGES CXX
7+
)
8+
9+
add_executable(simplex main.cpp)
10+
target_include_directories(simplex PRIVATE include)
11+
target_compile_features(simplex PRIVATE cxx_std_17)

include/SOPModel.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SOPModel {
2626
SOPVariables create_nonbasic(std::string name, char type, int id, double lb, double ub){
2727
SOPVariables new_var = SOPVariables(name, type, id, lb, ub);
2828
this->nonbasic.push_back(new_var);
29-
this->num_cols;
29+
this->num_cols++;
3030

3131
return new_var;
3232
}

include/SOPlpsolver.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class SOPlpSolve : public SOPSolver {
6161
}
6262

6363
bool primal_simplex(SOPModel& model) {
64+
this->p_iterations = 0;
6465
// step1. optimality test
6566
while (!optimality(model.get_header())) {
6667
/*std::cout << "finish step 1" << std::endl;*/
@@ -119,7 +120,6 @@ class SOPlpSolve : public SOPSolver {
119120
// step 7. compute dual step length
120121
// s = z_star_j / delta_z_j
121122
double s = model.get_header()(entering_index) / delta_z_N[entering_index];
122-
/*std::cout << "finish step 7" << std::endl;*/
123123

124124
// step 8. update current primal and dual solution
125125
for (int i = 0; i < model.get_num_rows(); ++i) {
@@ -155,13 +155,16 @@ class SOPlpSolve : public SOPSolver {
155155
/*std::cout << "entering variable index:" << entering_index << ", name: " << entering_variable.get_var_name() << std::endl;*/
156156
/*std::cout << "leaving variable index:" << leaving_index << ", name: " << leaving_variable.get_var_name() << std::endl;*/
157157
/*std::cout << "finish step 9" << std::endl;*/
158-
/**/
158+
159159
this->p_iterations++;
160160
}
161+
std::cout << "number of iterations: " << this->p_iterations << std::endl;
162+
161163
return true;
162164
}
163165

164166
bool dual_simplex(SOPModel& model) {
167+
this->d_iterations = 0;
165168
// step 1. optimality test
166169
while (!optimality(model.get_rhs())) {
167170
/*std::cout << "finish step 1" << std::endl;*/
@@ -255,6 +258,8 @@ class SOPlpSolve : public SOPSolver {
255258

256259
this->d_iterations++;
257260
}
261+
std::cout << "number of iterations: " << this->d_iterations << std::endl;
262+
258263
return true;
259264
}
260265

main

64 Bytes
Binary file not shown.

main.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,46 @@ int main() {
1111
// create decision variables
1212
SOPVariables x1 = model.create_nonbasic("x1", 'F', 0, 0, INT_MAX);
1313
SOPVariables x2 = model.create_nonbasic("x2", 'F', 1, 0, INT_MAX);
14-
SOPVariables x3 = model.create_nonbasic("x3", 'F', 2, 0, INT_MAX);
1514

16-
/*std::cout << x1.get_var_name() << std::endl;*/
17-
/*std::cout << x2.get_var_name() << std::endl;*/
15+
std::cout << x1.get_var_name() << std::endl;
16+
std::cout << x2.get_var_name() << std::endl;
1817

1918
// create objective function
2019
SOPObjective obj;
21-
obj.addTerm(3, x1);
22-
obj.addTerm(6, x2);
23-
obj.addTerm(0, x3);
24-
model.addMin(obj);
20+
obj.addTerm(-2, x1);
21+
obj.addTerm(-1, x2);
22+
model.addMax(obj);
2523

26-
/*std::cout << model.get_header() << std::endl;*/
24+
std::cout << "header: " << model.get_header() << std::endl;
2725

2826
// create bunch of constraints
29-
expr.addTerm(-1, x1);
30-
expr.addTerm(2, x2);
31-
expr.addTerm(-1, x3);
32-
model.addLe(expr, -6, "cons1");
33-
34-
expr.addTerm(-2, x1);
27+
expr.addTerm(-3, x1);
3528
expr.addTerm(-1, x2);
36-
expr.addTerm(2, x3);
37-
model.addLe(expr, -8, "cons2");
29+
model.addLe(expr, -3, "cons1");
3830

39-
expr.addTerm(0, x1);
40-
expr.addTerm(1, x2);
41-
model.addLe(expr, 5, "cons3");
31+
expr.addTerm(-4, x1);
32+
expr.addTerm(-3, x2);
33+
model.addLe(expr, -6, "cons2");
34+
35+
expr.addTerm(-1, x1);
36+
expr.addTerm(-2, x2);
37+
model.addLe(expr, -3, "cons3");
4238

4339
model.create_basis();
4440
std::cout << "basis: " << model.get_basis() << std::endl;
4541
std::cout << "all ranges: " << model.get_all_range() << std::endl;
4642
std::cout << "N: " << model.get_N() << std::endl;
4743
std::cout << "rhs: " << model.get_rhs() << std::endl;
4844

49-
for (int i = 0; i < 2; i++) std::cout << "variable index: " << model.get_basic(i).get_var_id() << std::endl;
45+
for (int i = 0; i < 2; i++) std::cout << "basic variable index: " << model.get_basic(i).get_var_id() << std::endl;
5046

5147
#pragma endregion
5248

5349
#pragma region solve linear program
5450
SOPlpSolve lpsolver;
55-
lpsolver.solve(model, 1);
51+
lpsolver.solve(model, 2);
5652
std::cout << "after solving, rhs: " << model.get_rhs() << std::endl;
53+
for (int i = 0; i < 2; i++) std::cout << "basic variable index: " << model.get_basic(i).get_var_id() << std::endl;
5754
#pragma endregion
5855

5956
std::cout << "finish" << std::endl;

0 commit comments

Comments
 (0)