| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * AtomByRandomAction.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Sep 11, 2020 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include "Atom/atom.hpp" | 
|---|
| 38 | #include "CodePatterns/Log.hpp" | 
|---|
| 39 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 40 | #include "World.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | #include "Descriptors/AtomOrderDescriptor.hpp" | 
|---|
| 43 | #include "RandomNumbers/RandomNumberDistributionFactory.hpp" | 
|---|
| 44 | #include "RandomNumbers/RandomNumberDistribution.hpp" | 
|---|
| 45 | #include "RandomNumbers/RandomNumberDistribution_Parameters.hpp" | 
|---|
| 46 | #include "RandomNumbers/RandomNumberGenerator.hpp" | 
|---|
| 47 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp" | 
|---|
| 48 |  | 
|---|
| 49 | #include <algorithm> | 
|---|
| 50 | #include <iostream> | 
|---|
| 51 | #include <string> | 
|---|
| 52 |  | 
|---|
| 53 | #include <boost/bind.hpp> | 
|---|
| 54 | #include <boost/shared_ptr.hpp> | 
|---|
| 55 |  | 
|---|
| 56 | #include "AtomByRandomAction.hpp" | 
|---|
| 57 |  | 
|---|
| 58 | using namespace MoleCuilder; | 
|---|
| 59 |  | 
|---|
| 60 | // and construct the stuff | 
|---|
| 61 | #include "AtomByRandomAction.def" | 
|---|
| 62 | #include "Action_impl_pre.hpp" | 
|---|
| 63 | /** =========== define the function ====================== */ | 
|---|
| 64 | ActionState::ptr SelectionAtomByRandomAction::performCall() | 
|---|
| 65 | { | 
|---|
| 66 | // get range of ids | 
|---|
| 67 | const int number_atoms = World::getConstInstance().numAtoms(); | 
|---|
| 68 | LOG(2, "DEBUG: There are " << number_atoms << " to choose from."); | 
|---|
| 69 |  | 
|---|
| 70 | // sensibility check: atoms present to select? | 
|---|
| 71 | if (number_atoms <= 0) { | 
|---|
| 72 | STATUS("No atoms are present."); | 
|---|
| 73 | return Action::failure; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | const int number_requested = params.number_requested.get(); | 
|---|
| 77 |  | 
|---|
| 78 | std::vector<atomId_t> atom_ids; | 
|---|
| 79 | if (number_requested >= number_atoms) { | 
|---|
| 80 | STATUS("Desired number is equal or exceeds present atom count, selecting all."); | 
|---|
| 81 |  | 
|---|
| 82 | // select all atoms | 
|---|
| 83 | World::getInstance().selectAllAtoms(AllAtoms()); | 
|---|
| 84 |  | 
|---|
| 85 | // note down set of all ids for undo | 
|---|
| 86 | const std::vector<const atom *> atoms = World::getConstInstance().getAllAtoms(AllAtoms()); | 
|---|
| 87 | std::transform(atoms.begin(), atoms.end(), | 
|---|
| 88 | std::back_inserter(atom_ids), boost::bind( &atom::getId, _1) ); | 
|---|
| 89 | std::sort(atom_ids.begin(), atom_ids.end()); | 
|---|
| 90 | } else { | 
|---|
| 91 | STATUS("Randomly selecting "+toString(number_requested)+" atoms."); | 
|---|
| 92 |  | 
|---|
| 93 | // get present type and parameter set for restore | 
|---|
| 94 | const std::string oldtype = RandomNumberDistributionFactory::getConstInstance().getCurrentTypeName(); | 
|---|
| 95 | RandomNumberDistributionFactory::getInstance().setCurrentType("uniform_01"); | 
|---|
| 96 | const RandomNumberGenerator& rng = RandomNumberGeneratorFactory::getConstInstance().makeRandomNumberGenerator(); | 
|---|
| 97 |  | 
|---|
| 98 | // randomly generate ids and select | 
|---|
| 99 | std::set<atomId_t> unique_ids; | 
|---|
| 100 | while (unique_ids.size() < number_requested) { | 
|---|
| 101 | const int current_id = (int)(rng()*number_atoms)+1; | 
|---|
| 102 | const atom * const Walker = World::getConstInstance().getAtom(AtomByOrder(current_id)); | 
|---|
| 103 | if (Walker != NULL) { | 
|---|
| 104 | LOG(1, "Selecting " << current_id << "th atom " << Walker->getName() | 
|---|
| 105 | << " from [1," << number_atoms << "]"); | 
|---|
| 106 | World::getInstance().selectAtom(Walker); | 
|---|
| 107 | unique_ids.insert( Walker->getId() ); | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 | atom_ids.insert(atom_ids.begin(), unique_ids.begin(), unique_ids.end()); | 
|---|
| 111 |  | 
|---|
| 112 | // restore old type and parameter set | 
|---|
| 113 | RandomNumberDistributionFactory::getInstance().setCurrentType(oldtype); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | return ActionState::ptr(new SelectionAtomByRandomState(atom_ids, params)); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | ActionState::ptr SelectionAtomByRandomAction::performUndo(ActionState::ptr _state) { | 
|---|
| 120 | SelectionAtomByRandomState *state = assert_cast<SelectionAtomByRandomState*>(_state.get()); | 
|---|
| 121 |  | 
|---|
| 122 | for (atomids_t::const_iterator iter = state->undoatomids.begin(); | 
|---|
| 123 | iter != state->undoatomids.end(); ++iter) | 
|---|
| 124 | World::getInstance().unselectAllAtoms(AtomById(*iter)); | 
|---|
| 125 |  | 
|---|
| 126 | return ActionState::ptr(_state); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | ActionState::ptr SelectionAtomByRandomAction::performRedo(ActionState::ptr _state){ | 
|---|
| 130 | SelectionAtomByRandomState *state = assert_cast<SelectionAtomByRandomState*>(_state.get()); | 
|---|
| 131 |  | 
|---|
| 132 | for (atomids_t::const_iterator iter = state->undoatomids.begin(); | 
|---|
| 133 | iter != state->undoatomids.end(); ++iter) | 
|---|
| 134 | World::getInstance().selectAllAtoms(AtomById(*iter)); | 
|---|
| 135 |  | 
|---|
| 136 | return ActionState::ptr(_state); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | bool SelectionAtomByRandomAction::canUndo() { | 
|---|
| 140 | return true; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | bool SelectionAtomByRandomAction::shouldUndo() { | 
|---|
| 144 | return true; | 
|---|
| 145 | } | 
|---|
| 146 | /** =========== end of function ====================== */ | 
|---|