2013-09-30 08:27:43 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
2013-06-20 19:54:09 +08:00
|
|
|
#include "precomp.hpp"
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
#include <climits>
|
|
|
|
#include <algorithm>
|
2013-07-11 01:11:52 +08:00
|
|
|
#include <cstdarg>
|
2013-06-25 01:27:11 +08:00
|
|
|
|
2014-08-14 16:50:07 +08:00
|
|
|
#define dprintf(x)
|
|
|
|
#define print_matrix(x)
|
|
|
|
|
|
|
|
namespace cv{
|
|
|
|
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
using std::vector;
|
2013-06-25 01:27:11 +08:00
|
|
|
|
2013-07-11 01:11:52 +08:00
|
|
|
#ifdef ALEX_DEBUG
|
2013-07-12 03:05:14 +08:00
|
|
|
static void print_simplex_state(const Mat& c,const Mat& b,double v,const std::vector<int> N,const std::vector<int> B){
|
|
|
|
printf("\tprint simplex state\n");
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-07-19 08:09:39 +08:00
|
|
|
printf("v=%g\n",v);
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-07-12 03:05:14 +08:00
|
|
|
printf("here c goes\n");
|
2013-07-19 08:09:39 +08:00
|
|
|
print_matrix(c);
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-07-12 03:05:14 +08:00
|
|
|
printf("non-basic: ");
|
2013-07-19 17:34:33 +08:00
|
|
|
print(Mat(N));
|
2013-07-12 03:05:14 +08:00
|
|
|
printf("\n");
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-07-12 03:05:14 +08:00
|
|
|
printf("here b goes\n");
|
2013-07-19 08:09:39 +08:00
|
|
|
print_matrix(b);
|
2013-07-12 03:05:14 +08:00
|
|
|
printf("basic: ");
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-07-19 17:34:33 +08:00
|
|
|
print(Mat(B));
|
2013-07-12 03:05:14 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
2013-07-11 17:29:55 +08:00
|
|
|
#else
|
2013-07-19 08:09:39 +08:00
|
|
|
#define print_simplex_state(c,b,v,N,B)
|
2013-07-11 01:11:52 +08:00
|
|
|
#endif
|
2013-07-03 18:54:23 +08:00
|
|
|
|
2013-07-11 01:11:52 +08:00
|
|
|
/**Due to technical considerations, the format of input b and c is somewhat special:
|
|
|
|
*both b and c should be one column bigger than corresponding b and c of linear problem and the leftmost column will be used internally
|
|
|
|
by this procedure - it should not be cleaned before the call to procedure and may contain mess after
|
|
|
|
it also initializes N and B and does not make any assumptions about their init values
|
|
|
|
* @return SOLVELP_UNFEASIBLE if problem is unfeasible, 0 if feasible.
|
|
|
|
*/
|
2013-07-20 20:14:02 +08:00
|
|
|
static int initialize_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow);
|
|
|
|
static inline void pivot(Mat_<double>& c,Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,int leaving_index,
|
|
|
|
int entering_index,vector<unsigned int>& indexToRow);
|
2013-07-11 01:11:52 +08:00
|
|
|
/**@return SOLVELP_UNBOUNDED means the problem is unbdd, SOLVELP_MULTI means multiple solutions, SOLVELP_SINGLE means one solution.
|
|
|
|
*/
|
2013-07-20 20:14:02 +08:00
|
|
|
static int inner_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow);
|
2013-07-11 14:52:13 +08:00
|
|
|
static void swap_columns(Mat_<double>& A,int col1,int col2);
|
2013-07-20 20:14:02 +08:00
|
|
|
#define SWAP(type,a,b) {type tmp=(a);(a)=(b);(b)=tmp;}
|
2013-07-03 18:54:23 +08:00
|
|
|
|
|
|
|
//return codes:-2 (no_sol - unbdd),-1(no_sol - unfsbl), 0(single_sol), 1(multiple_sol=>least_l2_norm)
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
int solveLP(const Mat& Func, const Mat& Constr, Mat& z){
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("call to solveLP\n"));
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-11 01:11:52 +08:00
|
|
|
//sanity check (size, type, no. of channels)
|
2013-07-12 03:05:14 +08:00
|
|
|
CV_Assert(Func.type()==CV_64FC1 || Func.type()==CV_32FC1);
|
|
|
|
CV_Assert(Constr.type()==CV_64FC1 || Constr.type()==CV_32FC1);
|
|
|
|
CV_Assert((Func.rows==1 && (Constr.cols-Func.cols==1))||
|
|
|
|
(Func.cols==1 && (Constr.cols-Func.rows==1)));
|
2013-07-03 18:54:23 +08:00
|
|
|
|
|
|
|
//copy arguments for we will shall modify them
|
2013-07-12 03:05:14 +08:00
|
|
|
Mat_<double> bigC=Mat_<double>(1,(Func.rows==1?Func.cols:Func.rows)+1),
|
2013-07-03 18:54:23 +08:00
|
|
|
bigB=Mat_<double>(Constr.rows,Constr.cols+1);
|
2013-07-12 03:05:14 +08:00
|
|
|
if(Func.rows==1){
|
|
|
|
Func.convertTo(bigC.colRange(1,bigC.cols),CV_64FC1);
|
|
|
|
}else{
|
2013-07-19 17:34:33 +08:00
|
|
|
Mat FuncT=Func.t();
|
|
|
|
FuncT.convertTo(bigC.colRange(1,bigC.cols),CV_64FC1);
|
2013-07-12 03:05:14 +08:00
|
|
|
}
|
|
|
|
Constr.convertTo(bigB.colRange(1,bigB.cols),CV_64FC1);
|
2013-07-03 18:54:23 +08:00
|
|
|
double v=0;
|
|
|
|
vector<int> N,B;
|
2013-07-20 20:14:02 +08:00
|
|
|
vector<unsigned int> indexToRow;
|
2013-07-03 18:54:23 +08:00
|
|
|
|
2013-07-20 20:14:02 +08:00
|
|
|
if(initialize_simplex(bigC,bigB,v,N,B,indexToRow)==SOLVELP_UNFEASIBLE){
|
2013-07-11 01:11:52 +08:00
|
|
|
return SOLVELP_UNFEASIBLE;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
Mat_<double> c=bigC.colRange(1,bigC.cols),
|
|
|
|
b=bigB.colRange(1,bigB.cols);
|
|
|
|
|
|
|
|
int res=0;
|
2013-07-20 20:14:02 +08:00
|
|
|
if((res=inner_simplex(c,b,v,N,B,indexToRow))==SOLVELP_UNBOUNDED){
|
2013-07-11 01:11:52 +08:00
|
|
|
return SOLVELP_UNBOUNDED;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
|
|
|
|
//return the optimal solution
|
2013-07-12 03:05:14 +08:00
|
|
|
z.create(c.cols,1,CV_64FC1);
|
2013-07-03 18:54:23 +08:00
|
|
|
MatIterator_<double> it=z.begin<double>();
|
2013-10-04 21:03:15 +08:00
|
|
|
unsigned int nsize = (unsigned int)N.size();
|
2013-07-03 18:54:23 +08:00
|
|
|
for(int i=1;i<=c.cols;i++,it++){
|
2013-10-04 21:03:15 +08:00
|
|
|
if(indexToRow[i]<nsize){
|
2013-07-03 18:54:23 +08:00
|
|
|
*it=0;
|
|
|
|
}else{
|
2013-10-04 21:03:15 +08:00
|
|
|
*it=b.at<double>(indexToRow[i]-nsize,b.cols-1);
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-07-20 20:14:02 +08:00
|
|
|
static int initialize_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow){
|
2013-07-03 18:54:23 +08:00
|
|
|
N.resize(c.cols);
|
|
|
|
N[0]=0;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
for (std::vector<int>::iterator it = N.begin()+1 ; it != N.end(); ++it){
|
|
|
|
*it=it[-1]+1;
|
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
B.resize(b.rows);
|
2013-10-04 21:03:15 +08:00
|
|
|
B[0]=(int)N.size();
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
for (std::vector<int>::iterator it = B.begin()+1 ; it != B.end(); ++it){
|
|
|
|
*it=it[-1]+1;
|
|
|
|
}
|
2013-07-20 20:14:02 +08:00
|
|
|
indexToRow.resize(c.cols+b.rows);
|
|
|
|
indexToRow[0]=0;
|
|
|
|
for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
|
|
|
|
*it=it[-1]+1;
|
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
v=0;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-03 18:54:23 +08:00
|
|
|
int k=0;
|
|
|
|
{
|
|
|
|
double min=DBL_MAX;
|
|
|
|
for(int i=0;i<b.rows;i++){
|
|
|
|
if(b(i,b.cols-1)<min){
|
|
|
|
min=b(i,b.cols-1);
|
|
|
|
k=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(b(k,b.cols-1)>=0){
|
|
|
|
N.erase(N.begin());
|
2013-07-20 20:14:02 +08:00
|
|
|
for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
|
|
|
|
--(*it);
|
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat_<double> old_c=c.clone();
|
|
|
|
c=0;
|
|
|
|
c(0,0)=-1;
|
|
|
|
for(int i=0;i<b.rows;i++){
|
|
|
|
b(i,0)=-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_simplex_state(c,b,v,N,B);
|
|
|
|
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("\tWE MAKE PIVOT\n"));
|
2013-07-20 20:14:02 +08:00
|
|
|
pivot(c,b,v,N,B,k,0,indexToRow);
|
2013-07-03 18:54:23 +08:00
|
|
|
|
|
|
|
print_simplex_state(c,b,v,N,B);
|
|
|
|
|
2013-07-20 20:14:02 +08:00
|
|
|
inner_simplex(c,b,v,N,B,indexToRow);
|
2013-07-03 18:54:23 +08:00
|
|
|
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("\tAFTER INNER_SIMPLEX\n"));
|
2013-07-03 18:54:23 +08:00
|
|
|
print_simplex_state(c,b,v,N,B);
|
|
|
|
|
2013-10-04 21:03:15 +08:00
|
|
|
unsigned int nsize = (unsigned int)N.size();
|
|
|
|
if(indexToRow[0]>=nsize){
|
|
|
|
int iterator_offset=indexToRow[0]-nsize;
|
2013-07-11 17:29:55 +08:00
|
|
|
if(b(iterator_offset,b.cols-1)>0){
|
2013-07-11 01:11:52 +08:00
|
|
|
return SOLVELP_UNFEASIBLE;
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
2013-07-20 20:14:02 +08:00
|
|
|
pivot(c,b,v,N,B,iterator_offset,0,indexToRow);
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 20:14:02 +08:00
|
|
|
vector<int>::iterator iterator;
|
2013-07-11 17:29:55 +08:00
|
|
|
{
|
2013-07-20 20:14:02 +08:00
|
|
|
int iterator_offset=indexToRow[0];
|
|
|
|
iterator=N.begin()+iterator_offset;
|
2013-07-11 17:29:55 +08:00
|
|
|
std::iter_swap(iterator,N.begin());
|
2013-07-20 20:14:02 +08:00
|
|
|
SWAP(int,indexToRow[*iterator],indexToRow[0]);
|
2013-07-11 17:29:55 +08:00
|
|
|
swap_columns(c,iterator_offset,0);
|
|
|
|
swap_columns(b,iterator_offset,0);
|
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("after swaps\n"));
|
2013-07-03 18:54:23 +08:00
|
|
|
print_simplex_state(c,b,v,N,B);
|
|
|
|
|
|
|
|
//start from 1, because we ignore x_0
|
|
|
|
c=0;
|
|
|
|
v=0;
|
2013-07-11 17:29:55 +08:00
|
|
|
for(int I=1;I<old_c.cols;I++){
|
2013-10-04 21:03:15 +08:00
|
|
|
if(indexToRow[I]<nsize){
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("I=%d from nonbasic\n",I));
|
2013-07-20 20:14:02 +08:00
|
|
|
int iterator_offset=indexToRow[I];
|
2013-08-27 17:57:24 +08:00
|
|
|
c(0,iterator_offset)+=old_c(0,I);
|
2013-07-03 18:54:23 +08:00
|
|
|
print_matrix(c);
|
|
|
|
}else{
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("I=%d from basic\n",I));
|
2013-10-04 21:03:15 +08:00
|
|
|
int iterator_offset=indexToRow[I]-nsize;
|
2013-07-11 17:29:55 +08:00
|
|
|
c-=old_c(0,I)*b.row(iterator_offset).colRange(0,b.cols-1);
|
|
|
|
v+=old_c(0,I)*b(iterator_offset,b.cols-1);
|
2013-07-03 18:54:23 +08:00
|
|
|
print_matrix(c);
|
|
|
|
}
|
|
|
|
}
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("after restore\n"));
|
2013-07-03 18:54:23 +08:00
|
|
|
print_simplex_state(c,b,v,N,B);
|
|
|
|
|
|
|
|
N.erase(N.begin());
|
2013-07-20 20:14:02 +08:00
|
|
|
for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
|
|
|
|
--(*it);
|
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-20 20:14:02 +08:00
|
|
|
static int inner_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow){
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
int count=0;
|
2013-07-30 09:14:36 +08:00
|
|
|
for(;;){
|
2013-07-11 19:43:48 +08:00
|
|
|
dprintf(("iteration #%d\n",count));
|
|
|
|
count++;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-11 01:11:52 +08:00
|
|
|
static MatIterator_<double> pos_ptr;
|
2013-07-03 18:54:23 +08:00
|
|
|
int e=-1,pos_ctr=0,min_var=INT_MAX;
|
|
|
|
bool all_nonzero=true;
|
|
|
|
for(pos_ptr=c.begin();pos_ptr!=c.end();pos_ptr++,pos_ctr++){
|
|
|
|
if(*pos_ptr==0){
|
|
|
|
all_nonzero=false;
|
|
|
|
}
|
|
|
|
if(*pos_ptr>0){
|
|
|
|
if(N[pos_ctr]<min_var){
|
|
|
|
e=pos_ctr;
|
|
|
|
min_var=N[pos_ctr];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(e==-1){
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("hello from e==-1\n"));
|
2013-07-03 18:54:23 +08:00
|
|
|
print_matrix(c);
|
|
|
|
if(all_nonzero==true){
|
2013-07-11 01:11:52 +08:00
|
|
|
return SOLVELP_SINGLE;
|
2013-07-03 18:54:23 +08:00
|
|
|
}else{
|
2013-07-11 01:11:52 +08:00
|
|
|
return SOLVELP_MULTI;
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
int l=-1;
|
2013-07-03 18:54:23 +08:00
|
|
|
min_var=INT_MAX;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
double min=DBL_MAX;
|
|
|
|
int row_it=0;
|
2013-07-03 18:54:23 +08:00
|
|
|
MatIterator_<double> min_row_ptr=b.begin();
|
|
|
|
for(MatIterator_<double> it=b.begin();it!=b.end();it+=b.cols,row_it++){
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
double myite=0;
|
2013-07-03 18:54:23 +08:00
|
|
|
//check constraints, select the tightest one, reinforcing Bland's rule
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
if((myite=it[e])>0){
|
|
|
|
double val=it[b.cols-1]/myite;
|
2013-07-03 18:54:23 +08:00
|
|
|
if(val<min || (val==min && B[row_it]<min_var)){
|
|
|
|
min_var=B[row_it];
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
min_row_ptr=it;
|
|
|
|
min=val;
|
|
|
|
l=row_it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(l==-1){
|
2013-07-11 01:11:52 +08:00
|
|
|
return SOLVELP_UNBOUNDED;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("the tightest constraint is in row %d with %g\n",l,min));
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-20 20:14:02 +08:00
|
|
|
pivot(c,b,v,N,B,l,e,indexToRow);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("objective, v=%g\n",v));
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
print_matrix(c);
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("constraints\n"));
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
print_matrix(b);
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("non-basic: "));
|
2013-07-19 17:34:33 +08:00
|
|
|
print_matrix(Mat(N));
|
|
|
|
dprintf(("basic: "));
|
|
|
|
print_matrix(Mat(B));
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-08-27 17:57:24 +08:00
|
|
|
static inline void pivot(Mat_<double>& c,Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,
|
2013-07-20 20:14:02 +08:00
|
|
|
int leaving_index,int entering_index,vector<unsigned int>& indexToRow){
|
2013-07-11 17:29:55 +08:00
|
|
|
double Coef=b(leaving_index,entering_index);
|
2013-07-03 18:54:23 +08:00
|
|
|
for(int i=0;i<b.cols;i++){
|
|
|
|
if(i==entering_index){
|
2013-07-11 17:29:55 +08:00
|
|
|
b(leaving_index,i)=1/Coef;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}else{
|
2013-07-11 17:29:55 +08:00
|
|
|
b(leaving_index,i)/=Coef;
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 18:54:23 +08:00
|
|
|
for(int i=0;i<b.rows;i++){
|
|
|
|
if(i!=leaving_index){
|
|
|
|
double coef=b(i,entering_index);
|
|
|
|
for(int j=0;j<b.cols;j++){
|
|
|
|
if(j==entering_index){
|
|
|
|
b(i,j)=-coef*b(leaving_index,j);
|
|
|
|
}else{
|
|
|
|
b(i,j)-=(coef*b(leaving_index,j));
|
|
|
|
}
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//objective function
|
2013-07-11 17:29:55 +08:00
|
|
|
Coef=c(0,entering_index);
|
2013-07-03 18:54:23 +08:00
|
|
|
for(int i=0;i<(b.cols-1);i++){
|
|
|
|
if(i==entering_index){
|
2013-07-11 17:29:55 +08:00
|
|
|
c(0,i)=-Coef*b(leaving_index,i);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}else{
|
2013-07-11 17:29:55 +08:00
|
|
|
c(0,i)-=Coef*b(leaving_index,i);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-11 17:29:55 +08:00
|
|
|
dprintf(("v was %g\n",v));
|
|
|
|
v+=Coef*b(leaving_index,b.cols-1);
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-07-20 20:14:02 +08:00
|
|
|
SWAP(int,N[entering_index],B[leaving_index]);
|
|
|
|
SWAP(int,indexToRow[N[entering_index]],indexToRow[B[leaving_index]]);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
2013-06-25 01:27:11 +08:00
|
|
|
|
2013-07-11 14:52:13 +08:00
|
|
|
static inline void swap_columns(Mat_<double>& A,int col1,int col2){
|
2013-07-03 18:54:23 +08:00
|
|
|
for(int i=0;i<A.rows;i++){
|
|
|
|
double tmp=A(i,col1);
|
|
|
|
A(i,col1)=A(i,col2);
|
|
|
|
A(i,col2)=tmp;
|
|
|
|
}
|
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|