FLOPC++
MP_data.hpp
Go to the documentation of this file.
1 // ******************** FlopCpp **********************************************
2 // File: MP_data.hpp
3 // $Id$
4 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
5 // Copyright (C) 2003 Tim Helge Hultberg
6 // All Rights Reserved.
7 // ****************************************************************************
8 
9 #ifndef _MP_data_hpp_
10 #define _MP_data_hpp_
11 
12 #include <vector>
13 #include <string>
14 
15 #include "MP_index.hpp"
16 #include "MP_set.hpp"
17 #include "MP_constant.hpp"
18 #include "MP_boolean.hpp"
19 
20 namespace flopc {
21 
22  class MP_data;
23 
29  class DataRef : public Constant_base, public Functor {
30  public:
32  const MP_index_exp& i1,
33  const MP_index_exp& i2,
34  const MP_index_exp& i3,
35  const MP_index_exp& i4,
36  const MP_index_exp& i5,
37  int s = 0) :
38  D(d),I1(i1),I2(i2),I3(i3),I4(i4),I5(i5),C(0),stochastic(s) {}
39 
40  ~DataRef() {}
41  DataRef& such_that(const MP_boolean& b);
42  double evaluate() const;
43  int getStage() const;
44  const DataRef& operator=(const DataRef& r);
45  const DataRef& operator=(const Constant& c);
46  void evaluate_lhs(double v) const;
47  void operator()() const;
48  private:
54  };
55 
71  class MP_data : public RowMajor, public Functor , public Named {
72  friend class MP_variable;
73  friend class DisplayData;
74  friend class DataRef;
75  friend class MP_model;
76  public:
77  void operator()() const;
79  void initialize(double d) {
80  for (int i=0; i<size(); i++) {
81  v[i] = d;
82  }
83  }
88  const MP_set_base &s2 = MP_set::getEmpty(),
89  const MP_set_base &s3 = MP_set::getEmpty(),
90  const MP_set_base &s4 = MP_set::getEmpty(),
91  const MP_set_base &s5 = MP_set::getEmpty()) :
92  RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
93  S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
94  v(new double[size()]), manageData(true)
95  {
96  initialize(0);
97  }
98 
102  MP_data(double* value,
103  const MP_set_base &s1 = MP_set::getEmpty(),
104  const MP_set_base &s2 = MP_set::getEmpty(),
105  const MP_set_base &s3 = MP_set::getEmpty(),
106  const MP_set_base &s4 = MP_set::getEmpty(),
107  const MP_set_base &s5 = MP_set::getEmpty()) :
108  RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
109  S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
110  v(value), manageData(false)
111  {
112  }
113 
115  if (manageData == true) delete[] v;
117 // for (unsigned int i=0; i<myrefs.size(); i++) {
118 // cout<<"# "<<i<<" "<<myrefs[i]<<endl;
119 // delete myrefs[i]; //Gives segmentation fault. I dont know why!
120 // }
121  }
122 
124  void value(const double* d) {
125  for (int i=0; i<size(); i++) {
126  v[i] = d[i];
127  }
128  }
129 
131  operator double() {
132  return operator()(0);
133  }
134 
139  double& operator()(int lcli1, int lcli2=0, int lcli3=0, int lcli4=0, int lcli5=0) {
140  lcli1 = S1.check(lcli1);
141  lcli2 = S2.check(lcli2);
142  lcli3 = S3.check(lcli3);
143  lcli4 = S4.check(lcli4);
144  lcli5 = S5.check(lcli5);
145  int i = f(lcli1,lcli2,lcli3,lcli4,lcli5);
146  if (i == outOfBound) {
147  outOfBoundData = 0;
148  return outOfBoundData;
149  } else {
150  return v[i];
151  }
152  }
153 
159  const MP_index_exp& lcli1 = MP_index_exp::getEmpty(),
160  const MP_index_exp& lcli2 = MP_index_exp::getEmpty(),
161  const MP_index_exp& lcli3 = MP_index_exp::getEmpty(),
162  const MP_index_exp& lcli4 = MP_index_exp::getEmpty(),
163  const MP_index_exp& lcli5 = MP_index_exp::getEmpty()
164  ) {
165  myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5));
166  return *myrefs.back();
167  }
168 
169 
171  void display(std::string s = "");
172  protected:
173  std::vector<DataRef*> myrefs;
174  private:
175  MP_data(const MP_data&); // Forbid copy constructor
176  MP_data& operator=(const MP_data&); // Forbid assignment
177 
178  static double outOfBoundData;
179 
181  const MP_set_base &S1,&S2,&S3,&S4,&S5;
182  double* v;
184  };
185 
186  class MP_stochastic_data : public MP_data {
187  public:
189  const MP_set_base &s2 = MP_set::getEmpty(),
190  const MP_set_base &s3 = MP_set::getEmpty(),
191  const MP_set_base &s4 = MP_set::getEmpty(),
192  const MP_set_base &s5 = MP_set::getEmpty()) :
193  MP_data(s1,s2,s3,s4,s5) {}
194 
195  using flopc::MP_data::operator();
197  const MP_index_exp& lcli1 = MP_index_exp::getEmpty(),
198  const MP_index_exp& lcli2 = MP_index_exp::getEmpty(),
199  const MP_index_exp& lcli3 = MP_index_exp::getEmpty(),
200  const MP_index_exp& lcli4 = MP_index_exp::getEmpty(),
201  const MP_index_exp& lcli5 = MP_index_exp::getEmpty()
202  ) {
203  myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5, 1));
204  return *myrefs.back();
205  }
206  };
207 
208 } // End of namespace flopc
209 #endif
MP_index_exp I2
Definition: MP_data.hpp:50
void evaluate_lhs(double v) const
Definition: MP_data.cpp:79
int getStage() const
Definition: MP_data.cpp:60
static MP_set & getEmpty()
gets the distinct &#39;empty&#39; MP_set.
Definition: MP_set.cpp:17
void value(const double *d)
Used to bind and deep copy data into the MP_data data structure.
Definition: MP_data.hpp:124
const DataRef & operator=(const DataRef &r)
Definition: MP_data.cpp:29
const MP_set_base & S3
Definition: MP_data.hpp:181
MP_data(const MP_set_base &s1=MP_set::getEmpty(), const MP_set_base &s2=MP_set::getEmpty(), const MP_set_base &s3=MP_set::getEmpty(), const MP_set_base &s4=MP_set::getEmpty(), const MP_set_base &s5=MP_set::getEmpty())
Definition: MP_data.hpp:87
MP_index i1
Definition: MP_data.hpp:180
Representation of an index.This is one of the main public interface classes. It is used to iterate th...
Definition: MP_index.hpp:53
double * v
Definition: MP_data.hpp:182
MP_data & operator=(const MP_data &)
MP_index i2
Definition: MP_data.hpp:180
void initialize(double d)
similar to value() but copies the same value to all entries.
Definition: MP_data.hpp:79
MP_index_exp I4
Definition: MP_data.hpp:50
MP_boolean B
Definition: MP_data.hpp:53
static double outOfBoundData
Definition: MP_data.hpp:178
Utility interface class for adding a string name onto a structure.
MP_index i5
Definition: MP_data.hpp:180
Representation of an expression involving an index.This is one of the main public interface classes...
Definition: MP_index.hpp:145
int f(int i1=0, int i2=0, int i3=0, int i4=0, int i5=0) const
std::vector< DataRef * > myrefs
Definition: MP_data.hpp:173
Base class for all &quot;constant&quot; types of data.
Definition: MP_constant.hpp:20
double evaluate() const
Definition: MP_data.cpp:43
MP_stochastic_data(const MP_set_base &s1=MP_set::getEmpty(), const MP_set_base &s2=MP_set::getEmpty(), const MP_set_base &s3=MP_set::getEmpty(), const MP_set_base &s4=MP_set::getEmpty(), const MP_set_base &s5=MP_set::getEmpty())
Definition: MP_data.hpp:188
int size() const
DataRef(MP_data *d, const MP_index_exp &i1, const MP_index_exp &i2, const MP_index_exp &i3, const MP_index_exp &i4, const MP_index_exp &i5, int s=0)
Definition: MP_data.hpp:31
static const MP_index_exp & getEmpty()
Return the unique empty expression.
Definition: MP_index.cpp:23
int check(int i) const
Definition: MP_set.hpp:37
MP_index_exp I3
Definition: MP_data.hpp:50
const int outOfBound
Distinct return value on conditions where an index goes out of bounds.
Reference to a set of data.
Definition: MP_data.hpp:29
This is the anchor point for all constructs in a FlopC++ model.The constructors take an OsiSolverInte...
Definition: MP_model.hpp:89
MP_index i4
Definition: MP_data.hpp:180
Constant C
Definition: MP_data.hpp:51
Reference counted class for all &quot;boolean&quot; types of data.This contains counters to ConstantBase pointe...
Definition: MP_boolean.hpp:45
friend class DisplayData
Definition: MP_data.hpp:73
DataRef & such_that(const MP_boolean &b)
Definition: MP_data.cpp:37
MP_index_exp I1
Definition: MP_data.hpp:50
MP_data * D
Definition: MP_data.hpp:49
friend class DataRef
Definition: MP_data.hpp:74
Symantic representation of a variable.This is one of the main public interface classes. It should be directly declared by clients of the FlopC++. The parametersof construction are MP_set s which specify the indexes over which the variable is defined.
Definition: MP_variable.hpp:75
Function object. Often used.
const MP_set_base & S5
Definition: MP_data.hpp:181
Reference counted class for all &quot;constant&quot; types of data.
Definition: MP_constant.hpp:48
Input data set.This is one of the main public interface classes. It is normally directly constructed ...
Definition: MP_data.hpp:71
double & operator()(int lcli1, int lcli2=0, int lcli3=0, int lcli4=0, int lcli5=0)
Definition: MP_data.hpp:139
const MP_set_base & S4
Definition: MP_data.hpp:181
MP_index i3
Definition: MP_data.hpp:180
Internal representation of a &quot;set&quot;.
Definition: MP_set.hpp:28
void display(std::string s="")
For displaying data in a human readable format.
Definition: MP_data.cpp:102
MP_index_exp I5
Definition: MP_data.hpp:50
const MP_set_base & S2
Definition: MP_data.hpp:181
MP_data(double *value, const MP_set_base &s1=MP_set::getEmpty(), const MP_set_base &s2=MP_set::getEmpty(), const MP_set_base &s3=MP_set::getEmpty(), const MP_set_base &s4=MP_set::getEmpty(), const MP_set_base &s5=MP_set::getEmpty())
Definition: MP_data.hpp:102
void operator()() const
Definition: MP_data.cpp:33
const MP_set_base & S1
Definition: MP_data.hpp:181
void operator()() const
Definition: MP_data.cpp:92