libatomprobe
Library for Atom Probe Tomography (APT) computation
massTool.h
Go to the documentation of this file.
1 /*
2  * massTool.h : Copyright (C) 2017 D Haley
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 #ifndef ATOMPROBE_MASSTOOL_H
20 #define ATOMPROBE_MASSTOOL_H
21 
22 #include <cstdlib>
23 #include <vector>
24 #include <map>
25 #include <utility>
26 namespace AtomProbe {
27 
29 class Weight
30 {
31  public:
32  Weight(){};
33  Weight(float m, size_t uniqId=0) { mass=m; uniqueId = uniqId;}
34 
35  float mass; //The effective mass (might be normalised by charge state) for the element we wish to search for
36  size_t uniqueId; //Optional unique identifier, to allow disambiguation of objects with same masses (but maybe later different solution types)
37 
38  bool operator==(const Weight &b) const { return (mass == b.mass && uniqueId == b.uniqueId);}
39  bool operator<(const Weight &b) const { return (mass < b.mass);}
40 };
41 
43 class MassTool
44 {
45  private:
47  // part of the solution. Speed-up only
48  static void preprocessKnapsack(std::vector<Weight> &weights,
49  float totalWeight, float tolerance, unsigned int maxCombine);
50  public:
51 
52 
54 
65  static void bruteKnapsack(std::vector<Weight> &weights,
66  float targetWeight, float tolerance,
67  unsigned int maxObjects, std::vector<std::vector<Weight> > &solutions);
68 
69 
71  /* ! This version has parameters the same as the single-target bruteKnapsack algorithm
72  * with the exception of targetWeight, which is now a vector of targets
73  *
74  * The solutions will be returned all together, and the caller must separate them.
75  *
76  * The efficiency (speed) gains of this function are
77  * not fully clear, as internal pre-processing will be
78  * less aggressive. However, only a single tree search
79  * will be performed.
80  */
81  static void bruteKnapsack(std::vector<Weight> &weights,
82  const std::vector<float> &targetWeight, float tolerance,
83  unsigned int maxObjects, std::vector<std::vector<Weight> > &solutions);
84 
85 #ifdef DEBUG
86  static bool runUnitTests();
87 #endif
88 };
89 
90 }
91 #endif
Class that brute-force solves the knapsack problem.
Definition: massTool.h:43
bool operator==(const Weight &b) const
Definition: massTool.h:38
bool operator<(const Weight &b) const
Definition: massTool.h:39
size_t uniqueId
Definition: massTool.h:36
Weight(float m, size_t uniqId=0)
Definition: massTool.h:33
Placeholder class for containing input weights for MassTool.
Definition: massTool.h:29