Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
Model.hpp
Go to the documentation of this file.
1 /*
2  * $Id: Model.hpp 225 2014-11-17 17:52:40Z jdl3 $
3  * Copyright (C) 2011–2013, 2014 John D Lamb
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
299 #ifndef IPO_MODEL_HPP
300 #define IPO_MODEL_HPP
301 
302 #include<map>
303 #include"Objective.hpp"
304 #include"Constraint.hpp"
305 #include"../ipo_function/DerivativesEstimates.hpp"
306 #include"detail/NewtonDescent.hpp"
307 #include"../ipo_function/concrete/Null.hpp"
308 #include"LinearConstraint.hpp"
309 
313 namespace ipo {
314  // Forward declarations
315  class LinearConstraint;
316  namespace detail {
317  class BarrierFunction;
318  class PhaseIModel;
319  }
325  class Model : public detail::ModelBase {
326  // Classes used internally to get subvectors of a vector
327  public:
328  class Parameters {
329  public:
333  Parameters();
338  double getMu() const { return mu; }
343  double getEpsilon() const { return epsilon; }
348  std::ostream* getOutputStream() const { return outputStream; }
353  void setMu( double const mu );
358  void setEpsilon( double const epsilon );
363  void setOutputStream( std::ostream* outputStream );
364  private:
377  double mu;
386  double epsilon;
393  std::ostream* outputStream;
394  };
395  private:
400 #ifdef TESTSUBVECTOR
401  public:
402 #endif
403 
408  class Subvector {
409  public:
419  virtual gsl::vector const get( gsl::vector const& vector ) const = 0;
426  virtual void set( gsl::vector& vector, gsl::vector const& subvector,
427  double const scale = 1.0 ) const = 0;
434  virtual void add( gsl::vector& vector, gsl::vector const& subvector,
435  double const scale = 1.0 ) const = 0;
442  virtual void set( gsl::matrix& matrix, gsl::matrix const& submatrix,
443  double const scale = 1.0 ) const = 0;
451  virtual void add( gsl::matrix& matrix, gsl::matrix const& submatrix,
452  double const scale = 1.0 ) const = 0;
460  virtual void set( gsl::matrix& matrix, gsl::vector const& vector,
461  double const scale = 1.0 ) const = 0;
469  virtual void add( gsl::matrix& matrix, gsl::vector const& vector,
470  double const scale = 1.0 ) const = 0;
471  };
475  class DirectSubvector : public Subvector {
476  public:
482  DirectSubvector( size_t const offset, size_t const length );
489  virtual gsl::vector const get( gsl::vector const& vector ) const {
490  return vector.subvector( offset, length ); }
497  virtual void set( gsl::vector& vector, gsl::vector const& subvector,
498  double const scale = 1.0 ) const {
499  for( size_t i { 0 }; i < length; ++i ) vector[i + offset] = scale * subvector[i]; }
506  virtual void add( gsl::vector& vector, gsl::vector const& subvector,
507  double const scale = 1.0 ) const {
508  for( size_t i { 0 }; i < length; ++i ) vector[i + offset] += scale * subvector[i]; }
515  virtual void set( gsl::matrix& matrix, gsl::matrix const& submatrix,
516  double const scale = 1.0 ) const {
517  for( size_t i { 0 }; i < length; ++i )
518  for( size_t j { i }; j < length; ++j ){
519  double const d { scale * submatrix.get( i, j ) };
520  matrix.set( i + offset, j + offset, d );
521  if( j > i ) matrix.set( j + offset, i + offset, d );
522  }
523  }
531  virtual void add( gsl::matrix& matrix, gsl::matrix const& submatrix,
532  double const scale = 1 ) const {
533  for( size_t i = 0; i < length; ++i )
534  for( size_t j = i; j < length; ++j ){
535  double const d = scale * submatrix.get( i, j )
536  + matrix.get( i + offset, j + offset );
537  matrix.set( i + offset, j + offset, d );
538  if( j > i ) matrix.set( j + offset, i + offset, d );
539  }
540  }
548  virtual void set( gsl::matrix& matrix, gsl::vector const& vector,
549  double const scale = 1 ) const {
550  for( size_t i = 0; i < length; ++i )
551  for( size_t j = i; j < length; ++j ){
552  double const d = scale * vector[i] * vector[j];
553  matrix.set( i + offset, j + offset, d );
554  if( j > i ) matrix.set( j + offset, i + offset, d );
555  }
556  }
564  virtual void add( gsl::matrix& matrix, gsl::vector const& vector,
565  double const scale = 1.0 ) const {
566  for( size_t i { 0 }; i < length; ++i )
567  for( size_t j { i }; j < length; ++j ){
568  double const d { scale * vector[i] * vector[j]
569  + matrix.get( i + offset, j + offset ) };
570  matrix.set( i + offset, j + offset, d );
571  if( j > i ) matrix.set( j + offset, i + offset, d );
572  }
573  }
574  private:
578  size_t const offset;
582  size_t const length;
583  };
587  class IndirectSubvector : public Subvector, public std::vector<size_t> {
588  public:
594  IndirectSubvector( size_t const size );
601  virtual gsl::vector const get( gsl::vector const& vector ) const {
602  gsl::vector subvector( size() );
603  for( size_t index { 0 }; index < size(); ++index )
604  subvector[index] = vector[(*this)[index]];
605  return subvector;
606  }
613  virtual void set( gsl::vector& vector, gsl::vector const& subvector,
614  double const scale = 1.0 ) const {
615  for( size_t index { 0 }; index < size(); ++index )
616  vector[(*this)[index]] = scale * subvector[index]; }
623  virtual void add( gsl::vector& vector, gsl::vector const& subvector,
624  double const scale = 1.0 ) const {
625  for( size_t index { 0 }; index < size(); ++index )
626  vector[(*this)[index]] += scale * subvector[index]; }
633  virtual void set( gsl::matrix& matrix, gsl::matrix const& submatrix,
634  double const scale = 1.0 ) const {
635  for( size_t i { 0 }; i < size(); ++i )
636  for( size_t j = i; j < size(); ++j ){
637  size_t const r { (*this)[i] };
638  size_t const c { (*this)[j] };
639  double const d { scale * submatrix.get( i , j ) };
640  matrix.set( r, c, d );
641  if( j > i ) matrix.set( c, r, d );
642  }
643  }
651  virtual void add( gsl::matrix& matrix, gsl::matrix const& submatrix,
652  double const scale = 1 ) const {
653  for( size_t i = 0; i < size(); ++i )
654  for( size_t j = i; j < size(); ++j ){
655  size_t const r = (*this)[i];
656  size_t const c = (*this)[j];
657  double const d = scale * submatrix.get( i , j ) + matrix.get( r, c );
658  matrix.set( r, c, d );
659  if( j > i ) matrix.set( c, r, d );
660  }
661  }
669  virtual void set( gsl::matrix& matrix, gsl::vector const& vector,
670  double const scale = 1.0 ) const {
671  for( size_t i { 0 }; i < size(); ++i )
672  for( size_t j { i }; j < size(); ++j ){
673  size_t const r { (*this)[i] };
674  size_t const c { (*this)[j] };
675  double const d { scale * vector[i] * vector[j] };
676  matrix.set( r, c, d );
677  if( j > i ) matrix.set( c, r, d );
678  }
679  }
687  virtual void add( gsl::matrix& matrix, gsl::vector const& vector,
688  double const scale = 1.0 ) const {
689  for( size_t i { 0 }; i < size(); ++i )
690  for( size_t j = i; j < size(); ++j ){
691  size_t const r { (*this)[i] };
692  size_t const c { (*this)[j] };
693  double const d { scale * vector[i] * vector[j] + matrix.get( r, c ) };
694  matrix.set( r, c, d );
695  if( j > i ) matrix.set( c, r, d );
696  }
697  }
698  };
700  friend class detail::PhaseIModel;
701  public:
705  Model();
710  virtual void notify();
725  void addConstraint( Constraint& constraint );
732  bool removeConstraint( Constraint& constraint );
737  std::vector<Constraint> const& getConstraints() const;
742  std::vector<LinearConstraint> const& getEqualityConstraints() const;
747  bool isFeasible();
753  bool isStrictlyFeasible();
758  bool minimise(){ return optimise( true ); }
763  bool maximise(){ return optimise( false ); }
771  virtual bool findFeasible();
776  Parameters& getParameters();
800  bool checkSize() const;
807  void testSizes() const;
812  void summary( std::ostream& ostream = std::cout ) const;
813  protected:
818  gsl::vector getVectorFromVariables() const;
824  void setVariablesFromVector( gsl::vector const & vector );
830  bool stoppingCriterion( double const t ) const;
836  virtual bool optimise( bool const minimise );
843  virtual void setIndices();
847  inline void setObjectiveIndices(){
848  size_t const OSIZE { objective.getVariables().size() }; /* identifies vars not in
849  * objective */
850  bool const DIRECTSUBVECTORFOROBJECTIVEINDICES { OSIZE == indexMap.size() };
851  // Set objectiveIndices
852  if( DIRECTSUBVECTORFOROBJECTIVEINDICES )
853  objectiveIndices = std::shared_ptr<Subvector>( new DirectSubvector( 0, OSIZE ) );
854  else {
855  IndirectSubvector* oIndices = new IndirectSubvector( OSIZE );
856  objectiveIndices = std::shared_ptr<Subvector>( oIndices );
857  size_t index { 0 };
858  for( auto& variable : objective.getVariables() ){
859  (*oIndices)[index] = indexMap[variable];
860  ++index;
861  }
862  }
863  }
867  void setConstraintIndices(); // local function
868  protected:
872  std::vector<Constraint> constraints;
876  std::vector<ipo::LinearConstraint> equalityConstraints;
886  std::shared_ptr<Subvector> objectiveIndices;
892  std::vector<std::shared_ptr<Subvector> > constraintIndices;
898  std::vector<std::shared_ptr<Subvector> > equalityConstraintIndices;
903  std::map<Variable,size_t> indexMap;
908  bool variablesChanged = false;
917  };
918 }
919 
920 #endif
std::map< Variable, size_t > indexMap
Used internally to match the variables in each variable to indices in a vector.
Definition: Model.hpp:903
Model used for finding an initial feasible solution in phase I of interior-pont optimisation.
Definition: PhaseIModel.hpp:36
double getMu() const
Get value of mu.
Definition: Model.hpp:338
std::vector< LinearConstraint > const & getEqualityConstraints() const
Get list of equality constraints.
Definition: Model.cc:87
virtual void setIndices()
Sets the values of objectiveIndices, constraintIndices and variableIndices from constraints.
Definition: Model.cc:92
std::vector< ipo::LinearConstraint > equalityConstraints
The set of equality constraints.
Definition: Model.hpp:876
virtual void set(gsl::vector &vector, gsl::vector const &subvector, double const scale=1.0) const =0
Set vector from a subvector.
This class gives direct access to a subvector of a vector.
Definition: Model.hpp:475
detail::LineSearch::Parameters & getLineSearchParameters()
Get line search parameters by reference.
Definition: Model.cc:336
bool minimise()
Try to minimise the objective function subject to the constraints.
Definition: Model.hpp:758
virtual void set(gsl::vector &vector, gsl::vector const &subvector, double const scale=1.0) const
Set vector from a subvector using indices without checking bounds.
Definition: Model.hpp:613
detail::LineSearch::Parameters lineSearchParameters
Parameters used by backtracking line search algorithm during optimisation.
Definition: Model.hpp:916
Objective getObjective()
Get the objective function.
Definition: Model.cc:43
virtual void set(gsl::matrix &matrix, gsl::matrix const &submatrix, double const scale=1.0) const
Set matrix from a submatrix without checking bounds.
Definition: Model.hpp:633
double mu
A value used for scaling: the default is 10.
Definition: Model.hpp:377
bool variablesChanged
Value that indicates if any variables, objective or constraints have been modified in such a way that...
Definition: Model.hpp:908
void testSizes() const
Check objective and constraint functions.
Definition: Model.cc:351
std::vector< std::shared_ptr< Subvector > > equalityConstraintIndices
Used internally to match the variables in each equality constraint to indices in a vector: (*constrai...
Definition: Model.hpp:898
std::ostream * getOutputStream() const
Get output stream.
Definition: Model.hpp:348
This class gives direct access to a subvector of a vector.
Definition: Model.hpp:587
detail::NewtonDescent::Parameters newtonDescentParameters
Parameters used by Newton descent algorithm during optimisation.
Definition: Model.hpp:912
virtual void set(gsl::matrix &matrix, gsl::vector const &vector, double const scale=1) const
Set matrix from a vector without checking bounds.
Definition: Model.hpp:548
detail::NewtonDescent::Parameters & getNewtonDescentParameters()
Get Newton descent parameters by reference.
Definition: Model.cc:331
size_type size() const
Get size of array.
Definition: Array.hpp:400
virtual void add(gsl::vector &vector, gsl::vector const &subvector, double const scale=1.0) const
Add to vector from a subvector without checking bounds.
Definition: Model.hpp:506
Array & getVariables()
Get variables used by Objective function.
Definition: Objective.cc:75
Objective objective
The objective function.
Definition: Model.hpp:880
Abstract base class for subvector classes.
Definition: Model.hpp:408
std::vector< Constraint > const & getConstraints() const
Get list of constraints.
Definition: Model.cc:82
bool isStrictlyFeasible()
Test whether current values of variables give a feasible solution.
Definition: Model.cc:175
void setVariablesFromVector(gsl::vector const &vector)
Set variable values from a vector.
Definition: Model.cc:224
std::vector< Constraint > constraints
The set of constraints.
Definition: Model.hpp:872
Class for a constraint function.
Definition: Constraint.hpp:40
double epsilon
A limit used for convergence.
Definition: Model.hpp:386
Model an interior-point optimisation problem.
Definition: Model.hpp:325
virtual void notify()
Notify function that model must implement.
Definition: Model.cc:130
std::ostream * outputStream
A stream (default is nullptr) for sending output to.
Definition: Model.hpp:393
Abstract base class for model.
Definition: Var.hpp:39
DirectSubvector(size_t const offset, size_t const length)
The constructor needs an offset and a length.
Definition: Model.cc:31
bool stoppingCriterion(double const t) const
Check whether or not stopping criterion has been met.
Definition: Model.cc:238
Class for an objective function.
Definition: Objective.hpp:37
virtual void add(gsl::matrix &matrix, gsl::vector const &vector, double const scale=1.0) const
Add to matrix from a vector without checking bounds.
Definition: Model.hpp:564
virtual void set(gsl::matrix &matrix, gsl::vector const &vector, double const scale=1.0) const
Set matrix from a vector without checking bounds.
Definition: Model.hpp:669
void summary(std::ostream &ostream=std::cout) const
Create a summary of this model.
Definition: Model.cc:391
virtual void add(gsl::matrix &matrix, gsl::matrix const &submatrix, double const scale=1) const
Add to matrix from a submatrix without checking bounds.
Definition: Model.hpp:651
gsl::vector getVectorFromVariables() const
Get a vector from values of variables.
Definition: Model.cc:214
Model()
Constructor.
Definition: Model.cc:132
size_t const length
The length.
Definition: Model.hpp:582
size_t const offset
The offset.
Definition: Model.hpp:578
Parameters & getParameters()
Get parameters by reference.
Definition: Model.cc:341
virtual void add(gsl::matrix &matrix, gsl::vector const &vector, double const scale=1.0) const
Add to matrix from a vector without checking bounds.
Definition: Model.hpp:687
void setObjectiveIndices()
Set objectiveIndices.
Definition: Model.hpp:847
virtual void set(gsl::vector &vector, gsl::vector const &subvector, double const scale=1.0) const
Set vector from a subvector without checking bounds.
Definition: Model.hpp:497
virtual void add(gsl::vector &vector, gsl::vector const &subvector, double const scale=1.0) const
Add to vector from a using indices without checking bounds.
Definition: Model.hpp:623
virtual void add(gsl::matrix &matrix, gsl::matrix const &submatrix, double const scale=1) const
Add to matrix from a submatrix without checking bounds.
Definition: Model.hpp:531
bool checkSize() const
Check objective and constraint functions.
Definition: Model.cc:343
Function for the logarithmic barrier.
void setConstraintIndices()
Set constraintIndices.
Definition: Model.cc:429
void setObjective(Objective &objective)
Set the objective function.
Definition: Model.cc:38
Line search parameters.
Definition: LineSearch.hpp:42
void setEpsilon(double const epsilon)
Set value of epsilon.
Definition: Model.cc:323
virtual void set(gsl::matrix &matrix, gsl::matrix const &submatrix, double const scale=1.0) const
Set matrix from a submatrix without checking bounds.
Definition: Model.hpp:515
virtual void add(gsl::vector &vector, gsl::vector const &subvector, double const scale=1.0) const =0
Add to vector from a subvector.
bool removeConstraint(Constraint &constraint)
Remove a constraint.
Definition: Model.cc:57
Subvector()
Default constructor.
Definition: Model.hpp:413
IndirectSubvector(size_t const size)
The constructor needs a vector to translate the indices of subvector to indices of vector: that is su...
Definition: Model.cc:34
Parameters parameters
Parameters for interior point optimisation.
Definition: Model.hpp:399
void setOutputStream(std::ostream *outputStream)
Set value of output stream.
Definition: Model.cc:327
double getEpsilon() const
Get value of epsilon.
Definition: Model.hpp:343
bool maximise()
Try to maximise the objective function subject to the constraints.
Definition: Model.hpp:763
bool isFeasible()
Test whether current values of variables give a feasible solution.
Definition: Model.cc:136
std::vector< std::shared_ptr< Subvector > > constraintIndices
Used internally to match the variables in each constraint to indices in a vector: (*constraintIndices...
Definition: Model.hpp:892
virtual bool findFeasible()
Try to find a set of values for the variables that gives a feasible solution.
Definition: Model.cc:531
Parameters()
Constructor: sets default values.
Definition: Model.cc:313
std::shared_ptr< Subvector > objectiveIndices
Used internally to match the variables in the objective to indices in a vector: objectiveIndices[i] i...
Definition: Model.hpp:886
void addConstraint(Constraint &constraint)
Add a constraint.
Definition: Model.cc:48
This namespace holds all the interior-point optimisation classes.
Definition: Array.hpp:28
void setMu(double const mu)
Set value of mu.
Definition: Model.cc:320
virtual bool optimise(bool const minimise)
Try to optimise the objective function subject to the constraints.
Definition: Model.cc:247