[FlopCpp] Patch

Gaetano Mendola mendola at gmail.com
Wed Mar 3 04:48:28 EST 2010


Hi all,
here another patch to stable/1.0

Change notes:
- Disabled Copy Constructor and Assignment operators for classes with
a pointer member.
- Classes with public inheritance from Named now have a private
inheritance (those weren't
  meant to be treated "is-a" Named; getName and setName are imported
in the public section
  and then still usable by end users.
- Removed MP_model::default_model from heap.
- Added some const where needed.
- Standard say delete can be called on a null pointer. Removed code
sounding like:
        if (p!=0)  delete p;
- Added some asserts before to dereference a pointer.
- Added explicitly default CTOR on Named.
- Fixed return value type for Handle assignment operator.
- Used pre-increment when post-increment is not needed and a waste (on
iterators for example).
- Added explicitly all members in classes initialization list.
- Removed blank spaces

I haven't find on FlopC project page any guide to coding rules.
The lack of coding rules gives, imho, too much freedom to contributors
and code quality suffers
from this (see some variable capitalized, some starting with an
underscore, some with the convention
mNameVariable (to indicate is a member) and so on.
On the other side who merges the patch, Tim on this case, has hard
time (I guess) to decide to
accept something or not (like specify all members class in the
initialization list).

If you want we can work at a document in order to write down those
coding rules and when accepted
then adhere to it.


Regards
Gaetano Mendola

-- 
cpp-today.blogspot.com
-------------- next part --------------
diff -uN coin-FlopCpp/FlopCpp/src/MP_boolean.cpp FlopC/FlopCpp/src/MP_boolean.cpp
--- coin-FlopCpp/FlopCpp/src/MP_boolean.cpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_boolean.cpp	2010-03-03 09:52:39.000000000 +0100
@@ -41,6 +41,11 @@
         return true;
       }
     }
+
+    //disable copy constructor and operator assignment
+    Boolean_SUBSETREF(const Boolean_SUBSETREF&);
+    Boolean_SUBSETREF& operator=(const Boolean_SUBSETREF&);
+
     SUBSETREF* C;
   };
 
diff -uN coin-FlopCpp/FlopCpp/src/MP_constant.hpp FlopC/FlopCpp/src/MP_constant.hpp
--- coin-FlopCpp/FlopCpp/src/MP_constant.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_constant.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -30,7 +30,7 @@
     int count;
 
   };
-    
+
   class MP_index_exp;
   class DataRef;
   class MP_domain;
diff -uN coin-FlopCpp/FlopCpp/src/MP_constraint.cpp FlopC/FlopCpp/src/MP_constraint.cpp
--- coin-FlopCpp/FlopCpp/src/MP_constraint.cpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_constraint.cpp	2010-03-03 09:52:39.000000000 +0100
@@ -42,16 +42,19 @@
 double MP_constraint::price(int i1, int i2, int i3, int i4, int i5) const {
   return  M->Solver->getRowPrice()[offset + f(i1,i2,i3,i4,i5)];
 }
- 
+
 MP_constraint::MP_constraint(
-  const MP_set_base &s1, 
-  const MP_set_base &s2, 
+  const MP_set_base &s1,
+  const MP_set_base &s2,
   const MP_set_base &s3,
-  const MP_set_base &s4, 
+  const MP_set_base &s4,
   const MP_set_base &s5) :
   RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
+  Named(),
   M(MP_model::current_model),
   offset(-1),
+  left(),
+  right(),
   S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
   I1(0),I2(0),I3(0),I4(0),I5(0)
 {
@@ -61,7 +64,7 @@
 void MP_constraint::coefficients(vector<MP::Coef>& cfs) {
   MP::GenerateFunctor f(this, cfs);
 // f.setConstraint(this);
-  
+
   vector<Constant> v;
   if (left.isDefined() && right.isDefined()) {
     left->generate((S1(I1)*S2(I2)*S3(I3)*S4(I4)*S5(I5)).such_that(B),v,f,1.0);
diff -uN coin-FlopCpp/FlopCpp/src/MP_constraint.hpp FlopC/FlopCpp/src/MP_constraint.hpp
--- coin-FlopCpp/FlopCpp/src/MP_constraint.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_constraint.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -200,17 +200,20 @@
       @todo more work on MP_constraint.
     
   */
-  class MP_constraint : public RowMajor, public Named {
-  public: 
+  class MP_constraint : public RowMajor, private Named {
+  public:
     /// construct the MP_constraint with appropriate sets for indexing.
     MP_constraint(
-      const MP_set_base &s1 = MP_set::getEmpty(), 
-      const MP_set_base &s2 = MP_set::getEmpty(), 
+      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 &s4 = MP_set::getEmpty(),
       const MP_set_base &s5 = MP_set::getEmpty()
       );
 
+    using Named::setName;
+    using Named::getName;
+
     MP_constraint& operator()(
       const MP_index_exp& i1 = MP_index_exp::getEmpty(), 
       const MP_index_exp& i2 = MP_index_exp::getEmpty(), 
@@ -243,7 +246,7 @@
     void insertVariables(set<MP_variable*>& v);
 
     void operator=(const Constraint& v); 
-    
+
     void display(string s="") const;
 
     MP_model* M;
diff -uN coin-FlopCpp/FlopCpp/src/MP_data.hpp FlopC/FlopCpp/src/MP_data.hpp
--- coin-FlopCpp/FlopCpp/src/MP_data.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_data.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -6,10 +6,10 @@
 #define _MP_data_hpp_
 
 #include <vector>
-#include "MP_index.hpp"    
-#include "MP_set.hpp"      
-#include "MP_constant.hpp" 
-#include "MP_boolean.hpp" 
+#include "MP_index.hpp"
+#include "MP_set.hpp"
+#include "MP_constant.hpp"
+#include "MP_boolean.hpp"
 
 namespace flopc {
 
@@ -22,16 +22,22 @@
   */
   class DataRef : public Constant_base, public Functor {
   public:
-    DataRef(MP_data* d, 
+    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) : 
-      D(d),I1(i1),I2(i2),I3(i3),I4(i4),I5(i5),C(0),stochastic(s) {}
+            int s = 0) :
+      Constant_base(), Functor(),
+      D(d),
+      I1(i1),I2(i2),I3(i3),I4(i4),I5(i5),
+      C(0),
+      stochastic(s),
+      B()
+      {}
 
-    ~DataRef() {} 
+    ~DataRef() {}
     DataRef& such_that(const MP_boolean& b);
     double evaluate() const;
     int getStage() const;
@@ -43,7 +49,7 @@
   private:
     //Disable copy constructor
     DataRef(const DataRef&);
-    
+
     MP_data* D;
     MP_index_exp I1,I2,I3,I4,I5;
     Constant C;
@@ -67,7 +73,7 @@
       @li constraint coefficients
       @li 'right hand sides'
   */
-  class MP_data : public RowMajor, public Functor , public Named {
+  class MP_data : public RowMajor, public Functor , private Named {
     friend class MP_variable;
     friend class DisplayData;
     friend class DataRef;
@@ -83,16 +89,20 @@
     /** Constructs the MP_data object, and allocates space for data, and
         initialize the data to zero.
     */
-    MP_data(const MP_set_base &s1 = MP_set::getEmpty(), 
-            const MP_set_base &s2 = MP_set::getEmpty(), 
+    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 &s4 = MP_set::getEmpty(),
             const MP_set_base &s5 = MP_set::getEmpty()) :
       RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
+      Functor(),
+      Named(),
+      myrefs(),
+      i1(),i2(),i3(),i4(),i5(),
       S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
       v(new double[size()]), manageData(true) 
       {
-        initialize(0); 
+        initialize(0);
       }
 
     /** Construct the object, and uses the data in the original array
@@ -105,6 +115,10 @@
             const MP_set_base &s4 = MP_set::getEmpty(), 
             const MP_set_base &s5 = MP_set::getEmpty()) :
       RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
+      Functor(),
+      Named(),
+      myrefs(),
+      i1(),i2(),i3(),i4(),i5(),
       S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
       v(value), manageData(false) 
       { }
@@ -112,7 +126,10 @@
     ~MP_data() {
       if (manageData == true) delete[] v;
     }
-        
+
+    using Named::setName;
+    using Named::getName;
+
     /// Used to bind and deep copy data into the MP_data data structure.
     void value(const double* d) {
       for (int i=0; i<size(); i++) {
@@ -124,18 +141,18 @@
     operator double() {
       return operator()(0);
     }
-    
+
     /** Looks up the data based on the index values passed in.
         @note this is used internally, but may also be useful for spot
         checking data or in other expressions.
     */
-    double& operator()(int lcli1, int lcli2=0, int lcli3=0, int lcli4=0, int lcli5=0) { 
-      lcli1 = S1.check(lcli1); 
-      lcli2 = S2.check(lcli2); 
-      lcli3 = S3.check(lcli3); 
-      lcli4 = S4.check(lcli4); 
-      lcli5 = S5.check(lcli5); 
-      int i = f(lcli1,lcli2,lcli3,lcli4,lcli5);     
+    double& operator()(int lcli1, int lcli2=0, int lcli3=0, int lcli4=0, int lcli5=0) {
+      lcli1 = S1.check(lcli1);
+      lcli2 = S2.check(lcli2);
+      lcli3 = S3.check(lcli3);
+      lcli4 = S4.check(lcli4);
+      lcli5 = S5.check(lcli5);
+      int i = f(lcli1,lcli2,lcli3,lcli4,lcli5);
       if (i == outOfBound) {
         outOfBoundData = 0;
         return outOfBoundData;
@@ -143,22 +160,20 @@
         return v[i];
       }
     }
-    
-    /** returns a DataRef which refers into the MP_data.  
+
+    /** returns a DataRef which refers into the MP_data.
         @note For internal use.
         @todo can this be private?
     */
     DataRef& operator() (
-			 const MP_index_exp& lcli1 = MP_index_exp::getEmpty(), 
-			 const MP_index_exp& lcli2 = MP_index_exp::getEmpty(), 
-			 const MP_index_exp& lcli3 = MP_index_exp::getEmpty(), 
-			 const MP_index_exp& lcli4 = MP_index_exp::getEmpty(), 
-			 const MP_index_exp& lcli5 = MP_index_exp::getEmpty() 
-			 ) {
-      myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5));
-      return *myrefs.back();
+      const MP_index_exp& lcli1 = MP_index_exp::getEmpty(), 
+      const MP_index_exp& lcli2 = MP_index_exp::getEmpty(), 
+      const MP_index_exp& lcli3 = MP_index_exp::getEmpty(), 
+      const MP_index_exp& lcli4 = MP_index_exp::getEmpty(), 
+      const MP_index_exp& lcli5 = MP_index_exp::getEmpty()) {
+        myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5));
+        return *myrefs.back();
     }
-    
 
     /// For displaying data in a human readable format.
     void display(string s = "");
@@ -188,16 +203,14 @@
     using flopc::MP_data::operator(); // From bugsquashing party. Some compiler needs this? 
 
     DataRef& operator() (
-			 const MP_index_exp& lcli1 = MP_index_exp::getEmpty(), 
-			 const MP_index_exp& lcli2 = MP_index_exp::getEmpty(), 
-			 const MP_index_exp& lcli3 = MP_index_exp::getEmpty(), 
-			 const MP_index_exp& lcli4 = MP_index_exp::getEmpty(), 
-			 const MP_index_exp& lcli5 = MP_index_exp::getEmpty() 
-			 ) {
-      myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5, 1));
-      return *myrefs.back();
+      const MP_index_exp& lcli1 = MP_index_exp::getEmpty(), 
+      const MP_index_exp& lcli2 = MP_index_exp::getEmpty(), 
+      const MP_index_exp& lcli3 = MP_index_exp::getEmpty(), 
+      const MP_index_exp& lcli4 = MP_index_exp::getEmpty(), 
+      const MP_index_exp& lcli5 = MP_index_exp::getEmpty()) {
+        myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5, 1));
+        return *myrefs.back();
     }
   };
-
 } // End of namespace flopc
 #endif
diff -uN coin-FlopCpp/FlopCpp/src/MP_domain.cpp FlopC/FlopCpp/src/MP_domain.cpp
--- coin-FlopCpp/FlopCpp/src/MP_domain.cpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_domain.cpp	2010-03-03 09:52:39.000000000 +0100
@@ -18,8 +18,11 @@
   class Functor_conditional : public Functor {
   public:
     Functor_conditional(const Functor* f, const std::vector<MP_boolean> & condition)
-      : F(f), Condition(condition) {}
+      : F(f), Condition(condition)
+    {}
+
     virtual ~Functor_conditional() {}
+    
     void operator()() const {
       bool goOn = true;
       for (size_t i = 0; i<Condition.size(); i++) {
@@ -63,7 +66,7 @@
   MP_model::getCurrentModel()->getMessenger()->logMessage(5,ss.str().c_str());
 }
 
-MP_domain::MP_domain() : Handle<MP_domain_base*>(0), last(0) {}
+MP_domain::MP_domain(): Handle<MP_domain_base*>(0), last(0) {}
 MP_domain::MP_domain(MP_domain_base* r) : Handle<MP_domain_base*>(r), last(r) {}
 MP_domain::~MP_domain() {}
 
diff -uN coin-FlopCpp/FlopCpp/src/MP_domain.hpp FlopC/FlopCpp/src/MP_domain.hpp
--- coin-FlopCpp/FlopCpp/src/MP_domain.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_domain.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -8,8 +8,8 @@
 #include <vector>
 #include <map>
 #include "MP_utilities.hpp"
-#include "MP_boolean.hpp" 
-#include "MP_index.hpp" 
+#include "MP_boolean.hpp"
+#include "MP_index.hpp"
 
 namespace flopc {
 
@@ -35,7 +35,7 @@
     virtual Functor* makeInsertFunctor() const;
     virtual MP_index* getIndex() const = 0;
     virtual const MP_set_base* getSet() const = 0;
-    void display()const;
+    void display() const;
     virtual size_t size() const ;
     //   const Functor* donext;
   private:
@@ -57,11 +57,11 @@
       reference can be obtained using MP_domain::getEmpty();
       @li The empty set is used when defaulting in parameters
       for dimensions which are not used.
-        
+
   */
   class MP_domain : public Handle<MP_domain_base*> {
     friend MP_domain operator*(const MP_domain& a, const MP_domain& b);
-    
+
     friend class MP_constraint;
     friend class MP_index_exp;
   public:
@@ -84,7 +84,7 @@
     MP_domain such_that(const MP_boolean& b);
 
     /** @brief Special conditional operation on the domain
-        
+
     This method will call the functor for each member of the MP_domain.
     */
     void forall(const Functor* op) const;
diff -uN coin-FlopCpp/FlopCpp/src/MP_expression.cpp FlopC/FlopCpp/src/MP_expression.cpp
--- coin-FlopCpp/FlopCpp/src/MP_expression.cpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_expression.cpp	2010-03-03 09:52:39.000000000 +0100
@@ -20,7 +20,7 @@
                            const MP_index_exp& i3,
                            const MP_index_exp& i4,
                            const MP_index_exp& i5) :
-    V(v),I1(i1),I2(i2),I3(i3),I4(i4),I5(i5) { 
+    V(v),offset(0),I1(i1),I2(i2),I3(i3),I4(i4),I5(i5) {
     assert(v != 0);
     offset = v->offset; 
   }
@@ -40,7 +40,7 @@
     int i3 = V->S3->check(I3->evaluate());
     int i4 = V->S4->check(I4->evaluate());
     int i5 = V->S5->check(I5->evaluate());
-    
+
     if (i1==outOfBound || i2==outOfBound || i3==outOfBound ||
         i4==outOfBound || i5==outOfBound) {
       return outOfBound;
diff -uN coin-FlopCpp/FlopCpp/src/MP_expression.hpp FlopC/FlopCpp/src/MP_expression.hpp
--- coin-FlopCpp/FlopCpp/src/MP_expression.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_expression.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -45,22 +45,25 @@
   protected:
     class GenerateFunctor : public Functor {
     public:
-      GenerateFunctor(MP_constraint* r, vector<Coef>& cfs): R(r), Coefs(cfs) {}
-      
+      GenerateFunctor(MP_constraint* r, vector<Coef>& cfs)
+      : Functor(), multiplicators(), R(r), M(), C(0), Coefs(cfs)
+      {}
+
       void setMultiplicator(vector<Constant>& mults, double m) {
         multiplicators = mults;
         M = m;
       }
+
       void setTerminalExpression(const TerminalExpression* c) {
         C = c;
       }
-      
+
       void operator()() const;
     private:
       //Disable copy costructor and assignment
       GenerateFunctor(const GenerateFunctor&);
       GenerateFunctor& operator=(const GenerateFunctor&);
-      
+
       vector<Constant> multiplicators;
       MP_constraint* R;
       double M; // sign (1=lhs, -1=rhs)
@@ -140,22 +143,26 @@
   public:
     int getColumn() const;
   private:
-    VariableRef(MP_variable* v, 
+    VariableRef(MP_variable* v,
                 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);
 
+    //Disable copy constructor and assignment operator
+    VariableRef(const VariableRef&);
+    VariableRef& operator=(const VariableRef&);
+
     double level() const;
 
     void insertVariables(set<MP_variable*>& v) const {
       v.insert(V);
     }
-    double getValue() const { 
+    double getValue() const {
       return 1.0;
     }
-    int getStage() const { 
+    int getStage() const {
       return 0;
     }
     void generate(const MP_domain& domain,
diff -uN coin-FlopCpp/FlopCpp/src/MP_index.cpp FlopC/FlopCpp/src/MP_index.cpp
--- coin-FlopCpp/FlopCpp/src/MP_index.cpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_index.cpp	2010-03-03 09:52:39.000000000 +0100
@@ -81,6 +81,7 @@
 
 
 MP_domain MP_index::getDomain(MP_set* s) const{
+  std::cout << __PRETTY_FUNCTION__ << std::endl;
   return new MP_domain_set(s,const_cast<MP_index*>(this)) ;
 }
 
diff -uN coin-FlopCpp/FlopCpp/src/MP_index.hpp FlopC/FlopCpp/src/MP_index.hpp
--- coin-FlopCpp/FlopCpp/src/MP_index.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_index.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -68,7 +68,7 @@
     /** Setter for the index.
         @todo should this assert "instatiated"?
     */
-    void assign(int i) { 
+    void assign(int i) {
       index = i;
     }
     /** unsetter for instatiated.
@@ -79,7 +79,7 @@
     /** setter for instatiated.
      */
     void instantiate() {
-      instantiated = true; 
+      instantiated = true;
     }
     /** getter for MP_index * data type.  
         @todo should this be virtual?
@@ -163,7 +163,7 @@
     friend MP_index_exp operator*(MP_index& i,const Constant& j);
   private:
     MP_index_mult(MP_index& i, const Constant& j) : left(i), right(j) {}
-    
+
     int evaluate() const {
       return left->evaluate()*int(right->evaluate()); 
     }
diff -uN coin-FlopCpp/FlopCpp/src/MP_model.cpp FlopC/FlopCpp/src/MP_model.cpp
--- coin-FlopCpp/FlopCpp/src/MP_model.cpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_model.cpp	2010-03-03 09:52:39.000000000 +0100
@@ -18,7 +18,7 @@
 
 using namespace flopc;
 
-MP_model& MP_model::default_model = *new MP_model(0);
+MP_model MP_model::default_model(0);
 MP_model* MP_model::current_model = &MP_model::default_model;
 MP_model &MP_model::getDefaultModel() { return default_model;}
 MP_model *MP_model::getCurrentModel() { return current_model;}
@@ -32,51 +32,61 @@
 }
 
 void NormalMessenger::generationTime(double t) {
-  cout<<"FlopCpp: Generation time: "<<t<<endl;
+  cout << "FlopCpp: Generation time: " << t << endl;
 }
 
-void VerboseMessenger::constraintDebug(string name, const vector<MP::Coef>& cfs) {
+void VerboseMessenger::constraintDebug(string name, const vector<MP::Coef>& cfs) const {
   cout<<"FlopCpp: Constraint "<<name<<endl;
-  for (unsigned int j=0; j<cfs.size(); j++) {
-    int col=cfs[j].col;
-    int row=cfs[j].row;
-    double elm=cfs[j].val;
-    int stage=cfs[j].stage;
-    cout<<row<<"   "<<col<<"  "<<elm<<"  "<<stage<<endl;
+  for (unsigned int j=0; j < cfs.size(); ++j) {
+    const int col    = cfs[j].col;
+    const int row    = cfs[j].row;
+    const double elm = cfs[j].val;
+    const int stage  = cfs[j].stage;
+    cout << row << "   " << col << "  " << elm << "  " << stage << endl;
   }
 }
 
-void VerboseMessenger::objectiveDebug(const vector<MP::Coef>& cfs) {
+void VerboseMessenger::objectiveDebug(const vector<MP::Coef>& cfs) const {
   cout<<"Objective "<<endl;
-  for (unsigned int j=0; j<cfs.size(); j++) {
-    int col=cfs[j].col;
-    int row=cfs[j].row;
-    double elm=cfs[j].val;
-    cout<<row<<"   "<<col<<"  "<<elm<<endl;
+  for (unsigned int j=0; j < cfs.size(); ++j) {
+    const int col    = cfs[j].col;
+    const int row    = cfs[j].row;
+    const double elm = cfs[j].val;
+    cout << row << "   " << col << "  " << elm << endl;
   }
 }
 
-MP_model::MP_model(OsiSolverInterface* s, Messenger* m) : 
-  messenger(m), Objective(0), Solver(s), 
-  m(0), n(0), nz(0), bl(0),
+MP_model::MP_model(OsiSolverInterface* s, Messenger* m) 
+: messenger(m),
+  Objective(0),
+  Constraints(),
+  Variables(),
+  Solver(s),
+  m(0), n(0), nz(0),
+  Cst(0),
+  Clg(0),
+  Rnr(0),
+  Elm(0),
+  bl(0),
+  bu(0),
+  c(0), l(0), u(0),
   mSolverState(((s==0)?(MP_model::DETACHED):(MP_model::SOLVER_ONLY))) {
   MP_model::current_model = this;
 }
 
 MP_model::~MP_model() {
+  MP_model::current_model = &MP_model::default_model;
   delete messenger;
-  if (Solver!=0) {
-    delete Solver;
-  }
+  delete Solver;
 }
 
-
 MP_model& MP_model::add(MP_constraint& constraint) {
   Constraints.insert(&constraint);
   return *this;
 }
 
 void MP_model::add(MP_constraint* constraint) {
+  assert(constraint != 0);
   constraint->M = this;
   if (constraint->left.isDefined() && constraint->right.isDefined()) {
     constraint->offset = m;
@@ -103,26 +113,27 @@
   vector<MP::Coef> cfs;
   vector<Constant> v;
   MP::GenerateFunctor f(0,cfs);
-  constraint->left->generate(MP_domain::getEmpty(),v,f,1.0);
-  constraint->right->generate(MP_domain::getEmpty(),v,f,-1.0);
+  constraint->left->generate(MP_domain::getEmpty(),  v, f,  1.0);
+  constraint->right->generate(MP_domain::getEmpty(), v, f, -1.0);
   CoinPackedVector newRow;
   double rhs = 0.0;
-  for (unsigned int j=0; j<cfs.size(); j++) {
-    int col=cfs[j].col;
+  for (unsigned int j = 0; j < cfs.size(); ++j) {
+    const int col = cfs[j].col;
     //int row=cfs[j].row;
-    double elm=cfs[j].val;
+    const double elm = cfs[j].val;
     //cout<<row<<"   "<<col<<"  "<<elm<<endl;
-    if (col>=0) {
-      newRow.insert(col,elm);
-    } else if (col==-1) {
+    if (col >= 0) {
+      newRow.insert(col, elm);
+    } else if (col == -1) {
       rhs = elm;
     }
   }
   // NB no "assembly" of added row yet. Will be done....
- 
+
   double local_bl = -rhs;
   double local_bu = -rhs;
 
+  assert(Solver);
   double inf = Solver->getInfinity();
   switch (constraint->sense) {
       case LE:
@@ -133,14 +144,14 @@
         break;
       case EQ:
         // Nothing to do
-        break;          
+        break;
   }
 
-  Solver->addRow(newRow,local_bl,local_bu);
+  Solver->addRow(newRow, local_bl, local_bu);
 }
 
-void MP_model::setObjective(const MP_expression& o) { 
-  Objective = o; 
+void MP_model::setObjective(const MP_expression& o) {
+  Objective = o;
 }
 
 void MP_model::minimize_max(MP_set &s, const MP_expression  &obj) {
@@ -149,8 +160,7 @@
   add(constraint);
   constraint(s) = v() >= obj;
   minimize(v());
-} 
-
+}
 
 void MP_model::assemble(vector<MP::Coef>& v, vector<MP::Coef>& av) {
   std::sort(v.begin(),v.end(),MP::CoefLess());
@@ -162,7 +172,7 @@
     r = i->row;
     val = i->val;
     s = i->stage;
-    i++;
+    ++i;
     while (i!=v.end() && c==i->col && r==i->row) {
       val += i->val;
       if (i->stage>s) {
@@ -217,7 +227,7 @@
     if (Solver == 0) {
       mSolverState = MP_model::DETACHED;
       return;
-    }  
+    }
   } else {  // use pre-attached solver.
     if(Solver && Solver!=_solver) {
       detach();
@@ -232,7 +242,7 @@
 
   typedef std::set<MP_variable* >::iterator varIt;
   typedef std::set<MP_constraint* >::iterator conIt;
-    
+
   Objective->insertVariables(Variables);
   for (conIt i=Constraints.begin(); i!=Constraints.end(); i++) {
     add(*i);
@@ -253,20 +263,20 @@
 
   messenger->statistics(Constraints.size(),m,Variables.size(),n,nz);
 
-  if (nz>0) {    
-    Elm = new double[nz]; 
-    Rnr = new int[nz];    
+  if (nz>0) {
+    Elm = new double[nz];
+    Rnr = new int[nz];
   }
-  Cst = new int[n+2];   
-  Clg = new int[n+1];   
+  Cst = new int[n+2];
+  Clg = new int[n+1];
   if (n>0) {
-    l =   new double[n];  
-    u =   new double[n];  
-    c =  new double[n]; 
+    l =  new double[n];
+    u =  new double[n];
+    c =  new double[n];
   }
   if (m>0) {
-    bl  = new double[m];  
-    bu  = new double[m];  
+    bl  = new double[m];
+    bu  = new double[m];
   }
   const double inf = Solver->getInfinity();
 
@@ -274,7 +284,7 @@
     Clg[j] = 0;
   }
   Clg[n] = 0;
-    
+
   // Treat right hand side as n'th column
   for (int j=0; j<=n; j++) {
     Clg[j] = 0;
@@ -320,19 +330,19 @@
       int begin = (*i)->offset;
       int end = (*i)->offset+(*i)->size();
       switch ((*i)->sense) {
-          case LE:
-            for (int k=begin; k<end; k++) {
-              bl[k] = - inf;
-            } 
-            break;
-          case GE:
-            for (int k=begin; k<end; k++) {
-              bu[k] = inf;
-            }     
-            break;
-          case EQ:
-            // Nothing to do
-            break;                
+        case LE:
+          for (int k=begin; k<end; k++) {
+            bl[k] = - inf;
+          }
+          break;
+        case GE:
+          for (int k=begin; k<end; k++) {
+            bu[k] = inf;
+          }
+          break;
+        case EQ:
+          // Nothing to do
+          break;
       }
     }
   }
@@ -342,10 +352,10 @@
   MP::GenerateFunctor f(0,cfs);
   coefs.erase(coefs.begin(),coefs.end());
   Objective->generate(MP_domain::getEmpty(), v, f, 1.0);
-  
+
   messenger->objectiveDebug(cfs);
   assemble(cfs,coefs);
-  
+
   for (int j=0; j<n; j++) {
     c[j] = 0.0;
   }
@@ -353,15 +363,15 @@
     int col = coefs[i].col;
     double elm = coefs[i].val;
     c[col] = elm;
-  } 
+  }
 
   // Column bounds
-  for (int j=0; j<n; j++) {
+  for (int j = 0; j < n; ++j) {
     l[j] = 0.0;
     u[j] = inf;
   }
 
-  for (varIt i=Variables.begin(); i!=Variables.end(); i++) {
+  for (varIt i=Variables.begin(); i!=Variables.end(); ++i) {
     for (int k=0; k<(*i)->size(); k++) {
       l[(*i)->offset+k] = (*i)->lowerLimit.v[k];
       u[(*i)->offset+k] = (*i)->upperLimit.v[k];
@@ -370,23 +380,23 @@
 
   Solver->loadProblem(n, m, Cst, Rnr, Elm, l, u, c, bl, bu);
 
-  if (nz>0) {    
-    delete [] Elm; 
-    delete [] Rnr;    
+  if (nz>0) {
+    delete [] Elm;
+    delete [] Rnr;
   }
-  delete [] Cst;   
-  delete [] Clg;   
+  delete [] Cst;
+  delete [] Clg;
   if (n>0) {
-    delete [] l;  
-    delete [] u;  
+    delete [] l;
+    delete [] u;
     delete [] c;
   }
   if (m>0) {
-    delete [] bl;  
+    delete [] bl;
     delete [] bu;
-  }  
+  }
 
-  for (varIt i=Variables.begin(); i!=Variables.end(); i++) {
+  for (varIt i=Variables.begin(); i!=Variables.end(); ++i) {
     int begin = (*i)->offset;
     int end = (*i)->offset+(*i)->size();
     if ((*i)->type == discrete) {
@@ -437,7 +447,7 @@
       cout<<e.message()<<endl;
     }
   }
-     
+
   if (Solver->isProvenOptimal() == true) {
     cout<<"FlopCpp: Optimal obj. value = "<<Solver->getObjValue()<<endl;
     cout<<"FlopCpp: Solver(m, n, nz) = "<<Solver->getNumRows()<<"  "<<
diff -uN coin-FlopCpp/FlopCpp/src/MP_model.hpp FlopC/FlopCpp/src/MP_model.hpp
--- coin-FlopCpp/FlopCpp/src/MP_model.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_model.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -34,8 +34,8 @@
     virtual void logMessage(int level, const char * const msg){}
     friend class MP_model;
   private:
-    virtual void constraintDebug(string name, const vector<MP::Coef>& cfs) {}
-    virtual void objectiveDebug(const vector<MP::Coef>& cfs) {}
+    virtual void constraintDebug(string name, const vector<MP::Coef>& cfs) const {}
+    virtual void objectiveDebug(const vector<MP::Coef>& cfs) const {}
     virtual void statistics(int bm, int m, int bn, int n, int nz) {}
     virtual void generationTime(double t) {}
   protected:
@@ -58,8 +58,8 @@
   class VerboseMessenger : public NormalMessenger {
     friend class MP_model;
   private:
-    virtual void constraintDebug(string name, const vector<MP::Coef>& cfs);
-    virtual void objectiveDebug(const vector<MP::Coef>& cfs);
+    virtual void constraintDebug(string name, const vector<MP::Coef>& cfs) const;
+    virtual void objectiveDebug(const vector<MP::Coef>& cfs) const;
   };
 
   /** @brief This is the anchor point for all constructs in a FlopC++ model.
@@ -232,14 +232,13 @@
   private:
     typedef std::set<MP_variable* >::iterator varIt;
     typedef std::set<MP_constraint* >::iterator conIt;
-    static MP_model& default_model;
+    static MP_model  default_model;
     static MP_model* current_model;
     MP_model(const MP_model&);
     MP_model& operator=(const MP_model&);
 
     Messenger* messenger;
-   
-    
+
     static void assemble(vector<MP::Coef>& v, vector<MP::Coef>& av);
     void add(MP_constraint* constraint);
     MP_expression Objective;
@@ -247,7 +246,7 @@
     set<MP_variable *> Variables;
   public:
     /// @todo should this be private?
-    OsiSolverInterface* Solver; 
+    OsiSolverInterface* Solver;
   private:
     int m;
     int n;
@@ -264,7 +263,7 @@
     MP_status mSolverState;
 
   };
-    
+
   /// allows print of result from call to solve();
   std::ostream &operator<<(std::ostream &os, 
                            const MP_model::MP_status &condition);
diff -uN coin-FlopCpp/FlopCpp/src/MP_set.hpp FlopC/FlopCpp/src/MP_set.hpp
--- coin-FlopCpp/FlopCpp/src/MP_set.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_set.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -19,19 +19,26 @@
 
 namespace flopc {
 
-/** @brief Internal representation of a "set" 
+/** @brief Internal representation of a "set"
     @ingroup INTERNAL_USE
     @note FOR INTERNAL USE: This is not normally used directly by the calling code.
 */
-  class MP_set_base : public MP_index , public Named {
+  class MP_set_base : public MP_index , private Named {
   public:
-    MP_set_base() : Cyclic(false) {}
+    MP_set_base()
+    : MP_index(),
+      Named(),
+      Cyclic(false)
+    { }
+
+    using Named::setName;
+    using Named::getName;
 
     virtual int size() const = 0;
     virtual operator MP_domain() const = 0;
     virtual MP_domain operator()(const MP_index_exp& i) const = 0;
- 
-    int check(int i) const {
+
+    int check(const int i) const {
       if ((i>=0) && (i<size())) {
         return i;
       } else {
@@ -61,7 +68,6 @@
     bool Cyclic;
   };
 
-    
 /** @brief Representation of a set for indexing into some other construct.
     @ingroup PublicInterface
     This is one of the main public interface classes.  One uses this when
@@ -111,7 +117,7 @@
   private:
     int cardinality;
   };
-    
+
   class MP_stage : public MP_set {
   public:
     MP_stage(int i = 0): MP_set(i) {}
diff -uN coin-FlopCpp/FlopCpp/src/MP_utilities.hpp FlopC/FlopCpp/src/MP_utilities.hpp
--- coin-FlopCpp/FlopCpp/src/MP_utilities.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_utilities.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -32,7 +32,7 @@
     Functor(const Functor&);
   private:
     Functor& operator=(const Functor&);
-  };    
+  };
 
   /** This template makes a vector of appropriate size out of the
       variable number of arguments.
@@ -61,7 +61,7 @@
   }
 
   /// Distinct return value on conditions where an index goes out of bounds.
-  const int outOfBound = -2;    
+  const int outOfBound = -2;
 
   /** Utility class to flatten multidimensional information into single
       dimentional offset information.
@@ -73,7 +73,8 @@
   protected:
     RowMajor(int s1, int s2, int s3, int s4, int s5) :
       size1(s1), size2(s2), size3(s3), size4(s4), size5(s5),
-      size_(s1*s2*s3*s4*s5) {}
+      size_(s1*s2*s3*s4*s5) { }
+
     int f(int i1=0, int i2=0, int i3=0, int i4=0, int i5=0) const {
       if ( i1==outOfBound || i2==outOfBound || i3==outOfBound ||
            i4==outOfBound || i5==outOfBound ) {
@@ -83,7 +84,7 @@
         i *= size2; i += i2;  i *= size3; i += i3;
         i *= size4; i += i4;  i *= size5; i += i5;
         return i;
-      } 
+      }
     }
     int size1,size2,size3,size4,size5,size_;
   };
@@ -94,6 +95,8 @@
   */
   class Named {
   public:
+    Named() : name() {}
+
     string getName() const { return name; }
     void setName(const string& n) { name = n; }
   private:
@@ -114,7 +117,7 @@
     Handle(const Handle& h) : root(h.root) {
       increment();
     }
-    const Handle& operator=(const Handle& h) {
+    Handle& operator=(const Handle& h) {
       if (root != h.root) {
         decrement();
         root = h.root;
@@ -140,7 +143,7 @@
           delete root;
           root = 0;
         } else {
-	  assert(root->count > 0 );
+          assert(root->count > 0 );
           --(root->count);
         }
       }
diff -uN coin-FlopCpp/FlopCpp/src/MP_variable.cpp FlopC/FlopCpp/src/MP_variable.cpp
--- coin-FlopCpp/FlopCpp/src/MP_variable.cpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_variable.cpp	2010-03-03 09:52:39.000000000 +0100
@@ -14,28 +14,31 @@
 #include "MP_model.hpp"
 using namespace flopc;
 
- 
 MP_variable::MP_variable(const MP_set_base &s1, 
-			 const MP_set_base &s2, 
-			 const MP_set_base &s3,
-			 const MP_set_base &s4, 
-			 const MP_set_base &s5) :
+      const MP_set_base &s2,
+      const MP_set_base &s3,
+      const MP_set_base &s4,
+      const MP_set_base &s5) :
   RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
+  Functor(),
+  Named(),
   upperLimit(s1,s2,s3,s4,s5),
   lowerLimit(s1,s2,s3,s4,s5),
   S1(&s1),S2(&s2),S3(&s3),S4(&s4),S5(&s5),
+  i1(),i2(),i3(),i4(),i5(),
+  M(0),
+  type(continuous),
   offset(-1)
 {
   lowerLimit.initialize(0.0);
   upperLimit.initialize(MP_model::getDefaultModel().getInfinity());
-  type = continuous;
-}    
+}
 
-double MP_variable::level(int lcl_i1, int lcl_i2, int lcl_i3, int lcl_i4, int lcl_i5) { 
+double MP_variable::level(int lcl_i1, int lcl_i2, int lcl_i3, int lcl_i4, int lcl_i5) const {
   assert(M != 0);
   assert(M->Solver != 0);
-  return M->Solver->getColSolution()[offset +  f(lcl_i1,lcl_i2,lcl_i3,lcl_i4,lcl_i5)]; 
-}  
+  return M->Solver->getColSolution()[offset +  f(lcl_i1,lcl_i2,lcl_i3,lcl_i4,lcl_i5)];
+}
 
 void MP_variable::operator()() const {
   if (S1!=&MP_set::getEmpty()) cout << i1.evaluate() << " ";
diff -uN coin-FlopCpp/FlopCpp/src/MP_variable.hpp FlopC/FlopCpp/src/MP_variable.hpp
--- coin-FlopCpp/FlopCpp/src/MP_variable.hpp	2010-03-03 09:52:09.000000000 +0100
+++ FlopC/FlopCpp/src/MP_variable.hpp	2010-03-03 09:52:39.000000000 +0100
@@ -28,7 +28,7 @@
       parametersof construction are MP_set s which specify the indexes
       over which the variable is defined.
   */
-  class MP_variable : public RowMajor, public Functor , public Named {
+  class MP_variable : public RowMajor, public Functor, private Named {
     friend class MP_model;
     friend class DisplayVariable;
     friend class VariableRef;
@@ -39,38 +39,41 @@
                 const MP_set_base &s4 = MP_set::getEmpty(), 
                 const MP_set_base &s5 = MP_set::getEmpty());
 
-    void display(const std::string &s = "");  
+    using Named::setName;
+    using Named::getName;
+
+    void display(const std::string &s = "");
 
     ~MP_variable() {
     }
 
     /// Returns the value of the variable given the specific index values.
-    double level(int i1=0, int i2=0, int i3=0, int i4=0, int i5=0);
+    double level(int i1=0, int i2=0, int i3=0, int i4=0, int i5=0) const;
 
     /// Interal use only.
-    const VariableRef& operator()(
-      const MP_index_exp& d1 = MP_index_exp::getEmpty(), 
-      const MP_index_exp& d2 = MP_index_exp::getEmpty(), 
+   const VariableRef& operator()(
+      const MP_index_exp& d1 = MP_index_exp::getEmpty(),
+      const MP_index_exp& d2 = MP_index_exp::getEmpty(),
       const MP_index_exp& d3 = MP_index_exp::getEmpty(),
       const MP_index_exp& d4 = MP_index_exp::getEmpty(), 
       const MP_index_exp& d5 = MP_index_exp::getEmpty()
       ) {
       return *new VariableRef(this, d1, d2, d3, d4, d5);
     }
-    
-    //void display(string s = "");  
+
+    //void display(string s = "");
 
     /// Call this method to turn the variable into a binary variable
-    void binary() { 
+    void binary() {
       upperLimit.initialize(1);
-      type = discrete; 
+      type = discrete;
     }
 
     /// Call this method to turn the MP_variable into an integer variable
-    void integer() { 
-      type = discrete; 
+    void integer() {
+      type = discrete;
     }
- 
+
     /// Upper bound on the variable value.
     MP_data upperLimit;
     /// Lower bound on the variable value.
@@ -79,7 +82,7 @@
     //Disabling copy constructor and assignment
     MP_variable(const MP_variable&);
     MP_variable& operator=(const MP_variable&);
- 
+
     void operator()() const;
     const MP_set_base *S1, *S2, *S3, *S4, *S5;
     MP_index i1,i2,i3,i4,i5;
Common subdirectories: coin-FlopCpp/FlopCpp/src/.svn and FlopC/FlopCpp/src/.svn


More information about the FlopCpp mailing list