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
00021 LatticeProtein*
00022 PivotMoveSet::applyMove(const LatticeProtein* const todo,
00023 size_t moveIndex) {
00024
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
00036 size_t position = (_moveIndex / (lattice->getNeighborhood().size()-1))+1;
00037 size_t moveIndex = _moveIndex % (lattice->getNeighborhood().size()-1);
00038
00039
00040 Move mutatedMove = lattice->getNeighborhood().getElementByIndex(moveIndex).getMove();
00041
00042
00043
00044
00045
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
00055 lastOverwrittenMove = todo->getMoveSeqRel().at(position);
00056 lastOverwritePos = position;
00057 lastChangedObject = todo;
00058
00059 MoveSequence relMoves = todo->getMoveSeqRel();
00060
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
00067 IPointVec* points = lpi->getPointsRef();
00068 *points = lattice->relMovesToPoints(relMoves);
00069
00070
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
00081
00082
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 }