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 };
00016 
00017 cons_obj::cons_obj ()
00018 {
00019   sense_ = Equal;
00020   rhs_ = 0;
00021   Ref_Acons=0;
00022 }
00023 
00024 int
00025 cons_obj::genRow (Active_Variables * vars, Row & ro)
00026 {
00027   row R;
00028   non_zero_entries(R);
00029 
00030   var_obj* v;
00031   forall_defined(v, CM) {
00032     R+=var(v)*CM[v];
00033   }
00034 
00035   //R.normalize();
00036 
00037   //if(R.undefined()) {
00038   //  cout<<"Have to implement nonzero\n";
00039     //return (*Ref_Acons)::ABA_CONSTRAINT->genRow(vars, ro); 
00040   //}
00041   row_entry t;
00042   int j;
00043   double rhs1=0;
00044   ro.realloc(R.size());
00045   forall(t, R) {
00046     if(t.Var==nil) { j=-1; rhs1+=t.coeff; } else
00047     j=t.Var.var_pointer()->index();
00048     if (j!=-1) ro.insert(j, t.coeff);
00049   }
00050   ro.rhs(rhs()+rhs1);
00051   if(sense_==Equal) ro.sense(ABA_CSENSE::Equal);
00052   if(sense_==Greater) ro.sense(ABA_CSENSE::Greater);
00053   if(sense_==Less) ro.sense(ABA_CSENSE::Less);
00054 
00055   return(R.size());
00056 
00057 };
00058 
00059 double
00060 cons_obj::slack (subproblem & S)
00061 {
00062 
00063   row
00064     r;
00065   row_entry
00066     t;
00067   double
00068     x = 0;
00069   non_zero_entries (r);
00070   forall (t, r)
00071   {
00072     x += t.coeff * S.value (t.Var);
00073   };
00074 
00075   return x - rhs ();
00076 }
00077 
00078 bool
00079 cons_obj::violated (subproblem & S)
00080 {
00081   row
00082     r;
00083   row_entry
00084     t;
00085   double
00086     x = 0;
00087   non_zero_entries (r);
00088   forall (t, r)
00089   {
00090     x += t.coeff * S.value (t.Var);
00091   };
00092 
00093   if (sense () == Equal)
00094     return fabs (x - rhs ()) > 0.001;
00095   if (sense () == Less)
00096     return x - rhs () > 0.001;
00097   return x - rhs () < -0.001;
00098 }
00099 
00100 
00101 void
00102 cons_obj::init (subproblem & S_, int n, Activation a)
00103 {
00104   Act=a;
00105   Ref_Acons = new ABA_Constraint (S_, this, a, Global, Liftable, n);
00106 }
00107 
00108 void
00109 cons_obj::init (ILP_Problem & IP, int n, Activation a)
00110 {
00111   Act=a;
00112   Ref_Acons = new ABA_Constraint (IP, this, a, Global, Liftable, n);
00113 };
00114 
00115 void
00116 cons_obj::set_sense (cons_sense s)
00117 {
00118   /*{\Mop sets the sense of the basic constraint.} */
00119   sense_ = s;
00120 };
00121 
00122 
00123 void
00124 cons_obj::set_rhs (double r)
00125 {
00126   /*{\Mop sets the right-hand side of the basic constraint.} */
00127   rhs_ = r;
00128 };
00129 
00130 ABA_Constraint *
00131 cons_obj::Acons ()
00132 {
00133   return Ref_Acons;
00134 }
00135 
00136 
00137 double
00138 cons_obj::rhs ()
00139 {
00140   /*{\Mop returns the right-hand-side.} */
00141   return rhs_;
00142 };
00143 
00144 
00145 cons_sense
00146 cons_obj::sense ()
00147 {
00148   /*{\Mop retunrs the sense.} */
00149   return sense_;
00150 }
00151 
00152 
00153 double
00154 cons_obj::coeff (ABA_VARIABLE * v)
00155 {
00156   if (CM.defined ((var_obj *) v))
00157     {
00158       cout << "set\n";
00159       return CM[(var_obj *) v];
00160     }
00161   double
00162     d = coeff ((var_obj *) v);
00163   if (d != 0)
00164     return d;
00165   return v->coeff (Acons ());
00166 };
00167 
00168 
00169 void
00170 cons_obj::set (var_obj * v, double d)
00171 {
00172   CM[v] = d;
00173 }
00174 
00175 cons_obj::~cons_obj() {
00176   if(Ref_Acons!=0) {
00177     Ref_Acons->Ref_cons=0;
00178     delete Ref_Acons;
00179   };
00180 };

Generated on Tue Nov 16 15:18:14 2004 for SCIL by doxygen1.2.16