Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
Objective.cc
Go to the documentation of this file.
1 /*
2  * $Id: Objective.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"Objective.hpp"
25 
26 using namespace ipo;
27 
30  std::string const& name )
31  : Var { model }, ModelFunction { function },
32  data{ std::shared_ptr<Data> { new Data { model } } }
33 {
34  data->name = name;
35  data->variables.setName( name );
36  model.notify();
37 }
38 
40  std::string const& name )
41  : Objective { model, detail::SharedFunctionPtr { function }, name }{}
42 
44  std::string const& name )
45  : Objective { model, detail::SharedFunctionPtr { function }, name }{}
46 
48  char const* const name )
49  : Objective { model, function, 0 == name ? std::string {} : name }{}
50 
52  char const* const name )
53  : Objective { model, function, 0 == name ? std::string {} : name }{}
54 
56  char const* const name )
57  : Objective { model, function, 0 == name ? std::string {} : name }{}
58 
59 std::string
61  return data->name;
62 }
63 
64 void
65 Objective::setName( std::string const& name ){
66  data->name = name;
67 }
68 
69 void
70 Objective::setName( char* const name ){
71  data->name = 0 == name ? std::string() : name;
72 }
73 
74 Array&
76  model.notify();
77  return data->variables;
78 }
79 
80 Objective::Objective( Objective const& objective )
81  : Var { objective.model }, ModelFunction { objective.function }
82 {
83  data = objective.data;
84  model.notify();
85 }
86 
88  : Var{ model }, ModelFunction { objective.function }
89 {
90  data = objective.data;
91 }
92 
93 Objective&
94 Objective::operator=( Objective const& objective ){
95  model = objective.model;
96  function = objective.function;
97  data = objective.data;
98  model.notify();
99  return *this;
100 }
101 
102 Objective&
104  model = objective.model;
105  function = objective.function;
106  data = objective.data;
107  return *this;
108 }
109 
110 Objective::Data::Data( detail::ModelBase& model ) : variables( model ){}
111 
112 void
114  try {
115  auto& array = getVariables();
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() );
121  }
122  array.push_back( variable );
123  } catch( ... ){
124  RETHROW( "ipo::Objective::addVariable()" );
125  }
126 }
127 
128 void
130  try {
131  auto& array = getVariables();
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() );
138  }
139  array.erase( iter );
140  } catch( ... ){
141  RETHROW( "ipo::Objective::removeVariable()" );
142  }
143 }
144 
145 void
147  if( array.getModel() != getModel() ){
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() );
153  }
154  for( auto& variable : array ){
155  auto iter = std::find( getVariables().begin(), getVariables().end(), variable );
156  if( getVariables().end() != iter ){
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() );
161  }
162  }
163  try {
164  for( auto& variable : array ) getVariables().push_back( variable );
165  } catch( ... ){
166  RETHROW( "ipo::Objective::addArray()" );
167  }
168 }
169 
170 void
172  if( array.getModel() != getModel() ){
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() );
178  }
179  for( auto& variable : array ){
180  auto iter = std::find( getVariables().begin(), getVariables().end(), variable );
181  if( getVariables().end() == iter ){
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() );
186  }
187  }
188  try {
189  for( auto& variable : array )
190  getVariables().erase( std::find( getVariables().begin(),
191  getVariables().end(), variable ) );
192  } catch( ... ){
193  RETHROW( "ipo::Objective::removeArray()" );
194  }
195 }
196 
197 void
198 Objective::summary( std::ostream& ostream, std::string const& prefix ) const {
199  ostream << prefix << getName() << " ( ";
200  bool first { true };
201  for( auto const& e : const_cast<ipo::Objective&>( *this ).getVariables() ){
202  if( first ) first = false; else ostream << ", ";
203  ostream << e.getName();
204  }
205  ostream << " ) = " << value() << std::endl;
206 }
207 
208 double
209 Objective::value() const noexcept {
210  try {
211  Array const& variables { const_cast<Objective&>( *this ).getVariables() };
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();
216  }
217  return (*const_cast<Objective&>( *this ).getFunction())( vector );
218  } catch( ... ){
219  // Return NaN if anything goes wrong
220  return std::numeric_limits<double>::quiet_NaN();
221  }
222 }
void addArray(Array &array)
Add an Array.
Definition: Objective.cc:146
Shared pointer to ipo::Function object.
void push_back(value_type const &value)
Insert value at end of array.
Definition: Array.cc:323
ModelBase & model
A Model to attach this to.
Definition: Var.hpp:101
size_type size() const
Get size of array.
Definition: Array.hpp:400
Array & getVariables()
Get variables used by Objective function.
Definition: Objective.cc:75
void setName(std::string const &name)
Set name of Objective.
Definition: Objective.cc:65
iterator erase(iterator pos)
Erase element specified by pos.
Definition: Array.hpp:487
std::shared_ptr< Data > data
The objective data.
Definition: Objective.hpp:205
#define IPOE(message)
Macro to allow file and line names in exceptions.
std::string getName() const
Get name of variable.
Definition: Variable.cc:49
Abstract base class for model.
Definition: Var.hpp:39
Class for an objective function.
Definition: Objective.hpp:37
#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.
Definition: Objective.cc:209
void removeArray(Array &array)
Remove an Array.
Definition: Objective.cc:171
Data(detail::ModelBase &model)
Constructor.
Definition: Objective.cc:110
std::string getName() const
Get name of variable.
Definition: Objective.cc:60
This class computes a function at a vector.
Definition: Function.hpp:38
This class represents a variable.
Definition: Variable.hpp:36
std::string getName() const
Get name of variable.
Definition: Array.cc:217
void addVariable(Variable &variable)
Add a Variable.
Definition: Objective.cc:113
void removeVariable(Variable &variable)
Remove (first, and usually only occurrence of) a Variable.
Definition: Objective.cc:129
This class represents an array of Variable objects.
Definition: Array.hpp:45
Objective & operator=(Objective const &objective)
Assignment operator.
Definition: Objective.cc:94
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.
Definition: Objective.cc:28
data name
Definition: Variable.cc:37
ModelBase const *const getModel() const
Get pointer to model.
Definition: Var.hpp:88
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.
Definition: Objective.cc:198
This namespace holds all the interior-point optimisation classes.
Definition: Array.hpp:28