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