libatomprobe
Library for Atom Probe Tomography (APT) computation
massFit.cpp
Go to the documentation of this file.
1 /* massFit.cpp : Example showing how to perform mass knapsack search
2  * Copyright (C) 2017 Daniel 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 #include <atomprobe/atomprobe.h>
18 
19 
20 #include <iostream>
21 #include <cstdlib>
22 #include <vector>
23 #include <fstream>
24 
25 
26 using std::cout;
27 using std::endl;
28 using std::vector;
29 using std::string;
30 
31 using namespace AtomProbe;
32 
33 //Display human-readable output
34 void printSolution(const vector<Weight> &solnComponents,
35  const vector<string> &labels)
36 {
37  double cumulativeMass = 0.0f;
38  for(unsigned int ui=0;ui<solnComponents.size();ui++)
39  {
40  Weight w;
41  w = solnComponents[ui];
42  cout << labels[w.uniqueId];
43  if(ui != solnComponents.size()-1)
44  cout << ":";
45 
46  cumulativeMass +=w.mass;
47  }
48  cout << "\t" << cumulativeMass <<endl;
49 
50 }
51 
52 int main(int argc, const char *argv[])
53 {
54  vector<string> names;
55  names.push_back("A"); // weight "A", mass 1
56  names.push_back("B"); // Weight "B", mass 2
57  names.push_back("C"); // Weight "C", mass 4
58 
59  vector<Weight> inputWeights;
60 
61  //Identifier (2nd argument) is optional
62  inputWeights.push_back(Weight(1.005f,0));
63  inputWeights.push_back(Weight(2.003f,1));
64  inputWeights.push_back(Weight(4.003f,2));
65 
66  //Explain problem to user
67  const float TARGET=3.001f;
68  const float TOLERANCE=0.05f;
69  const unsigned int MAX_COMBINE=3;
70 
71  cout << "Target weight is :" << TARGET << " with tolerance :" << TOLERANCE << endl;
72  cout << "Searching up to :" << MAX_COMBINE << " allowed items per solution" << endl;
73  cout << endl;
74  cout << "Input masses:" << endl;
75  cout << "-------------" << endl;
76  for(auto ui=0;ui<inputWeights.size();ui++)
77  {
78  cout << names[ui] << " -> mass is : " << inputWeights[ui].mass << endl;
79  }
80  cout << "-------------" << endl;
81 
82  cout << endl;
83 
84  //Provide brute-force list of weights
85  // note that the brute-forcer may drop weights from calculation if it thinks they are not needed
86  vector<vector<Weight> > solutions;
87  MassTool::bruteKnapsack(inputWeights,TARGET,TOLERANCE,MAX_COMBINE,solutions);
88 
89 
90  //Loop over all solutions
91  for(auto & solution : solutions)
92  printSolution(solution,names);
93 
94 }
void printSolution(const vector< Weight > &solnComponents, const vector< string > &labels)
Definition: massFit.cpp:34
int main(int argc, const char *argv[])
Definition: massFit.cpp:52
static void bruteKnapsack(std::vector< Weight > &weights, float targetWeight, float tolerance, unsigned int maxObjects, std::vector< std::vector< Weight > > &solutions)
Depth-First-Search based brute force solve the knapsack problem,.
Definition: massTool.cpp:117
size_t uniqueId
Definition: massTool.h:36
Placeholder class for containing input weights for MassTool.
Definition: massTool.h:29