32 : offset { offset }, length { length }{}
35 :
std::vector<size_t>( size ){}
50 if(
nullptr != lc and lc->isEqualityConstraint() )
64 if(
nullptr != lc and lc->isEqualityConstraint() ){
81 std::vector<Constraint>
const&
86 std::vector<LinearConstraint>
const&
98 RETHROW(
"ipo::Model::setIndices()" );
105 bool const DIRECTSUBVECTORFOROBJECTIVEINDICES { OSIZE ==
indexMap.size() };
108 for(
auto& variable : constraint.getVariables() )
indexMap[variable] = OSIZE;
110 for(
auto& variable : constraint.getVariables() )
indexMap[variable] = OSIZE;
113 if( DIRECTSUBVECTORFOROBJECTIVEINDICES )
116 for(
auto& entry :
indexMap )
if( OSIZE == entry.second ) entry.second = index++;
126 variablesChanged =
false;
139 gsl::vector superVector(
indexMap.size() );
142 superVector[p.second] = p.first.getValue();
145 for(
auto& entry : indexMap ){
146 Variable const& variable { entry.first };
147 double value { variable.
getValue() };
148 if( value <= variable.getLowerBound() or value >= variable.getUpperBound() )
150 superVector[entry.second] = value;
154 for(
size_t index { 0 }; index <
constraints.size(); ++index ){
158 double value { (*constraint.getFunction())( vector ) };
159 if( value < constraint.getLowerBound() or value > constraint.getUpperBound() )
167 double value { (*equalityConstraint.getFunction())( vector ) };
168 if( value != equalityConstraint.getUpperBound() )
178 gsl::vector superVector(
indexMap.size() );
181 superVector[p.second] = p.first.getValue();
184 for(
auto& entry : indexMap ){
185 Variable const& variable { entry.first };
186 double value { variable.
getValue() };
187 if( value <= variable.getLowerBound() or value >= variable.getUpperBound() )
189 superVector[entry.second] = value;
193 for(
size_t index { 0 }; index <
constraints.size(); ++index ){
197 double value { (*constraint.getFunction())( vector ) };
198 if( value <= constraint.getLowerBound() or value >= constraint.getUpperBound() )
206 double value { (*equalityConstraint.getFunction())( vector ) };
207 if( value != equalityConstraint.getUpperBound() )
216 size_t const SIZE =
indexMap.size();
217 gsl::vector vector( SIZE );
219 vector[entry.second] = entry.first.getValue();
225 size_t const SIZE {
indexMap.size() };
226 if( vector.size() != SIZE ){
227 std::ostringstream error;
228 error <<
"ipo::Model::setVariablesFromVector(): ";
229 error <<
"vector size is " << vector.size();
230 error <<
", but there are " << SIZE <<
" variables.";
231 throw IPOE( error.str() );
234 const_cast<Variable&
>( entry.first ).setValue( vector[entry.second] );
240 double numConstraints {
static_cast<double>(
constraints.size()
263 gsl::matrix A { EQUALITYCONSTRAINTSSIZE, vector.size() };
264 gsl::vector bounds( EQUALITYCONSTRAINTSSIZE );
265 for(
size_t i { 0 }; i < EQUALITYCONSTRAINTSSIZE; ++i ){
267 auto row = A.row( i );
269 for(
auto& var : vars )
281 barrierFunction.set_t( t );
283 if(
nullptr != outputStream ) *outputStream <<
" t = " << t << std::endl;
286 newtonDescent.setFunctionScale( (minimise ? 1 : -1) / t );
287 double const objectiveValue { barrierFunction( vector ) };
288 newtonDescent( vector, objectiveValue );
297 if(
nullptr != dynamic_cast<detail::PhaseIModel*>(
this ) and *vector.rbegin() < 0 )
307 RETHROW(
"ipo::Model::optimise()" );
315 epsilon = std::sqrt( std::numeric_limits<double>::epsilon() );
316 outputStream =
nullptr;
324 this->epsilon = epsilon; }
328 this->outputStream = outputStream; }
346 if( constraint.checkSize() == false )
return false;
352 std::ostringstream ost;
355 size_t const sizeF { std::get<0>( sizes ) };
356 size_t const sizeV { std::get<1>( sizes ) };
359 ost <<
"Objective function " <<
objective.
getName() <<
" has no variables." << std::endl;
363 <<
" needs " << sizeF <<
" variables, "
364 <<
"but " << sizeV <<
" supplied." << std::endl;
367 auto sizes = constraint.getSizes();
368 size_t const sizeF { std::get<0>( sizes ) };
369 size_t const sizeV { std::get<1>( sizes ) };
372 ost <<
"Constraint " << constraint.getName() <<
" has no variables." << std::endl;
375 ost <<
"Constraint " << constraint.getName()
376 <<
" needs " << sizeF <<
" variables, "
377 <<
" but " << sizeV <<
" supplied." << std::endl;
381 std::string
const str { ost.str() };
382 if( str.size() > 0 ){
383 std::ostringstream ost {
"ipo::Model::testSizes(): " };
384 ost <<
"errors in objective and constraints:";
385 ost << std::endl << str;
393 std::string
const prefix {
" " };
394 ostream <<
"Optimise" << std::endl;
396 ostream <<
"subject to" << std::endl;
399 Array variables {
const_cast<Model&
>( *this ), 0,
"variables" };
402 for(
auto const& variable : const_cast<Model&>( *this ).getObjective().getVariables() ){
403 if( not variables.contains( variable ) ) variables.push_back( variable );
407 for(
auto const& constraint : const_cast<Model&>( *this ).getConstraints() ){
408 constraint.
summary( ostream, prefix );
409 for(
auto const& variable : const_cast<Constraint&>( constraint ).getVariables() ){
410 if( not variables.contains( variable ) ) variables.push_back( variable );
415 for(
auto const& constraint : const_cast<Model&>( *this ).getEqualityConstraints() ){
416 constraint.summary( ostream, prefix );
417 for(
auto const& variable : const_cast<LinearConstraint&>( constraint ).getVariables() ){
418 if( not variables.contains( variable ) ) variables.push_back( variable );
424 variables.summary( ostream, prefix );
425 ostream << std::endl;
430 auto indirectSubvector = [
this](
Constraint& constraint,
size_t const index,
431 bool const eqConstraint )->
void {
437 (*cIndices)[i] = this->
indexMap[variable];
448 bool const DIRECTSUBVECTORFOROBJECTIVEINDICES { OSIZE ==
indexMap.size() };
453 for(
auto& constraint : constraints ){
454 if( not DIRECTSUBVECTORFOROBJECTIVEINDICES )
455 indirectSubvector( constraint, index,
false );
458 indirectSubvector( constraint, index,
false );
461 indirectSubvector( constraint, index,
false );
470 bool correctOrder {
true };
472 for(
size_t j { 0 }; j < variables.size(); ++j ){
473 auto& variable = variables[j];
475 correctOrder =
false;
483 indirectSubvector( constraint, index,
false );
494 if( not DIRECTSUBVECTORFOROBJECTIVEINDICES )
495 indirectSubvector( constraint, index,
true );
498 indirectSubvector( constraint, index,
true );
501 indirectSubvector( constraint, index,
true );
510 bool correctOrder {
true };
512 for(
size_t j { 0 }; j < variables.size(); ++j ){
513 auto& variable = variables[j];
515 correctOrder =
false;
523 indirectSubvector( constraint, index,
true );
533 if(
nullptr == dynamic_cast<detail::PhaseIModel*>(
this ) and not
isStrictlyFeasible() ){
std::map< Variable, size_t > indexMap
Used internally to match the variables in each variable to indices in a vector.
Model used for finding an initial feasible solution in phase I of interior-pont optimisation.
double getMu() const
Get value of mu.
std::vector< LinearConstraint > const & getEqualityConstraints() const
Get list of equality constraints.
virtual void setIndices()
Sets the values of objectiveIndices, constraintIndices and variableIndices from constraints.
std::vector< ipo::LinearConstraint > equalityConstraints
The set of equality constraints.
This class gives direct access to a subvector of a vector.
detail::LineSearch::Parameters & getLineSearchParameters()
Get line search parameters by reference.
std::tuple< size_t, size_t > getSizes() const
Get size of vector for function arguments and number of variables supplied.
bool minimise()
Try to minimise the objective function subject to the constraints.
detail::LineSearch::Parameters lineSearchParameters
Parameters used by backtracking line search algorithm during optimisation.
Objective getObjective()
Get the objective function.
bool variablesChanged
Value that indicates if any variables, objective or constraints have been modified in such a way that...
void testSizes() const
Check objective and constraint functions.
bool findFeasibleSolution()
Find a feasible solution to the base Model by partial optimisation of the PhaseIModel and copy the re...
std::vector< std::shared_ptr< Subvector > > equalityConstraintIndices
Used internally to match the variables in each equality constraint to indices in a vector: (*constrai...
std::ostream * getOutputStream() const
Get output stream.
This class gives direct access to a subvector of a vector.
detail::NewtonDescent::Parameters newtonDescentParameters
Parameters used by Newton descent algorithm during optimisation.
detail::NewtonDescent::Parameters & getNewtonDescentParameters()
Get Newton descent parameters by reference.
size_type size() const
Get size of array.
Array & getVariables()
Get variables used by Objective function.
Objective objective
The objective function.
Class to represent a linear combination as an Objective or Constraint.
#define IPOE(message)
Macro to allow file and line names in exceptions.
std::vector< Constraint > const & getConstraints() const
Get list of constraints.
bool isStrictlyFeasible()
Test whether current values of variables give a feasible solution.
void setVariablesFromVector(gsl::vector const &vector)
Set variable values from a vector.
std::vector< Constraint > constraints
The set of constraints.
Class for a constraint function.
Model an interior-point optimisation problem.
virtual void notify()
Notify function that model must implement.
DirectSubvector(size_t const offset, size_t const length)
The constructor needs an offset and a length.
bool stoppingCriterion(double const t) const
Check whether or not stopping criterion has been met.
Class for an objective function.
#define RETHROW(function)
Macro to allow file and line names in exceptions.
void summary(std::ostream &ostream=std::cout) const
Create a summary of this model.
gsl::vector getVectorFromVariables() const
Get a vector from values of variables.
Namespace for functions that can be used by ipo::Objective and ipo::Constraint.
std::string getName() const
Get name of variable.
double getValue() const
Get value of variable.
Parameters & getParameters()
Get parameters by reference.
void setObjectiveIndices()
Set objectiveIndices.
This class represents a variable.
Objects of this class are used to store information about an exception generated by ipo objects and f...
bool checkSize() const
Check objective and constraint functions.
Function for the logarithmic barrier.
This class represents an array of Variable objects.
void setConstraintIndices()
Set constraintIndices.
void setObjective(Objective &objective)
Set the objective function.
void setEpsilon(double const epsilon)
Set value of epsilon.
bool removeConstraint(Constraint &constraint)
Remove a constraint.
IndirectSubvector(size_t const size)
The constructor needs a vector to translate the indices of subvector to indices of vector: that is su...
Parameters parameters
Parameters for interior point optimisation.
bool checkSize() const
Check size of vector for function arguments matches number of variables supplied. ...
Parameters & getParameters()
Get parameters by reference.
void setOutputStream(std::ostream *outputStream)
Set value of output stream.
double getEpsilon() const
Get value of epsilon.
bool isFeasible()
Test whether current values of variables give a feasible solution.
std::vector< std::shared_ptr< Subvector > > constraintIndices
Used internally to match the variables in each constraint to indices in a vector: (*constraintIndices...
virtual bool findFeasible()
Try to find a set of values for the variables that gives a feasible solution.
virtual void summary(std::ostream &ostream=std::cout, std::string const &prefix="") const override
Create a summary of this function.
Parameters()
Constructor: sets default values.
void addConstraint(Constraint &constraint)
Add a constraint.
reference front()
Get reference to first Variable.
This namespace holds all the interior-point optimisation classes.
void setMu(double const mu)
Set value of mu.
virtual bool optimise(bool const minimise)
Try to optimise the objective function subject to the constraints.