Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
Variable.cc
Go to the documentation of this file.
1 /*
2  * $Id: Variable.cc 226 2014-11-17 17:53:01Z 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"Variable.hpp"
25 
26 using namespace ipo;
27 
28 Variable::Variable( detail::ModelBase& model, char const* const name )
29  : Variable { model, nullptr == name ? std::string() : std::string { name } }{
30  model.notify();
31 }
32 
33 Variable::Variable( detail::ModelBase& model, std::string const& name )
34  : Var { model }, data { std::shared_ptr<Data>{ new Data } }
35 {
36  data->value = 0;
37  data->name = name;
38  data->upperBound = infinity;
39  data->lowerBound = minusInfinity;
40  model.notify();
41 }
42 
43 double
45  return data->value;
46 }
47 
48 std::string
50  return data->name;
51 }
52 
53 double
55  return data->upperBound;
56 }
57 
58 double
60  return data->lowerBound;
61 }
62 
63 void
64 Variable::setValue( double const value ){
65  if( value < getLowerBound() ){
66  std::ostringstream error;
67  error << "ipo::Variable::set(): value ";
68  error << value << " less than lower bound ";
69  error << getLowerBound() << ".";
70  throw IPOE( error.str() );
71  } else if( value > getUpperBound() ){
72  std::ostringstream error;
73  error << "ipo::Variable::set(): value ";
74  error << value << " greater than upper bound ";
75  error << getUpperBound() << ".";
76  throw IPOE( error.str() );
77  }
78  data->value = value;
79 }
80 
81 void
82 Variable::setName( std::string const& name ){
83  data->name = name;
84 }
85 
86 void
87 Variable::setName( char* const name ){
88  data->name = nullptr == name ? std::string {} : std::string { name };
89 }
90 
91 void
93  if( upperBound < getLowerBound() ){
94  std::ostringstream error;
95  error << "ipo::Variable::setUpperBound(): upper bound ";
96  error << upperBound << " less than lower bound ";
97  error << getLowerBound() << ".";
98  throw IPOE( error.str() );
99  }
100  data->upperBound = upperBound;
101  data->value = std::min( getValue(), upperBound );
102 }
103 
104 void
106  if( lowerBound > getUpperBound() ){
107  std::ostringstream error;
108  error << "ipo::Variable::setLowerBound(): lower bound ";
109  error << lowerBound << " greater than upper bound ";
110  error << getUpperBound() << ".";
111  throw IPOE( error.str() );
112  }
113  data->lowerBound = lowerBound;
114  data->value = std::max( getValue(), lowerBound );
115 }
116 
117 bool
118 ipo::operator==( Variable const& lhs, Variable const& rhs ){
119  return lhs.data == rhs.data;
120 }
121 
122 bool
123 ipo::operator<( Variable const& lhs, Variable const& rhs ){
124  return lhs.data.get() < rhs.data.get();
125 }
126 
127 Variable&
128 Variable::operator=( Variable const& variable ){
129  if( &variable.model != &model )
130  throw IPOE( "ipo::Variable::operator=(): cannot assign variable to different model." );
131  data = variable.data;
132  return *this;
133 }
134 
135 Variable::Variable( Variable const& variable )
136  : Var( variable.model ), data( variable.data ){
137  // Two references for same variable
138 }
139 
140 Variable&
142  if( &variable.model != &model )
143  throw IPOE( "ipo::Variable::operator=(): cannot assign variable to different model." );
144  data = variable.data;
145  return *this;
146 }
147 
148 void
149 Variable::summary( std::ostream& ostream, std::string const& prefix ) const {
150  auto f = [](double x)->std::string {
151  std::ostringstream ost;
152  double a { std::fabs( x ) };
153  if( x < 0 ) ost << "−";
155  ost << "∞";
156  else
157  ost << x;
158  return ost.str();
159  };
160  ostream << prefix << getName() << " = " << getValue()
161  << " (" << f( getLowerBound() ) << " ≤ " << getName() << " ≤ "
162  << f( getUpperBound() ) << ")" << std::endl;
163 }
void setValue(double const value)
Set value of variable.
Definition: Variable.cc:64
bool operator<(Array const &lhs, Array const &rhs)
Compare two Array objects.
Definition: Array.hpp:704
std::shared_ptr< Data > data
The Variable data as a shared pointer.
Definition: Variable.hpp:154
double constexpr infinity
Infinity: use for unbounded variables.
Definition: Var.hpp:30
ModelBase & model
A Model to attach this to.
Definition: Var.hpp:101
void setName(std::string const &name)
Set name of variable.
Definition: Variable.cc:82
virtual void summary(std::ostream &ostream=std::cout, std::string const &prefix="") const override
Create a summary of this variable.
Definition: Variable.cc:149
#define IPOE(message)
Macro to allow file and line names in exceptions.
std::string getName() const
Get name of variable.
Definition: Variable.cc:49
Variable & operator=(Variable const &variable)
Copy assignment operator.
Definition: Variable.cc:128
Abstract base class for model.
Definition: Var.hpp:39
void setLowerBound(double const lowerBound)
Set lower bound of variable.
Definition: Variable.cc:105
double getUpperBound() const
Get upper bound of variable.
Definition: Variable.cc:54
Variable(detail::ModelBase &model, char const *const name=nullptr)
Default constructor creates a named variable with the given name, initial value 0 and unbounded...
Definition: Variable.cc:28
double getValue() const
Get value of variable.
Definition: Variable.cc:44
This class represents a variable.
Definition: Variable.hpp:36
double getLowerBound() const
Get lower bound of variable.
Definition: Variable.cc:59
data lowerBound
Definition: Variable.cc:39
double constexpr minusInfinity
Negative infinity: use for unbounded variables.
Definition: Var.hpp:34
data name
Definition: Variable.cc:37
data upperBound
Definition: Variable.cc:38
void setUpperBound(double const upperBound)
Set upper bound of variable.
Definition: Variable.cc:92
bool operator==(Array const &lhs, Array const &rhs)
Compare two Array objects.
Definition: Array.hpp:688
This namespace holds all the interior-point optimisation classes.
Definition: Array.hpp:28
std::string const infinity
Infinity sign, ∞.
Definition: Format.hpp:54