Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
Constraint.cc
Go to the documentation of this file.
1 /*
2  * $Id: Constraint.cc 239 2014-11-30 14:11:47Z jdl3 $
3  * Copyright (C) 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 
20 #ifdef HAVE_CONFIG_H
21 # include<config.h>
22 #endif
23 
24 #include"Constraint.hpp"
25 
26 using namespace ipo;
27 
29  std::string const& name )
30  : Objective { model, function, name }{}
31 
33  std::string const& name )
34  : Constraint { model, detail::SharedFunctionPtr { function }, name }{}
35 
37  std::string const& name )
38  : Constraint { model, detail::SharedFunctionPtr { function }, name }{}
39 
41  char const* const name )
42  : Constraint { model, function, 0 == name ? std::string {} : name }{}
43 
45  char const* const name )
46  : Constraint { model, function, 0 == name ? std::string {} : name }{}
47 
49  char const* const name )
50  : Constraint { model, function, 0 == name ? std::string {} : name }{}
51 
52 double
54  return data->upperBound;
55 }
56 
57 double
59  return data->lowerBound;
60 }
61 
62 void
64  if( upperBound <= data->lowerBound )
65  throw IPOE( "ipo::Constraint::setUpperBound(): upper bound must exceed lower." );
66  data->upperBound = upperBound;
67 }
68 
69 void
71  if( data->upperBound <= lowerBound )
72  throw IPOE( "ipo::Constraint::setLowerBound(): upper bound must exceed lower." );
73  data->lowerBound = lowerBound;
74 }
75 
76 bool
77 ipo::operator==( Constraint const& lhs, Constraint const& rhs ){
78  return lhs.data == rhs.data;
79 }
80 
81 
82 Constraint::Constraint( Constraint const& constraint )
83  : Objective { constraint }{}
84 
86 Constraint::operator=( Constraint const& constraint ){
87  model = constraint.model;
88  function = constraint.function;
89  data = std::move( constraint.data );
90  return *this;
91 }
92 
93 void
94 Constraint::summary( std::ostream& ostream, std::string const& prefix ) const {
95  auto f = [](double x)->std::string {
96  std::ostringstream ost;
97  double a { std::fabs( x ) };
98  if( x < 0 ) ost << "−";
100  ost << "∞";
101  else
102  ost << x;
103  return ost.str();
104  };
105  ostream << prefix << f( getLowerBound() ) << " ≤ " << getName() << " ( ";
106  bool first { true };
107  for( auto const& e : const_cast<ipo::Constraint&>( *this ).getVariables() ){
108  if( first ) first = false; else ostream << ", ";
109  ostream << e.getName();
110  }
111  ostream << " ) ≤ " << f( getUpperBound() ) << std::endl;
112  std::cout << prefix << " Constraint function value = " << value() << std::endl;
113 }
Shared pointer to ipo::Function object.
ModelBase & model
A Model to attach this to.
Definition: Var.hpp:101
virtual void summary(std::ostream &ostream=std::cout, std::string const &prefix="") const override
Create a summary of this function.
Definition: Constraint.cc:94
std::shared_ptr< Data > data
The objective data.
Definition: Objective.hpp:205
#define IPOE(message)
Macro to allow file and line names in exceptions.
Class for a constraint function.
Definition: Constraint.hpp:40
Abstract base class for model.
Definition: Var.hpp:39
Class for an objective function.
Definition: Objective.hpp:37
double getLowerBound() const
Get lower bound of Constraint.
Definition: Constraint.cc:58
virtual void setUpperBound(double const upperBound)
Set upper bound of Constraint.
Definition: Constraint.cc:63
double value() const noexcept
Value of the objective function at the current value of the variables.
Definition: Objective.cc:209
std::string getName() const
Get name of variable.
Definition: Objective.cc:60
This class computes a function at a vector.
Definition: Function.hpp:38
Constraint(detail::ModelBase &model, detail::SharedFunctionPtr function, std::string const &name)
Use this constructor to construct from a ipo_function::SharedFunctionPtr object.
Definition: Constraint.cc:28
data lowerBound
Definition: Variable.cc:39
double getUpperBound() const
Get upper bound of Constraint.
Definition: Constraint.cc:53
data name
Definition: Variable.cc:37
data upperBound
Definition: Variable.cc:38
bool operator==(Array const &lhs, Array const &rhs)
Compare two Array objects.
Definition: Array.hpp:688
SharedFunctionPtr function
The objective or constraint function.
virtual void setLowerBound(double const lowerBound)
Set lower bound of Constraint.
Definition: Constraint.cc:70
Constraint & operator=(Constraint const &constraint)
Assignment operator.
Definition: Constraint.cc:86
This namespace holds all the interior-point optimisation classes.
Definition: Array.hpp:28
std::string const infinity
Infinity sign, ∞.
Definition: Format.hpp:54