]> ruin.nu Git - germs.git/blob - src/main.cpp
2e074957b459fbd72c3b5b2ada38fd7ca96753bb
[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
24         int opt;
25         while ((opt = getopt(argc, argv, "m:n:h")) != -1) {
26                 switch (opt) {
27                         case 'm':
28                                 model = Model::modelFactory(optarg);
29                                 detectModel = false;
30                                 break;
31                         case 'n':
32                                 ann = optarg;
33                                 break;
34                         case 'h':
35                                 cout << "Usage: " << argv[0] << " [OPTION] [FILE]" << endl
36                                         << endl << "  -m <model> Specifies which model to use for sorting: Whirl, X, Zipper, FatX or Cloud "
37                                         << endl << "  -n <ann>   Specifies which artificial neural network to use for identification. '" << ann << "' is used by default"
38                                         << endl << "  -h         Prints this help message"
39                                         << endl << endl
40                                         << "With no FILE, or if FILE is '-', stdin will be used"
41                                         << endl;
42                                 exit(EXIT_SUCCESS);
43                                 break;
44                         default: /* '?' */
45                                 cerr << "Usage:  " << argv[0] << " [-n <ann>] [-h] [FILE]" << endl;
46                                 exit(EXIT_FAILURE);
47                 }
48         }
49
50         istream* in;
51         ifstream file;
52         if (optind == argc || *argv[optind] == '-'){
53                 in = &cin;
54         }else{
55                 file.open(argv[optind]);
56                 if (file.fail()){
57                         cerr << "Could not open file: '" << argv[optind] << "'" << endl;
58                         exit(EXIT_FAILURE);
59                 }
60                 in = &file;
61         }
62         //TODO: Parse
63         vector<Gene> g;
64         copy(istream_iterator<int>(*in), istream_iterator<int>(),
65                      back_inserter(g));   
66         GeneOrder go(g.begin(),g.end());
67
68         //TODO: Identify
69         ModelIdentifier mi(ann);
70         priority_queue<pair<double,Model> > pq = mi.identify(go);
71         if (detectModel){
72                 model = pq.top().second;
73         }
74         while (pq.size() > 0){
75                 cout << "Model: " << pq.top().second.name() << " score: " << pq.top().first << endl;
76                 pq.pop();
77         }
78         cout << "Using model: " << model.name() << endl;
79
80         cout << "Distance: " << inversionDistance(go) << endl;
81         //copy(go.begin(), go.end(), ostream_iterator<int>(cout, " "));
82         //cout << endl;
83
84         //TODO: Chose a sorter
85         GeneSorter so;
86         //TODO: Sort
87         GeneSorter::ActionList al = so.sort(go,model);
88
89         double score = 0;
90         for (GeneSorter::ActionList::iterator sa = al.begin(); sa != al.end(); ++sa){
91                 cout << "Action: " << sa->toString() << " model score: " << model.score(*sa,go) << endl;
92                 score += model.score(*sa,go);
93         }
94         cout << "Avg score: " << score / al.size() << endl;
95         //TODO: Print result
96         return EXIT_SUCCESS;
97 }