Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

semantics.h

Go to the documentation of this file.
00001 // Semantics implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2003 Hermann Schichl
00004 //
00005 // This file is part of the COCONUT API.  This library
00006 // is free software; you can redistribute it and/or modify it under the
00007 // terms of the Library GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // Library GNU General Public License for more details.
00015 
00016 // As a special exception, you may use this file as part of a free software
00017 // library without restriction.  Specifically, if other files instantiate
00018 // templates or use macros or inline functions from this file, or you compile
00019 // this file and link it with other files to produce an executable, this
00020 // file does not by itself cause the resulting executable to be covered by
00021 // the Library GNU General Public License.  This exception does not however
00022 // invalidate any other reasons why the executable file might be covered by
00023 // the Library GNU General Public License.
00024 
00027 #ifndef _SEMANTICS_H_
00028 #define _SEMANTICS_H_
00029 
00030 #include <stdint.h>
00031 #include <string>
00032 #include <fstream>
00033 #include <iostream>
00034 #include <coconut_types.h>
00035 
00036 typedef enum { c_convex=1, c_linear=0, c_concave=-1, c_maybe=2 } convex_info;
00037 
00038 typedef enum { v_exists=0, v_forall=1, v_free=2, v_stochastic=3 }
00039                                                         type_annotation;
00040 
00066 typedef enum { a_redundant=1,
00067                a_active_lo=2, a_active_lo_red=a_active_lo|a_redundant,
00068                a_active_hi=4, a_active_hi_red=a_active_hi|a_redundant,
00069                a_active=a_active_lo|a_active_hi, 
00070                a_active_red=a_active|a_redundant }      activity_descr;
00071 
00072 class convex_e
00073 {
00074 private:
00075   convex_info e;
00076   uint16_t tested;
00077 public:
00078   convex_e() : e(c_maybe), tested(0) {}
00079   convex_e(const convex_info& __e, uint16_t __t) : e(__e), tested(__t) {}
00080   convex_e(const convex_e& __e) : e(__e.e), tested(__e.tested) {}
00081   explicit convex_e(uint16_t __e) : tested(0) { e = (convex_info)__e; }
00082 
00083   ~convex_e() {}
00084 
00085   const convex_info& i() const { return e; }
00086   uint16_t t() const { return tested; }
00087   
00088   convex_e operator-() const
00089   {
00090     convex_e __tmp;
00091     if(e == c_convex)
00092       __tmp.e = c_concave;
00093     else if(e == c_concave)
00094       __tmp.e = c_convex;
00095     else
00096       __tmp.e = e;
00097     return __tmp;
00098   }
00099 
00100   convex_e& operator=(const convex_e& __c)
00101   {
00102     e = __c.e;
00103     tested = __c.tested;
00104     return *this;
00105   }
00106 
00107   convex_e& operator=(const convex_info& __i)
00108   {
00109     e = __i;
00110     return *this;
00111   }
00112 
00113   convex_e& operator=(unsigned int __t)
00114   {
00115     tested = (uint16_t) __t;
00116     return *this;
00117   }
00118 
00119   convex_e& operator=(uint16_t __t)
00120   {
00121     tested = __t;
00122     return *this;
00123   }
00124 
00125   void read(char *c);
00126 
00127   void merge(const convex_e& __c)
00128   {
00129     if(e == __c.e)
00130     {
00131       if(tested < __c.tested)
00132         tested = __c.tested;
00133     }
00134     else // e != __c.e
00135     {
00136       if(tested < __c.tested)
00137         *this = __c;
00138       else if(tested == __c.tested) // this is a contradiction
00139         e = c_maybe;
00140     }
00141   }
00142 
00143   friend bool operator==(const convex_e& __c, const convex_e& __d);
00144   friend bool operator==(const convex_e& __c, const convex_info& __d);
00145   friend bool operator==(const convex_info& __c, const convex_e& __d);
00146   friend bool operator!=(const convex_e& __c, const convex_e& __d);
00147   friend bool operator!=(const convex_e& __c, const convex_info& __d);
00148   friend bool operator!=(const convex_info& __c, const convex_e& __d);
00149   friend std::ostream& operator<< (std::ostream& o, const convex_e& __s);
00150 };
00151 
00152 inline bool operator==(const convex_e& __c, const convex_e& __d)
00153 {
00154   return __c.e == __d.e;
00155 }
00156 
00157 inline bool operator==(const convex_e& __c, const convex_info& __d)
00158 {
00159   return __c.e == __d;
00160 }
00161 
00162 inline bool operator==(const convex_info& __c, const convex_e& __d)
00163 {
00164   return __c == __d.e;
00165 }
00166 
00167 inline bool operator!=(const convex_e& __c, const convex_e& __d)
00168 {
00169   return __c.e != __d.e;
00170 }
00171 
00172 inline bool operator!=(const convex_e& __c, const convex_info& __d)
00173 {
00174   return __c.e != __d;
00175 }
00176 
00177 inline bool operator!=(const convex_info& __c, const convex_e& __d)
00178 {
00179   return __c != __d.e;
00180 }
00181 
00182 inline std::ostream& operator<< (std::ostream& o, const convex_e& __s)
00183 {
00184   char c[5] = "VLXM";
00185   o << c[__s.e+1] << __s.tested;
00186   return o;
00187 }
00188 
00189 class semantics
00190 {
00191 public:
00192   struct {
00193     convex_e c_info;
00194     activity_descr act;                 // activity descriptor
00195     tristate separable;
00196     tristate active;
00197     bool is_at_any_bound;
00198   } property_flags;                     // mathematical constraint properties
00199 
00200   struct {
00201     bool kj;                            // is from KJ conditions
00202     bool integer;                       // is integer / not real
00203     type_annotation type;               // exists, forall, free, stochastic
00204     bool hard;                          // hard or soft constraint
00205   } annotation_flags;
00206 
00207   struct {
00208     tristate has_0chnbase;
00209   } info_flags;                         // algorithmic properties
00210 
00211   unsigned int _0chnbase;
00212 
00213   int addinfo;                          // for KJ variables this number
00214                                         // is the corresponding constraint
00215                                         // number, -1 if kappa (for obj.)
00216 
00217   int degree;                           // -1: essential non-linearity
00218   int dim;                              // num of vars involved
00219   int stage;                            // for multistage problems
00220 public:
00221   // and others needed
00222   semantics()
00223   {
00224     property_flags.c_info = convex_e(c_maybe,0);
00225     property_flags.separable = t_maybe;
00226     property_flags.act = a_active;
00227     property_flags.is_at_any_bound = false;
00228 
00229     annotation_flags.kj = false;
00230     annotation_flags.integer = false;
00231     annotation_flags.hard = true;
00232     annotation_flags.type = v_exists;
00233 
00234     info_flags.has_0chnbase = t_false;
00235 
00236     _0chnbase = 0;
00237     addinfo = 0;
00238 
00239     degree = -1;
00240     dim = 0;
00241     stage = 0;
00242   }
00243 
00244   semantics(const semantics& __s) : property_flags(__s.property_flags),
00245                                     annotation_flags(__s.annotation_flags),
00246                                     info_flags(__s.info_flags),
00247                                     _0chnbase(__s._0chnbase),
00248                                     addinfo(__s.addinfo), degree(__s.degree),
00249                                     dim(__s.dim), stage(__s.stage) {}
00250 
00251   ~semantics() {}
00252 
00253   friend std::ostream& operator<< (std::ostream& o, const semantics& __s);
00254 
00255 private:
00256   const char* print_tristate(const tristate& x) const;
00257   tristate read_tristate(char c) const;
00258   const char* print_type_annotation(const type_annotation& x) const;
00259   type_annotation read_type_annotation(char c) const;
00260   const char* print_activity_descr(const activity_descr& x) const;
00261   activity_descr read_activity_descr(char c) const;
00262 
00263   void merge_tristate(tristate& __o, const tristate& __n)
00264   {
00265     if(__o != __n)
00266     {
00267       if((__o == t_true && __n == t_false) || (__o == t_false && __n == t_true))
00268         __o = t_maybe;
00269       else if(__n == t_true)
00270         __o = t_true;
00271       else if(__n == t_false)
00272         __o = t_false;
00273     }
00274   }
00275 
00276   void merge_type_annotation(type_annotation& __o, const type_annotation& __n)
00277   {
00278     if(__o != __n)
00279     {
00280       if(__o == v_exists)
00281         __o = __n;
00282       else if(__o == v_free)
00283         __o = __n;
00284       else if(__o == v_forall)
00285         __o = __n;
00286     }
00287   }
00288 
00289   void merge_activity_descr(activity_descr& __o, const activity_descr& __n)
00290   {
00291     if(__o != __n)
00292     {
00293       unsigned int _n = (unsigned int) __n;
00294       unsigned int _o = (unsigned int) __o;
00295       unsigned int _r = _n & ((unsigned int)a_redundant); // rem. redundant bit
00296       _n |= (unsigned int)a_redundant;   // set redundant bit
00297       _o &= _n;       // unset all _n bits in _o.
00298       _o |= _r;       // one is redundant, hence the other one as well
00299       if(_o == 0) _o = (unsigned int)a_redundant;
00300       __o = (activity_descr)_o;
00301     }
00302   }
00303 
00304 public:
00305   void read(char *c);
00306 
00307   void merge(const semantics& __s)
00308   {
00309     property_flags.c_info.merge(__s.property_flags.c_info);
00310     merge_tristate(property_flags.separable, __s.property_flags.separable);
00311     merge_activity_descr(property_flags.act, __s.property_flags.act);
00312     if(__s.property_flags.is_at_any_bound)
00313       property_flags.is_at_any_bound = true;
00314     if(!__s.annotation_flags.kj)
00315       annotation_flags.kj = false;
00316     if(__s.annotation_flags.integer)
00317       annotation_flags.integer = true;
00318     if(__s.annotation_flags.hard)
00319       annotation_flags.hard = true;
00320     merge_type_annotation(annotation_flags.type, __s.annotation_flags.type);
00321     merge_tristate(info_flags.has_0chnbase, __s.info_flags.has_0chnbase);
00322     if(stage == 0 && __s.stage != 0)
00323       stage = __s.stage;
00324   }
00325 
00326 public:
00327   // access and storage methods for flags which are
00328   // not automatically set by the simplifier
00329   const convex_e& convexity() const { return property_flags.c_info; }
00330   void convexity(const convex_e& __c) { property_flags.c_info = __c; }
00331 
00332   const tristate& separability() const { return property_flags.separable; }
00333   void separability(const tristate& __c) { property_flags.separable = __c; }
00334 
00335   const activity_descr& activity() const { return property_flags.act; }
00336   void activity(const activity_descr& __c) { property_flags.act = __c; }
00337 
00338   bool kj_flag() const { return annotation_flags.kj; }
00339   void kj_flag(bool __c) { annotation_flags.kj = __c; }
00340 
00341   bool integer_flag() const { return annotation_flags.integer; }
00342   void integer_flag(bool __c) { annotation_flags.integer = __c; }
00343 
00344   bool hard_flag() const { return annotation_flags.hard; }
00345   void hard_flag(bool __c) { annotation_flags.hard = __c; }
00346 
00347   bool is_at_any_bound() const { return property_flags.is_at_any_bound; }
00348   void is_at_any_bound(bool __c) { property_flags.is_at_any_bound = __c; }
00349 
00350   const type_annotation& type() const { return annotation_flags.type; }
00351   void type(const type_annotation& __c) { annotation_flags.type = __c; }
00352 
00353   bool redundancy() const
00354     { return (bool)(((unsigned int)activity()) & (unsigned int)a_redundant); }
00355   bool inactive_hi() const
00356     { return (bool)(!((unsigned int)activity() & (unsigned int)a_active_hi)); }
00357   bool inactive_lo() const
00358     { return (bool)(!((unsigned int)activity() & (unsigned int)a_active_lo)); }
00359   bool inactive() const
00360     { return (bool)(!((unsigned int)activity() & (unsigned int)a_active)); }
00361 };
00362 
00363 std::ostream& operator<< (std::ostream& o, const semantics& __s);
00364 
00365 #endif // _SEMANTICS_H_

Generated on Tue Nov 4 01:57:58 2003 for COCONUT API by doxygen1.2.18