Generated on Tue Dec 16 12:49:16 2008 for BIU-2.2.0 by doxygen 1.5.1

src/biu/PivotMoveSet.cc

Go to the documentation of this file.
00001 #include <biu/PivotMoveSet.hh>
00002 
00003 namespace biu
00004 {
00005     PivotMoveSet::PivotMoveSet(const LatticeModel* lattice)
00006      :  LatticeMoveSet(lattice) , 
00007         lastOverwrittenMove(lattice->getNeighborhood().size()-1),
00008         lastOverwritePos(0),
00009         lastChangedObject(NULL)
00010     {} 
00011 
00012     PivotMoveSet::~PivotMoveSet()
00013     {}
00014     
00015     PivotMoveSet* 
00016     PivotMoveSet::clone() {
00017         return new PivotMoveSet(*this);
00018     }
00019 
00020 // abstract functions (LatticeMoveSet)
00021     LatticeProtein*
00022     PivotMoveSet::applyMove(const LatticeProtein* const todo, 
00023                             size_t moveIndex)  {
00024             // apply the pivot move inplace to a copy of todo
00025         return applyMoveInPlace(new LatticeProtein_Ipnt(*todo), moveIndex);
00026     }
00027 
00028     LatticeProtein* 
00029     PivotMoveSet::applyMoveInPlace(LatticeProtein* todo, 
00030      size_t _moveIndex)  {
00031 
00032         assertbiu(_moveIndex >=0 && _moveIndex < getMoveNumber(todo),
00033     "The pivot move moveIndex has to be in [0, number of available moves).");
00034 
00035             //compute position & moveIndex from _moveIndex
00036         size_t position = (_moveIndex / (lattice->getNeighborhood().size()-1))+1;
00037         size_t moveIndex = _moveIndex % (lattice->getNeighborhood().size()-1);
00038         
00039             // get the mutated relative move
00040         Move mutatedMove = lattice->getNeighborhood().getElementByIndex(moveIndex).getMove();
00041 
00042             // if the current relative move at 'position' is equal to the chosen
00043             // mutated move, choose the move with the highest move index
00044             // this one is not chosen randomly
00045             // by that it is ensured that always a random neighbor is returned
00046         if (todo->getMoveSeqRel()[position] == mutatedMove) {
00047             mutatedMove = lattice->getNeighborhood().getElementByIndex(
00048                     lattice->getNeighborhood().size() - 1).getMove();
00049         }
00050 
00051         lastChangedObject = NULL;
00052 
00053         if ( todo->getMoveSeqRel().at(position) != mutatedMove) {
00054                 // save for next undo
00055             lastOverwrittenMove = todo->getMoveSeqRel().at(position);
00056             lastOverwritePos    = position;
00057             lastChangedObject   = todo;
00058             
00059             MoveSequence relMoves = todo->getMoveSeqRel();
00060                 // change the relative move string at position to the mutated move
00061             relMoves.at(position) = mutatedMove;
00062             
00063             LatticeProtein_Ipnt* lpi = dynamic_cast<LatticeProtein_Ipnt*>(todo);
00064             assertbiu(lpi!=NULL, "Downcasting to LatticeProtein_Ipnt failed.");
00065             
00066                 // update points accordingly
00067             IPointVec* points = lpi->getPointsRef();
00068             *points = lattice->relMovesToPoints(relMoves);
00069             
00070                 // check selfavoidingness
00071             if (!todo->isSelfAvoiding())
00072                 undoLastMove(todo);
00073         }
00074 
00075         return todo;
00076     }
00077 
00078     size_t
00079     PivotMoveSet::getMoveNumber( const LatticeProtein* const lp ) const {
00080             // number of pivot moves is the number of relative moves minus one
00081             // for the current relative move multiplied by the number of movable
00082             // elements
00083         return (lattice->getNeighborhood().size()-1)*(lp->getLength()-2);
00084     }
00085 
00086 
00087     LatticeProtein* 
00088     PivotMoveSet::undoLastMove(LatticeProtein* toUndo) {
00089         if (toUndo == lastChangedObject 
00090             && toUndo->getMoveSeqRel()[lastOverwritePos] != lastOverwrittenMove) 
00091         {
00092             MoveSequence relMoves = toUndo->getMoveSeqRel();
00093             relMoves[lastOverwritePos] = lastOverwrittenMove;
00094             LatticeProtein_Ipnt* lpi = dynamic_cast<LatticeProtein_Ipnt*>(toUndo);
00095             assertbiu(lpi!=NULL, "Downcasting to LatticeProtein_Ipnt failed.");
00096             IPointVec* points = lpi->getPointsRef();
00097             *points = lattice->relMovesToPoints(relMoves);
00098         }
00099         
00100         return toUndo;
00101     }
00102 }