// // $Source: /cvsroot/gambit/gambit/sources/tools/lp/btableau.imp,v $ // $Date: 2006/01/07 06:37:34 $ // $Revision: 1.6 $ // // DESCRIPTION: // Implementation of base tableau classes // // This file is part of Gambit // Copyright (c) 2002, The Gambit Project // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // --------------------------------------------------------------------------- // BaseTableau method definitions // --------------------------------------------------------------------------- #include "btableau.h" template bool BaseTableau::ColIndex(int x) const { return MinCol()<=x && x<=MaxCol(); } template bool BaseTableau::RowIndex(int x) const { return MinRow()<=x && x<=MaxRow(); } template bool BaseTableau::ValidIndex(int x) const { return (ColIndex(x) || RowIndex(-x)); } template void BaseTableau::CompPivot(int outlabel, int col) { Pivot(Find(outlabel),col); Pivot(Find(-col),-outlabel); } // --------------------------------------------------------------------------- // TableauInterface method definitions // --------------------------------------------------------------------------- // Constructors and Destructor template TableauInterface::TableauInterface(const Gambit::Matrix &A, const Gambit::Vector &b) : A(&A), b(&b), basis(A.MinRow(),A.MaxRow(),A.MinCol(),A.MaxCol()), solution(A.MinRow(),A.MaxRow()), npivots(0), artificial(A.MaxCol()+1,A.MaxCol()) { // These are the values recommended by Murtagh (1981) for 15 digit // accuracy in LP problems // Note: for gRational, eps1 and eps2 resolve to 0 Gambit::Epsilon(eps1,5); Gambit::Epsilon(eps2); } template TableauInterface::TableauInterface(const Gambit::Matrix &A, const Gambit::Array &art, const Gambit::Vector &b) : A(&A), b(&b), basis(A.MinRow(),A.MaxRow(),A.MinCol(),A.MaxCol()+art.Length()), solution(A.MinRow(),A.MaxRow()), npivots(0), artificial(A.MaxCol()+1,A.MaxCol()+art.Length()) { Gambit::Epsilon(eps1,5); Gambit::Epsilon(eps2); for(int i = 0;i TableauInterface::TableauInterface(const TableauInterface &orig) : A(orig.A), b(orig.b), basis(orig.basis), solution(orig.solution), npivots(orig.npivots), eps1(orig.eps1), eps2(orig.eps2), artificial(orig.artificial) { } template TableauInterface::~TableauInterface() { } template TableauInterface& TableauInterface::operator=(const TableauInterface &orig) { if(this!= &orig) { A = orig.A; b = orig.b; basis= orig.basis; solution= orig.solution; npivots = orig.npivots; artificial = orig.artificial; } return *this; } // getting information template int TableauInterface::MinRow() const { return A->MinRow(); } template int TableauInterface::MaxRow() const { return A->MaxRow(); } template int TableauInterface::MinCol() const { return basis.MinCol(); } template int TableauInterface::MaxCol() const { return basis.MaxCol(); } template Basis & TableauInterface::GetBasis(void) {return basis; } template const Gambit::Matrix & TableauInterface::Get_A(void) const {return *A; } template const Gambit::Vector & TableauInterface::Get_b(void) const {return *b;} template bool TableauInterface::Member(int i) const { return basis.Member(i);} template int TableauInterface::Label(int i) const { return basis.Label(i);} template int TableauInterface::Find(int i) const { return basis.Find(i);} template long TableauInterface::NumPivots() const { return npivots; } template long &TableauInterface::NumPivots() { return npivots; } template void TableauInterface::Mark(int label) {basis.Mark(label);} template void TableauInterface::UnMark(int label) {basis.UnMark(label);} template bool TableauInterface::IsBlocked(int label) const { return basis.IsBlocked(label); } template void TableauInterface::GetColumn(int col, Gambit::Vector &ret) const { if(IsArtifColumn(col)) { ret = (T) 0; ret[artificial[col]] = (T)1; } else if(basis.IsRegColumn(col)) A->GetColumn(col, ret); else if (basis.IsSlackColumn(col)) { ret = (T) 0; ret[-col] = (T) 1; } } template void TableauInterface::GetBasis(Basis &out) const { out= basis; } template BFS TableauInterface::GetBFS() { Gambit::Vector sol(basis.First(),basis.Last()); BasisVector(sol); BFS cbfs((T) 0); for(int i=MinCol();i<=MaxCol();i++) if(Member(i)) cbfs.Define(i,sol[basis.Find(i)]); return cbfs; } template BFS TableauInterface::GetBFS1() const { Gambit::Vector sol(basis.First(),basis.Last()); BasisVector(sol); BFS cbfs((T) 0); int i; for(i=-MaxRow();i<=-MinRow();i++) if(Member(i)) cbfs.Define(i,sol[basis.Find(i)]); for(i=MinCol();i<=MaxCol();i++) if(Member(i)) cbfs.Define(i,sol[basis.Find(i)]); return cbfs; } // miscellaneous functions template bool TableauInterface::EqZero(T x) const { return (LeZero(x) && GeZero(x)); } template bool TableauInterface::LtZero(T x) const { return !GeZero(x); } template bool TableauInterface::GtZero(T x) const { return !LeZero(x); } template bool TableauInterface::LeZero(T x) const { if(x <=eps2) return 1; return 0; } template bool TableauInterface::GeZero(T x) const { if(x >= -eps2) return 1; return 0; } template T TableauInterface::Epsilon(int i) const { if(i!=1 && i!=2) { throw Gambit::IndexException(); } if(i==1) return eps1; return eps2; } template bool TableauInterface::IsArtifColumn(int col) const { return col>=artificial.First() && col<=artificial.Last(); } /* template BaseTableau::BadPivot::~BadPivot() { } template gText BaseTableau::BadPivot::Description(void) const { return "Bad Pivot in BaseTableau"; } */