30 std::string
const&
name )
31 : Var { model }, ModelFunction {
function },
32 data{ std::shared_ptr<Data> {
new Data { model } } }
35 data->variables.setName(
name );
40 std::string
const&
name )
44 std::string
const&
name )
48 char const*
const name )
49 :
Objective {
model,
function, 0 == name ? std::string {} : name }{}
52 char const*
const name )
53 :
Objective {
model,
function, 0 == name ? std::string {} : name }{}
56 char const*
const name )
57 :
Objective {
model,
function, 0 == name ? std::string {} : name }{}
71 data->name = 0 == name ? std::string() :
name;
77 return data->variables;
81 : Var { objective.
model }, ModelFunction { objective.function }
83 data = objective.data;
88 : Var{
model }, ModelFunction { objective.function }
90 data = objective.data;
104 model = objective.model;
105 function = objective.function;
106 data = objective.data;
116 if( std::find( array.begin(), array.end(), variable ) != array.end() ){
117 std::ostringstream ost {
"ipo::Objective::addVariable(): " };
118 ost <<
"objective or constraint " <<
getName() <<
" already contains variable ";
119 ost << variable.
getName() <<
".";
120 throw IPOE( ost.str() );
122 array.push_back( variable );
124 RETHROW(
"ipo::Objective::addVariable()" );
132 auto iter = std::find( array.begin(), array.end(), variable );
133 if( array.end() == iter ){
134 std::ostringstream ost {
"ipo::Objective::removeVariable(): " };
135 ost <<
"objective or constraint " <<
getName() <<
" does not contain variable ";
136 ost << variable.
getName() <<
".";
137 throw IPOE( ost.str() );
141 RETHROW(
"ipo::Objective::removeVariable()" );
148 std::ostringstream ost {
"ipo::Objective::addArray(): " };
149 ost <<
"array " << array.
getName()
150 <<
" must be in same model as "
151 <<
"objective or constraint " <<
getName() <<
".";
152 throw IPOE( ost.str() );
154 for(
auto& variable : array ){
157 std::ostringstream ost {
"ipo::Objective::addArray(): " };
158 ost <<
"objective or constraint " <<
getName() <<
" already contains variable ";
159 ost << variable.getName() <<
".";
160 throw IPOE( ost.str() );
166 RETHROW(
"ipo::Objective::addArray()" );
173 std::ostringstream ost {
"ipo::Objective::removeArray(): " };
174 ost <<
"array " << array.
getName()
175 <<
" must be in same model as "
176 <<
"objective or constraint " <<
getName() <<
".";
177 throw IPOE( ost.str() );
179 for(
auto& variable : array ){
182 std::ostringstream ost {
"ipo::Objective::removeArray(): " };
183 ost <<
"objective or constraint " <<
getName() <<
" does not contain variable ";
184 ost << variable.getName() <<
".";
185 throw IPOE( ost.str() );
189 for(
auto& variable : array )
193 RETHROW(
"ipo::Objective::removeArray()" );
199 ostream << prefix <<
getName() <<
" ( ";
201 for(
auto const& e : const_cast<ipo::Objective&>( *this ).getVariables() ){
202 if( first ) first =
false;
else ostream <<
", ";
203 ostream << e.getName();
205 ostream <<
" ) = " <<
value() << std::endl;
212 size_t const SIZE { variables.
size() };
213 gsl::vector vector( SIZE );
214 for(
size_t i { 0 }; i < SIZE; ++i ){
215 vector[i] = variables[i].getValue();
217 return (*const_cast<Objective&>( *this ).getFunction())( vector );
220 return std::numeric_limits<double>::quiet_NaN();
void addArray(Array &array)
Add an Array.
Shared pointer to ipo::Function object.
void push_back(value_type const &value)
Insert value at end of array.
ModelBase & model
A Model to attach this to.
size_type size() const
Get size of array.
Array & getVariables()
Get variables used by Objective function.
void setName(std::string const &name)
Set name of Objective.
iterator erase(iterator pos)
Erase element specified by pos.
std::shared_ptr< Data > data
The objective data.
#define IPOE(message)
Macro to allow file and line names in exceptions.
std::string getName() const
Get name of variable.
Abstract base class for model.
Class for an objective function.
#define RETHROW(function)
Macro to allow file and line names in exceptions.
double value() const noexcept
Value of the objective function at the current value of the variables.
void removeArray(Array &array)
Remove an Array.
Data(detail::ModelBase &model)
Constructor.
std::string getName() const
Get name of variable.
This class computes a function at a vector.
This class represents a variable.
std::string getName() const
Get name of variable.
void addVariable(Variable &variable)
Add a Variable.
void removeVariable(Variable &variable)
Remove (first, and usually only occurrence of) a Variable.
This class represents an array of Variable objects.
Objective & operator=(Objective const &objective)
Assignment operator.
virtual void notify()=0
Notify function that model must implement.
Objective(detail::ModelBase &model, detail::SharedFunctionPtr function, std::string const &name)
Use this constructor to construct from a SharedFunctionPtr object.
ModelBase const *const getModel() const
Get pointer to model.
SharedFunctionPtr function
The objective or constraint function.
virtual void summary(std::ostream &ostream=std::cout, std::string const &prefix="") const override
Create a summary of this function.
This namespace holds all the interior-point optimisation classes.