mtc_redundantcolors.cpp

Go to the documentation of this file.
00001 /** @file mtc_redundantcolors.cpp
00002 
00003 Methods for removing redundant colors for the supervisor synthesis from MtcSystems
00004 
00005 */
00006 
00007 /* FAU Discrete Event Systems Library (libfaudes)
00008 
00009    Copyright (C) 2008  Matthias Singer
00010    Copyright (C) 2006  Bernd Opitz
00011    Exclusive copyright is granted to Klaus Schmidt
00012 
00013    This library is free software; you can redistribute it and/or
00014    modify it under the terms of the GNU Lesser General Public
00015    License as published by the Free Software Foundation; either
00016    version 2.1 of the License, or (at your option) any later version.
00017 
00018    This library is distributed in the hope that it will be useful,
00019    but WITHOUT ANY WARRANTY; without even the implied warranty of
00020    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021    Lesser General Public License for more details.
00022 
00023    You should have received a copy of the GNU Lesser General Public
00024    License along with this library; if not, write to the Free Software
00025    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00026 
00027 
00028 #include "mtc_redundantcolors.h"
00029 
00030 namespace faudes {
00031 
00032 // SearchScc(state,rCount,rGen, rNewStates,rSTACK,rStackStates,rDFN,rLOWLINK,rSccSet,rRoots)
00033 void SearchScc(
00034   const Idx state, 
00035   int& rCount,      // why is this a ref?
00036   const Generator& rGen,
00037   StateSet&  rNewStates,
00038   std::stack<Idx>& rSTACK, 
00039   StateSet& rStackStates,
00040   std::map<const Idx, int>& rDFN,
00041   std::map<const Idx, int>& rLOWLINK,
00042   std::set<StateSet>& rSccSet,
00043   StateSet& rRoots)
00044 {
00045   FD_DF("SearchScc: -- search from state "<< state << "--");
00046 
00047   // mark state "old";
00048   rNewStates.Erase(state);
00049   // DFNUMBER[state] <- count;
00050   rDFN[state]=rCount;
00051   rCount++;
00052   // LOWLINK[v] <- DFNUMBER[v];
00053   rLOWLINK[state]=rDFN[state];
00054   // push state on STACK;
00055   rSTACK.push(state);
00056   rStackStates.Insert(state);
00057   //<<create set "L[state]" of successor states of state
00058   StateSet SuccStates = StateSet();
00059   TransSet::Iterator it = rGen.TransRelBegin(state);
00060   TransSet::Iterator it_end = rGen.TransRelEnd(state);
00061   for (; it != it_end; ++it) {
00062     SuccStates.Insert(it->X2);
00063   }
00064   // for each vertex *sit on L[state] do
00065   StateSet::Iterator sit = SuccStates.Begin();
00066   StateSet::Iterator sit_end = SuccStates.End();
00067   for (; sit != sit_end; ++sit)
00068     {
00069       // if *sit is marked "new" then
00070       if(rNewStates.Exists(*sit))
00071         {// begin
00072       // SearchC(*sit);
00073       SearchScc(*sit, rCount, rGen, rNewStates, rSTACK, rStackStates, rDFN, rLOWLINK, rSccSet, rRoots);
00074       // LOWLINK[state] <- MIN(LOWLINK[state],LOWLINK[*sit]);
00075       if(rLOWLINK[*sit]<rLOWLINK[state])
00076         {
00077           rLOWLINK[state]=rLOWLINK[*sit];
00078         }
00079         }//end
00080       else
00081         {
00082       // if DFNUMBER[*sit]<DFNUMBER[state] and [*sit] is on STACK then
00083       if((rDFN[*sit]<rDFN[state])&&(rStackStates.Exists(*sit)))
00084         {
00085           // LOWLINK[state]<-MIN(DFNUMBER[*sit],LOWLINK[state]);
00086           if(rDFN[*sit]<rLOWLINK[state])
00087         {
00088           rLOWLINK[state]=rDFN[*sit];
00089         }
00090         }                
00091         }
00092     }//end for
00093   // if LOWLINK[state]=DFNUMBER[state] (i.e. state is root of a SCC) then
00094   if((rLOWLINK[state]==rDFN[state]))
00095     {
00096         
00097       //create SCC
00098       StateSet Scc;
00099       Idx top;
00100       // begin
00101       // repeat
00102       while(true)
00103         {// begin
00104             // pop x from top of STACK and print x;
00105             top=rSTACK.top();
00106             Scc.Insert(top);
00107             rStackStates.Erase(top);
00108             rSTACK.pop();
00109             // until x=state;
00110             if(top==state){
00111                 // print "end of SCC", insert SCC into SCCset;
00112                 rSccSet.insert(Scc);
00113                 rRoots.Insert(state);
00114                 break;
00115             }
00116         } //end while
00117     } // end if
00118 }
00119 
00120 //  ComputeSCC(rGen,rSCCSet,rRoots)
00121 bool ComputeSCC(const Generator& rGen, std::set<StateSet>& rSCCSet, StateSet& rRoots){
00122     // helpers:
00123     StateSet newStates = rGen.States();
00124     int count = 1;
00125     std::stack<Idx> stack;
00126     StateSet stackStates;
00127     std::map<const Idx,int> DFN;
00128     std::map<const Idx, int> LOWLINK;
00129     // Search for SCCs until the list of new states is empty
00130     while(!newStates.Empty() ){
00131         SearchScc(*newStates.Begin(), count, rGen, newStates, stack, stackStates, DFN, LOWLINK, rSCCSet, rRoots);
00132     }
00133     if(rSCCSet.empty() )
00134         return false;
00135     else
00136         return true;
00137 }
00138 
00139 // ColoredSCC(rGen,rColors,rColoredSCCs)
00140 void ColoredSCC(MtcSystem& rGen, ColorSet& rColors, std::set<StateSet>& rColoredSCCs){
00141     //helpers:
00142     StateSet roots;
00143     std::set<StateSet>::iterator stIt, stEndIt, tmpIt;
00144     ColorSet currentColors;
00145     StateSet::Iterator sIt, sEndIt;
00146     // first find all SCCs
00147     ComputeSCC(rGen, rColoredSCCs, roots);
00148     // Erase the SCCS that do not have all colors in rColors
00149     stIt = rColoredSCCs.begin();
00150     stEndIt = rColoredSCCs.end();
00151     // investigate each SCC
00152     for( ; stIt != stEndIt; ){
00153         currentColors.Clear();
00154         sIt = stIt->Begin();
00155         sEndIt = stIt->End();
00156         // collect the  colors from all states in the current SCC
00157         for( ; sIt != sEndIt; sIt++){
00158             currentColors = currentColors + rGen.Colors(*sIt);
00159         }
00160         // Remove the SCC if not all colors appear
00161         if(currentColors != rColors){
00162             tmpIt = stIt;
00163             stIt++;
00164             rColoredSCCs.erase(tmpIt);
00165             continue;
00166         }
00167         stIt++;
00168     }
00169 }
00170 
00171 // CheckRedundantColor(rGen,redundantColor)
00172 bool CheckRedundantColor(MtcSystem rGen, Idx redundantColor){
00173     //helpers:
00174     ColorSet remainingColors = rGen.Colors();
00175     remainingColors.Erase(redundantColor);
00176     std::set<StateSet> coloredSCCs;
00177     std::set<StateSet>::const_iterator csIt, csEndIt;
00178     // Remove all states with redundantColor from a copy of rGen
00179     MtcSystem copyGen = rGen;
00180     StateSet copyStates = copyGen.States();
00181     TransSetX2EvX1 copyTransSet;
00182     copyGen.TransRel(copyTransSet);
00183     StateSet::Iterator stIt, stEndIt;
00184     stIt = copyStates.Begin();
00185     stEndIt = copyStates.End();
00186     for( ; stIt!= stEndIt; stIt++){
00187       // delete all states with the redundant color
00188       StateSet criticalStates;
00189       if( copyGen.ExistsColor(*stIt,redundantColor) ){
00190           StateSet::Iterator crIt;
00191           copyGen.DelState(*stIt);
00192           criticalStates.Clear();
00193           TraverseUncontrollableBackwards(copyGen.ControllableEvents(),copyTransSet,criticalStates, *stIt); 
00194           for(crIt = criticalStates.Begin(); crIt != criticalStates.End(); crIt++){
00195               copyGen.DelState(*crIt);
00196           }
00197       }
00198     }
00199     // Fixed point iteration that alternately computes SCCs and removes states with outgoing uncontrollable 
00200     // transitions from SCCs
00201     while(true){
00202         coloredSCCs.clear();
00203         // Determine the colored strongly connected components in the remaining generator copyGen
00204         ColoredSCC(copyGen, remainingColors, coloredSCCs);
00205         // if there are no colored SCCs, the color is redundant
00206         if(coloredSCCs.empty() ){
00207             return true;
00208         }
00209         // otherwise, the SCCs can be used to construct a strongly nonblocking and controllable subbehavior of rGen that is 
00210         // blocking w.r.t. the potentially redundant color (just remove all transitions that leave the SCCs in G)
00211         else{
00212             // go over all SCCs and remove states with outgoing uncontrollable transisions
00213             bool done = true;
00214             csIt = coloredSCCs.begin();
00215             csEndIt = coloredSCCs.end();
00216             // go over all states in the SCC
00217             for( ; csIt != csEndIt; csIt++){
00218                 stIt = csIt->Begin();
00219                 stEndIt = csIt->End();
00220                 for( ; stIt != stEndIt; stIt++){
00221                     // check all transitions
00222                     TransSet::Iterator tIt, tEndIt;
00223                     tIt = copyGen.TransRelBegin(*stIt);
00224                     tEndIt = copyGen.TransRelEnd(*stIt);
00225                     for( ; tIt != tEndIt; tIt++){
00226                         if( (!copyGen.Controllable(tIt->Ev) && !csIt->Exists(tIt->X2) ) ){
00227                             done = false;
00228                             copyGen.DelState(*stIt);
00229                             break;                            
00230                         }
00231                     }
00232                 }
00233             }
00234             if(done == true)
00235                 return false;
00236         }
00237     }
00238 }
00239 
00240 
00241 // OptimalColorSet(rGen,rOptimalColors,rHighAlph)
00242 void OptimalColorSet(const MtcSystem& rGen, ColorSet& rOptimalColors, EventSet& rHighAlph){
00243     std::vector<Idx> colorVector;
00244     ColorSet genColors = rGen.Colors();
00245     ColorSet::Iterator cIt, cEndIt;
00246     cIt = genColors.Begin();
00247     cEndIt = genColors.End();
00248     for( ; cIt != cEndIt; cIt++){
00249         colorVector.push_back(*cIt);
00250     }
00251     Idx vectorSize = colorVector.size();
00252     Idx optimalNumberStates, currentNumberStates, optimalNumberColors;
00253     // the abstracted generator has at most as many states as the original generator with the overall alphabet 
00254     // as high-level alphabet and all colors
00255     optimalNumberStates = rGen.Size(); 
00256     EventSet optimalAlph, currentHighAlph;
00257     optimalAlph = rGen.Alphabet();
00258     rOptimalColors = rGen.Colors();
00259     // check all colors for redundancy
00260     for(Idx i = 1; i <= vectorSize; i++){
00261         bool redundant = CheckRedundantColor(rGen, colorVector[i-1] );
00262         // if the current color can be removed, check the next colors (note that this is only possible if 
00263         // there are additional colors 
00264         if(redundant == true){
00265             MtcSystem reducedGen = rGen;
00266             reducedGen.DelColor(colorVector[i-1] );
00267             currentHighAlph = rHighAlph;
00268             currentNumberStates = calcNaturalObserver(reducedGen, currentHighAlph);
00269             // Set the optimal values if a smaller generator than before is achieved
00270             if(currentNumberStates < optimalNumberStates){
00271                 optimalNumberStates = currentNumberStates;
00272                 optimalAlph = currentHighAlph;
00273                 rOptimalColors = reducedGen.Colors();
00274                 optimalNumberColors = rOptimalColors.Size();
00275             }
00276             // if the abstraction has the same size but less colors are needed
00277             else if(currentNumberStates == optimalNumberStates && reducedGen.Colors().Size() < rOptimalColors.Size() ){
00278                 optimalAlph = currentHighAlph;
00279                 rOptimalColors = reducedGen.Colors();
00280                 optimalNumberColors = rOptimalColors.Size();                
00281             }
00282             rec_OptimalColorSet(reducedGen, colorVector, i + 1, rOptimalColors, optimalNumberStates, optimalNumberColors, rHighAlph, optimalAlph); 
00283         }
00284     }
00285     rHighAlph = optimalAlph;
00286 }
00287 
00288 // rec_OptimalColorSet(rGen,rColorVector,colorNumber,rOptimalColors,rOptimalNumberStates,rOptimalNumberColors,rHighAlph,rOptimalHighAlph)
00289   void rec_OptimalColorSet(const MtcSystem& rGen, const std::vector<Idx>& rColorVector, Idx colorNumber, ColorSet& rOptimalColors, 
00290         Idx& rOptimalNumberStates, Idx& rOptimalNumberColors, const EventSet& rHighAlph, EventSet& rOptimalHighAlph){
00291     Idx vectorSize = rColorVector.size();
00292     EventSet currentHighAlph;
00293     Idx currentNumberStates;
00294     for(Idx i = colorNumber; i <= vectorSize; i++){
00295         bool redundant = CheckRedundantColor(rGen, rColorVector[i-1] );
00296         // if there are additional colors and the current color can be removed, check the next colors
00297         if(redundant == true){
00298             MtcSystem reducedGen = rGen;
00299             reducedGen.DelColor(rColorVector[i-1] );
00300             currentHighAlph = rHighAlph;
00301             currentNumberStates = calcNaturalObserver(reducedGen, currentHighAlph);
00302             // Set the optimal values if a smaller generator than before is achieved
00303             if(currentNumberStates < rOptimalNumberStates){
00304                 rOptimalNumberStates = currentNumberStates;
00305                 rOptimalHighAlph = currentHighAlph;
00306                 rOptimalColors = reducedGen.Colors();
00307                 rOptimalNumberColors = rOptimalColors.Size();
00308             }
00309             // if the abstraction has the same size but less colors are needed
00310             else if(currentNumberStates == rOptimalNumberStates && reducedGen.Colors().Size() < rOptimalColors.Size() ){
00311                 rOptimalHighAlph = currentHighAlph;
00312                 rOptimalColors = reducedGen.Colors();
00313                 rOptimalNumberColors = rOptimalColors.Size();                
00314             }
00315             // if there are potential colors to be removed, call the recursive function
00316             if(i < vectorSize){
00317                 rec_OptimalColorSet(reducedGen, rColorVector, colorNumber + 1, rOptimalColors, rOptimalNumberStates, rOptimalNumberColors, rHighAlph, rOptimalHighAlph); 
00318             }
00319         }       
00320     }
00321 }
00322 
00323 } // namespace faudes

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen