Program Listing for File solver.h

Return to documentation for file (src/popf/solver.h)

/************************************************************************
 * Copyright 2010, Strathclyde Planning Group,
 * Department of Computer and Information Sciences,
 * University of Strathclyde, Glasgow, UK
 * http://planning.cis.strath.ac.uk/
 *
 * Andrew Coles, Amanda Coles - Code for POPF
 * Maria Fox, Richard Howey and Derek Long - Code from VAL
 * Stephen Cresswell - PDDL Parser
 *
 * This file is part of the planner POPF.
 *
 * POPF 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.
 *
 * POPF 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 POPF.  If not, see <http://www.gnu.org/licenses/>.
 *
 ************************************************************************/

#ifndef SOLVER_H
#define SOLVER_H

#include <string>
#include <vector>
#include <utility>
#include <list>
#include <map>
using std::string;
using std::vector;
using std::pair;
using std::list;
using std::map;

extern void readParams(char * argv[], const int & a);

class MILPSolver {


    public:

        class Objective {

        public:
            struct Coefficient {
                double linearCoefficient;
                map<int,double> nonLinearCoefficients;

                Coefficient()
                : linearCoefficient(0.0)
                {
                }


            };

        protected:
            map<int, Coefficient> terms;
            bool _maximise;
        public:

            typedef map<int, Coefficient>::const_iterator const_iterator;

            Objective(const bool maxObj=false)
                : _maximise(maxObj) {
            }

            void setMaximise(const bool & m) {
                _maximise = m;
            }

            inline const bool & maximise() const {
                return _maximise;
            }

            Coefficient & getTerm(const int & v) {
                return terms.insert(std::make_pair(v, Coefficient())).first->second;
            }

            const_iterator begin() const {
                return terms.begin();
            }

            const_iterator end() const {
                return terms.end();
            }

            size_t size() const {
                return terms.size();
            }
        };

        enum ColumnType {
            C_REAL = 1,
            C_INT = 2,
            C_BOOL = 3
        };

    protected:

        MILPSolver() {
        }

        Objective quadraticObjective;

    public:

        virtual ~MILPSolver() {
        }

        virtual MILPSolver * clone() = 0;

        virtual double getInfinity() = 0;



        virtual void addRow(const vector<pair<int,double> > & entries, const double & lb, const double & ub) = 0;

        virtual void setRowName(const int & cons, const string & asString) = 0;
        virtual string getRowName(const int & cons) = 0;

        virtual double getRowUpper(const int & var) = 0;
        virtual void setRowUpper(const int & c, const double & b) = 0;
        virtual double getRowLower(const int & var) = 0;
        virtual void setRowLower(const int & c, const double & b) = 0;

        virtual int getNumRows() = 0;


        virtual void addCol(const vector<pair<int,double> > & entries, const double & lb, const double & ub, const ColumnType & type) = 0;

        virtual bool isColumnInteger(const int & c) = 0;

        virtual bool isColumnBinary(const int & c) {
            return (isColumnInteger(c) && (getColLower(c) == 0) && (getColUpper(c) == 1));
        }

        virtual void setColName(const int & var, const string & asString) = 0;
        virtual string getColName(const int & var) = 0;

        virtual double getColUpper(const int & var) = 0;
        virtual void setColUpper(const int & var, const double & b) = 0;
        virtual double getColLower(const int & var) = 0;
        virtual void setColLower(const int & var, const double & b) = 0;
        virtual void setColBounds(const int & var, const double & lb, const double & ub) = 0;

        virtual int getNumCols() = 0;


        virtual void addEmptyRealCols(const int & n) {
            static const vector<pair<int,double> > emptyEntries;

            for (int i = 0; i < n; ++i) {
                addCol(emptyEntries, 0.0, getInfinity(), C_REAL);
            }
        }




        virtual void setMaximiseObjective(const bool & maxim) = 0;


        virtual void setObjective(double * const entries) = 0;

        virtual void setQuadraticObjective(const Objective & o) {
            quadraticObjective = o;
        }

        virtual void setObjCoeff(const int & var, const double & w) = 0;

        virtual void clearObjective() = 0;



        virtual bool solve(const bool & skipPresolve) = 0;

        virtual bool quadraticPreSolve();

        virtual const double * getSolution() = 0;

        virtual const double * getPartialSolution(const int & from, const int & to) = 0;

        virtual double getSingleSolutionVariableValue(const int & col) {
            return getSolution()[col];
        }

        virtual const double * getSolutionRows() = 0;

        virtual double getSingleSolutionRowValue(const int & row) {
            return getSolutionRows()[row];
        }

        virtual double getObjValue() = 0;


        virtual void writeLp(const string & filename) = 0;

        virtual void getRow(const int & i, vector<pair<int,double> > & entries) = 0;

        virtual void hush() = 0;


};

extern MILPSolver * getNewSolver();
extern const double LPinfinity;

#endif