Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
Array.cc
Go to the documentation of this file.
1 /*
2  * $Id: Array.cc 187 2014-10-22 09:25:23Z 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"Array.hpp"
25 
26 using namespace ipo;
27 
28 Array::Data::Data( detail::ModelBase& model, std::vector<Variable>::allocator_type alloc,
29  std::string const& name )
30  : name { name }, array { alloc }
31 {
32  if( not testModelMatch( model ) )
33  throw IPOE( "ipo::Array::Data::Data: at least one variable discarded because it is "
34  "not owned by model that owns array." );
35 }
36 
37 Array::Data::Data( detail::ModelBase& model, size_t size, std::string const name )
38  : name { name }, array { size, Variable { model } }
39 {
40  // Recreate and rename variables
41  for( size_t i { 0 }; i < size; ++i ){
42  array[i] = Variable { model };
43  array[i].setName( name + format::subscript( i ) );
44  }
45  model.notify();
46 }
47 
49  std::vector<Variable>::value_type const& val,
50  std::vector<Variable>::allocator_type alloc, std::string const& name )
51  : name { name }, array { size, val, alloc }
52 {
53  if( not testModelMatch( model ) )
54  throw IPOE( "ipo::Array::Data::Data: at least one variable discarded because it is "
55  "not owned by model that owns array." );
56 }
57 
59  : name { dataArray.name }, array { dataArray.array }
60 {
61  if( not testModelMatch( model ) )
62  throw IPOE( "ipo::Array::Data::Data: at least one variable discarded because it is "
63  "not owned by model that owns array." );
64 }
65 
67  std::vector<Variable>::allocator_type const& alloc )
68  : name { dataArray.name }, array { dataArray.array, alloc }
69 {
70  if( not testModelMatch( model ) )
71  throw IPOE( "ipo::Array::Data::Data: at least one variable discarded because it is "
72  "not owned by model that owns array." );
73 }
74 
75 Array::Data::Data( detail::ModelBase& model, std::initializer_list<Variable> init,
76  std::vector<Variable>::allocator_type const& alloc,
77  char const* const name )
78  : name { nullptr == name ? std::string() : std::string { name } }, array { init, alloc }
79 {
80  if( not testModelMatch( model ) )
81  throw IPOE( "ipo::Array::Data::Data: at least one variable discarded because it is "
82  "not owned by model that owns array." );
83 }
84 
85 Array::Data::Data( detail::ModelBase& model, std::initializer_list<Variable> init,
86  std::vector<Variable>::allocator_type const& alloc,
87  std::string const name )
88  : name { name }, array { init, alloc }
89 {
90  if( not testModelMatch( model ) )
91  throw IPOE( "ipo::Array::Data::Data: at least one variable discarded because it is "
92  "not owned by model that owns array." );
93 }
94 
95 Array::Data::Data( detail::ModelBase& model, std::initializer_list<Variable> init,
96  char const* const name )
97  : name { nullptr == name ? std::string() : std::string { name } }, array { init }
98 {
99  if( not testModelMatch( model ) )
100  throw IPOE( "ipo::Array::Data::Data: at least one variable discarded because it is "
101  "not owned by model that owns array." );
102 }
103 
104 Array::Data::Data( detail::ModelBase& model, std::initializer_list<Variable> init,
105  std::string const& name )
106  : name { name }, array { init }
107 {
108  if( not testModelMatch( model ) )
109  throw IPOE( "ipo::Array::Data::Data: at least one variable discarded because it is "
110  "not owned by model that owns array." );
111 }
112 
113 Array::Array( detail::ModelBase& model, allocator_type const& alloc, char const* const name )
114  : Array { model, 0, nullptr == name ? std::string {} : std::string { name } }
115 {
116  model.notify();
117 }
118 
119 Array::Array( detail::ModelBase& model, std::string const& name )
120  : Var { model },
121  dataArray { std::shared_ptr<Data> { new Data { model, static_cast<size_t>( 0 ), name } } }
122 {
123  model.notify();
124 }
125 
126 Array::Array( detail::ModelBase& model, size_t const size, char const* const name )
127  : Array { model, size, nullptr == name ? std::string {} : std::string { name } }
128 {
129  model.notify();
130 }
131 
132 Array::Array( detail::ModelBase& model, size_t const size, std::string const& name )
133  : Var { model }, dataArray { std::shared_ptr<Data> { new Data { model, size, name } } }
134 {
135  model.notify();
136 }
137 
138 Array::Array( detail::ModelBase& model, size_t const size, value_type const& val,
139  allocator_type const& alloc, std::string const& name )
140  : Var { model },
141  dataArray { std::shared_ptr<Data> { new Data { model, size, val, alloc, name } } }
142 {
143  model.notify();
144 }
145 
146 Array::Array( detail::ModelBase& model, size_t const size, value_type const& val,
147  allocator_type const& alloc, char const* const name )
148  : Var { model },
149  dataArray { std::shared_ptr<Data> { new Data { model, size, val, alloc, name } } }
150 {
151  model.notify();
152 }
153 
154 Array::Array( detail::ModelBase& model, size_t const size, value_type const& val,
155  std::string const& name )
156  : Var { model },
157  dataArray { std::shared_ptr<Data> { new Data { model, size, val, allocator_type{}, name } } }
158 {
159  model.notify();
160 }
161 
162 Array::Array( detail::ModelBase& model, size_t const size, value_type const& val,
163  char const * const name )
164  : Var { model },
165  dataArray { std::shared_ptr<Data> { new Data { model, size, val, allocator_type{}, name } } }
166 {
167  model.notify();
168 }
169 
170 Array::Array( Array const& array )
171  : Var { array.model },
172  dataArray { std::shared_ptr<Data> { new Data { model, *array.dataArray } } }
173 {
174  model.notify();
175 }
176 
177 Array::Array( Array const& array, allocator_type const& alloc )
178  : Var { array.model },
179  dataArray { std::shared_ptr<Data> { new Data { model, *array.dataArray, alloc } } }
180 {
181  model.notify();
182 }
183 
184 Array::Array( detail::ModelBase& model, std::initializer_list<Variable> init,
185  std::vector<Variable>::allocator_type const& alloc,
186  char const* const name )
187  : Var { model },
188  dataArray { std::shared_ptr<Data> { new Data { model, init, alloc, name } } }
189 {
190  model.notify();
191 }
192 
193 Array::Array( detail::ModelBase& model, std::initializer_list<Variable> init,
194  std::vector<Variable>::allocator_type const& alloc,
195  std::string const& name )
196  : Var { model },
197  dataArray { std::shared_ptr<Data> { new Data { model, init, alloc, name } } }
198 {
199  model.notify();
200 }
201 
202 Array::Array( detail::ModelBase& model, std::initializer_list<Variable> init,
203  char const* const name )
204  : Var { model }, dataArray { std::shared_ptr<Data> { new Data { model, init, name } } }
205 {
206  model.notify();
207 }
208 
209 Array::Array( detail::ModelBase& model, std::initializer_list<Variable> init,
210  std::string const& name )
211  : Var { model }, dataArray { std::shared_ptr<Data> { new Data { model, init, name } } }
212 {
213  model.notify();
214 }
215 
216 std::string
218  return dataArray->name;
219 }
220 
221 void
222 Array::setName( std::string const& name ){
223  dataArray->name = name;
224 }
225 
226 void
227 Array::setName( char* const name ){
228  dataArray->name = nullptr == name ? std::string() : name;
229 }
230 
231 void
232 Array::swap( Array& array ){
233  if( getModel() != array.getModel() )
234  throw IPOE( "ipo::Array::swap: arrays not swapped because they are "
235  "owned by different models." );
236  std::swap( array.dataArray, dataArray );
237  model.notify();
238 }
239 
240 Array&
242  if( &array.model != &model )
243  throw IPOE( "ipo::Array::operator=(): cannot assign array to different model." );
244  std::swap( dataArray, array.dataArray );
245  return *this;
246 }
247 
248 Array&
250  if( &array.model != &model )
251  throw IPOE( "ipo::Array::operator=(): cannot assign array to different model." );
252  dataArray = array.dataArray;
253  return *this;
254 }
255 
256 bool
258  bool result { true };
259  auto p = array.begin();
260  while( p != array.end() )
261  if( p->getModel() != &model ){
262  p = array.erase( p );
263  result = false;
264  } else ++p;
265  return result;
266 }
267 
268 void
269 Array::assign( size_type count, value_type const& value ){
270  if( value.getModel() != getModel() )
271  throw IPOE( "ipo::Array::assign: variable not assigned because it is "
272  "not owned by model that owns array." );
273  dataArray->array.assign( count, value );
274  model.notify();
275 }
276 
277 void
278 Array::assign( std::initializer_list<Variable> init ){
279  dataArray->array.assign( init );
280  if( not dataArray->testModelMatch( *getModel() ) )
281  throw IPOE( "ipo::Array::assign: at least one variable discarded because it is "
282  "not owned by model that owns array." );
283  model.notify();
284 }
285 
287 Array::insert( iterator pos, const value_type& value ){
288  if( value.getModel() != getModel() )
289  throw IPOE( "ipo::Array::insert: variable not inserted because it is "
290  "not owned by model that owns array." );
291  model.notify();
292  return dataArray->array.insert( pos, value );
293 }
294 
297  if( value.getModel() != getModel() )
298  throw IPOE( "ipo::Array::insert: variable not inserted because it is "
299  "not owned by model that owns array." );
300  model.notify();
301  return dataArray->array.insert( pos, value );
302 }
303 
304 void
305 Array::insert( iterator pos, size_type count, const value_type& value ){
306  if( value.getModel() != getModel() )
307  throw IPOE( "ipo::Array::insert: variable not inserted because it is "
308  "not owned by model that owns array." );
309  dataArray->array.insert( pos, count, value );
310  model.notify();
311 }
312 
313 void
314 Array::insert( iterator pos, std::initializer_list<Variable> ilist ){
315  dataArray->array.insert( pos, ilist );
316  if( not dataArray->testModelMatch( *getModel() ) )
317  throw IPOE( "ipo::Array::insert: at least one variable discarded because it is "
318  "not owned by model that owns array." );
319  model.notify();
320 }
321 
322 void
323 Array::push_back( value_type const& value ){
324  if( value.getModel() != getModel() )
325  throw IPOE( "ipo::Array::push_back: variable not inserted because it is "
326  "not owned by model that owns array." );
327  dataArray->array.push_back( value );
328  model.notify();
329 }
330 
331 void
333  if( value.getModel() != getModel() )
334  throw IPOE( "ipo::Array::push_back: variable not inserted because it is "
335  "not owned by model that owns array." );
336  dataArray->array.push_back( value );
337  model.notify();
338 }
339 
340 void
341 Array::resize( size_type count, const value_type& value){
342  if( count > dataArray->array.size() and value.getModel() != getModel() )
343  throw IPOE( "ipo::Array::resize: array not resized because new values would"
344  "not be owned by model that owns array." );
345  dataArray->array.resize( count, value );
346  model.notify();
347 }
348 
349 bool
350 Array::contains( Variable const& variable ) const {
351  return std::find( begin(), end(), variable ) != end();
352 }
353 
354 void
355 Array::setValue( gsl::vector const& vector ){
356  size_t const sizeV { vector.size() };
357  if( sizeV != size() )
358  throw IPOE( "ipo::Array::setValue(): "
359  "sizes of vector and array do not match." );
360  for( size_t i { 0 }; i < sizeV; ++i ) (*this)[i].setValue( vector[i] );
361 }
362 
363 gsl::vector
365  size_t const sizeA { size() };
366  gsl::vector vector( sizeA );
367  for( size_t i { 0 }; i < sizeA; ++i ) vector[i] = (*this)[i].getValue();
368  return vector;
369 }
370 
371 void
372 Array::summary( std::ostream& ostream, std::string const& prefix ) const {
373  ostream << prefix << getName() << ":" << std::endl;
374  for( auto const& e : *this ){
375  ostream << prefix;
376  e.summary( ostream, prefix );
377  }
378 }
void setName(std::string const &name)
Set name of variable.
Definition: Array.cc:222
std::string name
The name of the array.
Definition: Array.hpp:663
std::vector< Variable >::iterator iterator
Iterator type.
Definition: Array.hpp:82
void swap(Array &array)
Swap contents of container with those of another.
Definition: Array.cc:232
void push_back(value_type const &value)
Insert value at end of array.
Definition: Array.cc:323
void setValue(gsl::vector const &vector)
Set the value of this from a gsl::vector.
Definition: Array.cc:355
ModelBase & model
A Model to attach this to.
Definition: Var.hpp:101
std::vector< Variable > array
The array.
Definition: Array.hpp:667
iterator end()
Get iterator to end of range.
Definition: Array.hpp:350
Data(detail::ModelBase &model, std::vector< Variable >::allocator_type alloc, std::string const &name)
Constructor.
Definition: Array.cc:28
bool contains(Variable const &variable) const
Check whether or not array contains variable.
Definition: Array.cc:350
size_type size() const
Get size of array.
Definition: Array.hpp:400
void assign(size_type count, value_type const &value)
Assign values.
Definition: Array.cc:269
std::shared_ptr< Data > dataArray
The Variable dataArray as a shared pointer.
Definition: Array.hpp:672
#define IPOE(message)
Macro to allow file and line names in exceptions.
std::string subscript(size_t subscript)
Create a string representing a subscript from a whole number.
Definition: Format.cc:27
Abstract base class for model.
Definition: Var.hpp:39
Array & operator=(Array &&array)
Move assignment operator.
Definition: Array.cc:241
void swap(Array &first, Array &second)
Swap contents of container with those of another.
Definition: Array.hpp:736
Array(detail::ModelBase &model, allocator_type const &alloc=allocator_type(), char const *const name=nullptr)
Default constructor.
Definition: Array.cc:113
gsl::vector getValue() const
Create a gsl::vector containing the values of this in order.
Definition: Array.cc:364
virtual void summary(std::ostream &ostream=std::cout, std::string const &prefix="") const override
Create a summary of this Array object.
Definition: Array.cc:372
This class represents a variable.
Definition: Variable.hpp:36
std::string getName() const
Get name of variable.
Definition: Array.cc:217
std::vector< Variable >::value_type value_type
Value type.
Definition: Array.hpp:50
This class represents an array of Variable objects.
Definition: Array.hpp:45
virtual void notify()=0
Notify function that model must implement.
Struct to contain Array dataArray.
Definition: Array.hpp:569
void resize(size_type count)
Resize array.
Definition: Array.hpp:528
data name
Definition: Variable.cc:37
iterator insert(iterator pos, value_type const &value)
Insert value before position given by iterator pos.
Definition: Array.cc:287
ModelBase const *const getModel() const
Get pointer to model.
Definition: Var.hpp:88
std::vector< Variable >::size_type size_type
Size type.
Definition: Array.hpp:58
iterator begin()
Get iterator to beginning of range.
Definition: Array.hpp:335
std::vector< Variable >::allocator_type allocator_type
Allocator type.
Definition: Array.hpp:54
bool testModelMatch(detail::ModelBase const &model)
Test model match and remove any Vraiable objects that fail.
Definition: Array.cc:257
This namespace holds all the interior-point optimisation classes.
Definition: Array.hpp:28