Main Page   Class Hierarchy   Compound List   File List   Contact   Download   Symbolic Constraints   Examples  

cons_obj.cc

00001 #include<scil/cons_obj.h>
00002 #include<scil/var_obj.h>
00003 #include<scil/row.h>
00004 #include<scil/subproblem.h>
00005 #include<scil/aba_constraint.h>
00006 #include<abacus/row.h>
00007 
00008 using namespace SCIL;
00009 
00010 cons_obj::cons_obj (cons_sense s, double rhs__)
00011 {
00012   sense_ = s;
00013   rhs_ = rhs__;
00014   Ref_Acons=0;
00015   qrStatus = CHECK;
00016 };
00017 
00018 cons_obj::cons_obj ()
00019 {
00020   sense_ = Equal;
00021   rhs_ = 0;
00022   Ref_Acons=0;
00023   qrStatus = CHECK;
00024 }
00025 
00026 int
00027 cons_obj::genRow (Active_Variables * vars, Row & ro)
00028 {
00029   row R;
00030   non_zero_entries(R);
00031 
00032   tr1::unordered_map<var_obj*,double>::const_iterator v;
00033   foreach( v, CM ){
00034     R+=var(v->first) * v->second;
00035   }
00036 
00037   //R.normalize();
00038 
00039   //if(R.undefined()) {
00040   //  cout<<"Have to implement nonzero\n";
00041     //return (*Ref_Acons)::ABA_CONSTRAINT->genRow(vars, ro); 
00042   //}
00043   int j;
00044   double rhs1=0;
00045   ro.realloc(R.size());
00046   row_entry::list_iterator it;
00047  
00048   foreach( it, R ){
00049     if( it->Var==nil ) {
00050       j=-1; rhs1+=it->coeff;
00051     }
00052     else {
00053       j=it->Var.var_pointer()->index();
00054     }
00055     if( j!=-1 ) {
00056       ro.insert(j,it->coeff);
00057     }
00058   }
00059 
00060   ro.rhs(rhs()+rhs1);
00061   if(sense_==Equal) ro.sense(ABA_CSENSE::Equal);
00062   if(sense_==Greater) ro.sense(ABA_CSENSE::Greater);
00063   if(sense_==Less) ro.sense(ABA_CSENSE::Less);
00064 
00065   return(R.size());
00066 
00067 };
00068 
00069 double
00070 cons_obj::slack (subproblem & S)
00071 {
00072 
00073   row
00074     r;
00075   row_entry::list_constiterator
00076     it;
00077   double
00078     x = 0;
00079   non_zero_entries (r);
00080   foreach(it, r)
00081   {
00082     x += it->coeff * S.value (it->Var);
00083   };
00084 
00085   return x - rhs ();
00086 }
00087 
00088 bool
00089 cons_obj::violated (subproblem & S)
00090 {
00091   row
00092     r;
00093   row_entry::list_constiterator
00094     it;
00095   double
00096     x = 0;
00097   non_zero_entries (r);
00098   foreach (it, r)
00099   {
00100     x += it->coeff * S.value (it->Var);
00101   };
00102 
00103   if (sense () == Equal)
00104     return fabs (x - rhs ()) > 0.001;
00105   if (sense () == Less)
00106     return x - rhs () > 0.001;
00107   return x - rhs () < -0.001;
00108 }
00109 
00110 
00111 double
00112 cons_obj::violation (subproblem & S)
00113 {
00114   row
00115     r;
00116   row_entry::list_constiterator
00117     it;
00118   double
00119     x = 0;
00120   non_zero_entries (r);
00121   foreach (it, r)
00122   {
00123     x += it->coeff * S.value (it->Var);
00124   };
00125   if (sense () == Greater)
00126     return rhs () - x;
00127   else if( sense() == Less )
00128      return x - rhs();
00129   else
00130     return fabs (x - rhs ());
00131 }
00132 
00133 
00134 //FIXME
00135 double
00136 cons_obj::violation (solution & S)
00137 {
00138   row
00139     r;
00140   row_entry::list_constiterator
00141     it;
00142   double
00143     x = 0;
00144   non_zero_entries (r);
00145   foreach (it, r)
00146   {
00147     x += it->coeff * S.value (it->Var);
00148   };
00149   if (sense () == Greater)
00150     return rhs () - x;
00151   else if( sense() == Less )
00152      return x - rhs();
00153   else
00154     return fabs (x - rhs ());
00155 }
00156 
00157 void
00158 cons_obj::init (subproblem & S_, int n, Activation a, Validity v)
00159 {
00160   Act=a;
00161   Ref_Acons = new ABA_Constraint (S_, this, a, v, Liftable, n);
00162 }
00163 
00164 void
00165 cons_obj::init (ILP_Problem & IP, int n, Activation a, Validity v)
00166 {
00167   Act=a;
00168   Ref_Acons = new ABA_Constraint (IP, this, a, v, Liftable, n);
00169 };
00170 
00171 void
00172 cons_obj::set_sense (cons_sense s)
00173 {
00174   /*{\Mop sets the sense of the basic constraint.} */
00175   sense_ = s;
00176 };
00177 
00178 
00179 void
00180 cons_obj::set_rhs (double r)
00181 {
00182   /*{\Mop sets the right-hand side of the basic constraint.} */
00183   rhs_ = r;
00184 };
00185 
00186 Activation cons_obj::get_Act() {
00187    return Act;
00188 }
00189 
00190 
00191 
00192 ABA_Constraint *
00193 cons_obj::Acons ()
00194 {
00195   return Ref_Acons;
00196 }
00197 
00198 
00199 double
00200 cons_obj::rhs ()
00201 {
00202   /*{\Mop returns the right-hand-side.} */
00203   return rhs_;
00204 };
00205 
00206 
00207 cons_sense
00208 cons_obj::sense ()
00209 {
00210   /*{\Mop retunrs the sense.} */
00211   return sense_;
00212 }
00213 
00214 
00215 double
00216 cons_obj::coeff (ABA_VARIABLE * v)
00217 {
00218   if( CM.find((var_obj*)v) != CM.end() ){
00219     cout << "set\n";
00220     return CM[ (var_obj*)v ];
00221   }
00222   double
00223     d = coeff ((var_obj *) v);
00224   if (d != 0)
00225     return d;
00226   return v->coeff (Acons ());
00227 };
00228 
00229 
00230 
00231 void
00232 cons_obj::set (var_obj * v, double d)
00233 {
00234   CM[v] = d;
00235 }
00236 
00237 cons_obj::~cons_obj() {
00238   if(Ref_Acons!=0) {
00239     Ref_Acons->Ref_cons=0;
00240     delete Ref_Acons;
00241   };
00242 };
Generated on Mon Mar 28 22:03:47 2011 for SCIL by  doxygen 1.6.3