00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Implicative.hh"
00024
00025 Implicative::Implicative(int ns, bool* quant, int* nv) {
00026 n=0;
00027 for (int i=0;i<ns;i++) {
00028 n += nv[i];
00029 }
00030 nbSpaces=ns;
00031 v=new void*[n];
00032 type_of_v = new VarType[n];
00033 Quantifiers = quant;
00034 whichSpaceOwns=new int[n];
00035 nbVarBySpace=new int[nbSpaces];
00036 rules=new MySpace*[ns];
00037 nbVarBySpace[0]=nv[0];
00038 rules[0]=new MySpace(nbVarBySpace[0]);
00039 for (int i=1;i<nbSpaces;i++) {
00040 nbVarBySpace[i]=nbVarBySpace[i-1]+nv[i];
00041 rules[i]=new MySpace(nbVarBySpace[i]);
00042 }
00043 goal=new MySpace(n);
00044 for(unsigned int i=0;i<n;i++) {
00045 int lespace=0;
00046 while (nbVarBySpace[lespace]<=i) lespace++;
00047 whichSpaceOwns[i]=lespace;
00048 }
00049
00050
00051 varInitialised=new bool[n];
00052 for (unsigned int i=0;i<n;i++) varInitialised[i]=false;
00053 currentDeclareSpace=0;
00054
00055 optim = new Opts[nbSpaces];
00056 for (int i=0;i<nbSpaces;i++)
00057 optim[i].opt_type = 0;
00058 }
00059
00060
00061
00062 Implicative::~Implicative() {
00063 for (int i=0;i<nbSpaces;i++) {
00064 delete rules[i];
00065 }
00066
00067 delete goal;
00068 delete [] rules;
00069 }
00070
00071
00072 int Implicative::spaces() {
00073 return nbSpaces;
00074 }
00075
00076
00077 void Implicative::QIntVar(int var,int min,int max) {
00078 if (varInitialised[var]) {
00079 cout<<"Variable "<<var<<" Already created !!"<<endl;
00080 abort();
00081 }
00082
00083 for (int i=whichSpaceOwns[var];i<nbSpaces;i++) {
00084 rules[i]->v[var] = new IntVar(rules[i],min,max);
00085 rules[i]->type_of_v[var] = VTYPE_INT;
00086 }
00087 goal->v[var] = new IntVar(goal,min,max);
00088 goal->type_of_v[var] = VTYPE_INT;
00089 varInitialised[var]=true;
00090 type_of_v[var]=VTYPE_INT;
00091 }
00092
00093
00094 void Implicative::QIntVar(int var,IntSet dom) {
00095 if (varInitialised[var]) {
00096 cout<<"Variable "<<var<<" Already created !!"<<endl;
00097 abort();
00098 }
00099
00100 for (int i=whichSpaceOwns[var];i<nbSpaces;i++) {
00101 rules[i]->v[var] = new IntVar(rules[i],dom);
00102 rules[i]->type_of_v[var] = VTYPE_INT;
00103 }
00104 goal->v[var] = new IntVar(goal,dom);
00105 goal->type_of_v[var] = VTYPE_INT;
00106 varInitialised[var]=true;
00107 type_of_v[var]=VTYPE_INT;
00108 }
00109
00110
00111 void Implicative::QBoolVar(int var) {
00112 if (varInitialised[var]) {
00113 cout<<"Variable "<<var<<" Already created !!"<<endl;
00114 abort();
00115 }
00116
00117 for (int i=whichSpaceOwns[var];i<nbSpaces;i++) {
00118 rules[i]->v[var] = new BoolVar(rules[i],0,1);
00119 rules[i]->type_of_v[var]=VTYPE_BOOL;
00120 }
00121 goal->v[var] = new BoolVar(goal,0,1);
00122 goal->type_of_v[var]=VTYPE_BOOL;
00123 varInitialised[var]=true;
00124 type_of_v[var]=VTYPE_BOOL;
00125 }
00126
00127 MySpace* Implicative::space() {
00128 if (currentDeclareSpace<nbSpaces)
00129 return rules[currentDeclareSpace];
00130 if (currentDeclareSpace==nbSpaces)
00131 return goal;
00132 return NULL;
00133 }
00134
00135
00136 IntVar Implicative::var(int n) {
00137 if (!varInitialised[n]) {
00138 cout<<"Variable "<<n<<" not initialized !"<<endl;
00139 abort();
00140 }
00141 if (type_of_v[n] != VTYPE_INT) {
00142 cout<<"Variable "<<n<<" is not INT"<<endl;
00143 abort();
00144 }
00145 return *(static_cast<IntVar*>(space()->v[n]));
00146 }
00147
00148
00149 BoolVar Implicative::bvar(int n) {
00150 if (!varInitialised[n]) {
00151 cout<<"Variable "<<n<<" not initialized !"<<endl;
00152 abort();
00153 }
00154 if (type_of_v[n] != VTYPE_BOOL){
00155 cout<<"Variable "<<n<<" is not BOOL"<<endl;
00156 abort();
00157 }
00158 return *(static_cast<BoolVar*>(space()->v[n]));
00159 }
00160
00161
00162 int Implicative::nextScope() {
00163 if (currentDeclareSpace == -1) return -1;
00164 currentDeclareSpace++;
00165 if (currentDeclareSpace>nbSpaces) return -1;
00166 return currentDeclareSpace;
00167 }
00168
00169 void Implicative::makeStructure() {
00170 for (unsigned int i=0;i<n;i++) {
00171 if (varInitialised[i] == false) {
00172 cout<<"Can't make structure : variable "<<i<<" not initialised"<<endl;
00173 abort();
00174 }
00175 }
00176
00177 for (int i=0;i<nbSpaces;i++) {
00178 if (!Quantifiers[i])
00179 if (optim[i].vars.empty())
00180 optimize(i,0,getExistential( (i==0)?0:nbVarBySpace[i-1]));
00181 }
00182 }
00183
00184
00185 OptVar* Implicative::getAggregate(int scope, OptVar* opt, Aggregator* agg) {
00186 if (!Quantifiers[scope]) {cout<<"Try to get aggregate on existential scope"<<endl;abort();}
00187 if (opt->getScope() < scope) {cout<<"aggregated variable out of aggregator scope"<<endl;abort();}
00188 for (int i=scope+1; i<opt->getScope();i++)
00189 if (Quantifiers[i])
00190 {cout<<"Universal scope between variable and aggregator"<<endl;abort();}
00191
00192 UnivOptVar* zeopt = new UnivOptVar(scope,opt,agg);
00193 optim[scope].vars.push_back(zeopt);
00194 return zeopt;
00195 }
00196
00197
00198 OptVar* Implicative::getExistential(int var) {
00199 if (Quantifiers[whichSpaceOwns[var]]) {cout<<"can't create existOptVar : variable "<<var<<" is universal"<<endl;abort();}
00200 ExistOptVar* opt = new ExistOptVar(var,whichSpaceOwns[var]);
00201 return opt;
00202 }
00203
00204
00205 void Implicative::optimize(int scope, int optType,OptVar* var) {
00206 if (var->getScope() < scope) {cout<<"optvar out of optimizing scope"<<endl;abort();}
00207 for (int i=scope; i<var->getScope();i++) {
00208 if (Quantifiers[i])
00209 { cout<<"universal scope between variable and optimizing scope"<<endl;abort();}
00210 }
00211
00212 optim[scope].vars.clear();
00213 optim[scope].vars.push_back(var);
00214 optim[scope].opt_type=optType;
00215 }
00216
00217
00218 bool Implicative::qt_of_var(int v) {
00219 return Quantifiers[whichSpaceOwns[v]];
00220 }
00221
00222
00223 MySpace* Implicative::getSpace(int scope) {
00224 if (scope<0) {
00225 cout<<"I return NULL coz of bad scope value"<<endl;
00226 return NULL;
00227 }
00228 if (scope>nbSpaces) {
00229 cout<<"I return NULL coz of bad scope value"<<endl;
00230 return NULL;
00231 }
00232 if (scope==nbSpaces) {
00233 if (goal->status() == SS_FAILED) {
00234 cout<<"I return NULL coz goal is failed"<<endl;
00235 return NULL;
00236 }
00237
00238 return static_cast<MySpace*>(goal->clone());
00239 }
00240 if (rules[scope]->status() == SS_FAILED) {
00241 cout<<"I return NULL coz scope "<<scope<<" is failed"<<endl;
00242 return NULL;
00243 }
00244
00245 return (static_cast<MySpace*>(rules[scope]->clone()));
00246 }
00247
00248
00249 MySpace* Implicative::getGoal() {
00250 if (goal->status() == SS_FAILED) return NULL;
00251 return static_cast<MySpace*>(goal->clone());
00252 }
00253
00254
00255 int Implicative::getOptType(int scope) {
00256 if (Quantifiers[scope]) {cout<<"Try to get OptType on universal scope"<<endl;abort();}
00257 return optim[scope].opt_type;
00258 }
00259
00260
00261 OptVar* Implicative::getOptVar(int scope) {
00262 if (Quantifiers[scope]) abort();
00263
00264
00265 return static_cast<OptVar*>(optim[scope].vars[0]);
00266 }
00267