]> ruin.nu Git - germs.git/blob - src/main.cpp
85569e1c295dfa614245be5e6c759f534d1cd525
[germs.git] / src / main.cpp
1 #include <iostream>
2 #include <vector>
3 #include <queue>
4 #include <iterator>
5 #include <fstream>
6 #include <cstdlib>
7
8 using namespace std;
9
10 #include <unistd.h>
11
12 #include "geneorder.h"
13 #include "modelidentifier.h"
14 #include "genesorter.h"
15 #include "threadgenesorter.h"
16 #include "sortaction.h"
17 #include "genealgorithms.h"
18 #include "model.h"
19
20 int main(int argc, char** argv){
21
22         string ann = "default.ann";
23         Model model(0);
24         bool detectModel = true;
25         bool onlyIdentify = false;
26         bool printPerm = false;
27
28         //Parse command line arguments
29         int opt;
30         int threads = 0;
31         while ((opt = getopt(argc, argv, "im:n:t:hp")) != -1) {
32                 switch (opt) {
33                         case 'm':
34                                 model = Model::modelFactory(optarg);
35                                 detectModel = false;
36                                 break;
37                         case 'n':
38                                 ann = optarg;
39                                 break;
40                         case 't':
41                                 threads = atoi(optarg);
42                                 break;
43                         case 'i':
44                                 onlyIdentify = true;
45                                 break;
46                         case 'p':
47                                 printPerm = true;
48                                 break;
49                         case 'h':
50                                 cout << "Usage: " << argv[0] << " [OPTION] [FILE]" << endl
51                                         << endl << "  -m <model> Specifies which model to use for sorting: Whirl, X, Zipper, FatX or Cloud "
52                                         << endl << "  -n <ann>   Specifies which artificial neural network to use for identification. '" << ann << "' is used by default"
53                                         << endl << "  -i         Only identify the model, don't try to sort it"
54                                         << endl << "  -p         Print the resulting permutation at each sorting step"
55                                         << endl << "  -h         Prints this help message"
56                                         << endl << endl
57                                         << "With no FILE, or if FILE is '-', stdin will be used"
58                                         << endl;
59                                 exit(EXIT_SUCCESS);
60                                 break;
61                         default: /* '?' */
62                                 cerr << "Usage:  " << argv[0] << " [-n <ann>] [-h] [FILE]" << endl;
63                                 exit(EXIT_FAILURE);
64                 }
65         }
66
67         //Open file, or stdin
68         istream* in;
69         ifstream file;
70         if (optind == argc || *argv[optind] == '-'){
71                 in = &cin;
72         }else{
73                 file.open(argv[optind]);
74                 if (file.fail()){
75                         cerr << "Could not open file: '" << argv[optind] << "'" << endl;
76                         exit(EXIT_FAILURE);
77                 }
78                 in = &file;
79         }
80
81         //Parse the gene order permutation
82         vector<Gene> g;
83         copy(istream_iterator<int>(*in), istream_iterator<int>(),
84              back_inserter(g));
85         GeneOrder go(g.begin(),g.end());
86
87         //Identify the model
88         ModelIdentifier mi(ann);
89         priority_queue<pair<double,Model> > pq = mi.identify(go);
90         if (detectModel){
91                 model = pq.top().second;
92         }
93         while (pq.size() > 0){
94                 cout << "Model: " << pq.top().second.name() << " score: " << pq.top().first << endl;
95                 pq.pop();
96         }
97         cout << "Using model: " << model.name() << endl;
98
99         cout << "Distance: " << inversionDistance(go) << endl;
100         //copy(go.begin(), go.end(), ostream_iterator<int>(cout, " "));
101         //cout << endl;
102
103         if (onlyIdentify){
104                 return EXIT_SUCCESS;
105         }
106         if (threads){
107                 ThreadGeneSorter so(model, threads);
108                 so.start(go);
109                 so.join();
110
111                 ThreadGeneSorter::SolutionsQueue solutions = so.solutions();
112
113                 while (solutions.size() > 0){
114                         pair<double,GeneSorter::ActionList> s = solutions.top();
115                         solutions.pop();
116
117                         cout << "Actions: ";
118                         for (GeneSorter::ActionList::iterator sa = s.second.begin(); sa != s.second.end(); ++sa){
119                                 cout << sa->toString() << " ";
120
121                         }
122                         cout << endl << "Score: " << s.first << endl;
123                 }
124                 
125         }else{
126                 //Sort
127                 GeneSorter so;
128                 GeneSorter::ActionList al = so.sort(go,model);
129
130                 //Print the result
131                 double score = 0;
132
133                 GeneOrder temp(go);
134                 for (GeneSorter::ActionList::iterator sa = al.begin(); sa != al.end(); ++sa){
135                         cout << "Action: " << sa->toString() << " model score: " << model.score(*sa,temp) << endl;
136                         (*sa)(temp);
137                         score += model.score(*sa,temp);
138
139                         if (printPerm){
140                                 copy(temp.begin(), temp.end(), ostream_iterator<int>(cout, " "));
141                                 cout << endl;
142                         }
143                 }
144                 cout << "Avg score: " << score / al.size() << endl;
145         }
146
147         return EXIT_SUCCESS;
148 }