]> ruin.nu Git - germs.git/blob - src/main.cpp
More help output
[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 << "  -i         Only identify the model, don't try to sort it"
48                                         << endl << "  -p         Print the resulting permutation at each sorting step"
49                                         << endl << "  -h         Prints this help message"
50                                         << endl << endl
51                                         << "With no FILE, or if FILE is '-', stdin will be used"
52                                         << endl;
53                                 exit(EXIT_SUCCESS);
54                                 break;
55                         default: /* '?' */
56                                 cerr << "Usage:  " << argv[0] << " [-n <ann>] [-h] [FILE]" << endl;
57                                 exit(EXIT_FAILURE);
58                 }
59         }
60
61         //Open file, or stdin
62         istream* in;
63         ifstream file;
64         if (optind == argc || *argv[optind] == '-'){
65                 in = &cin;
66         }else{
67                 file.open(argv[optind]);
68                 if (file.fail()){
69                         cerr << "Could not open file: '" << argv[optind] << "'" << endl;
70                         exit(EXIT_FAILURE);
71                 }
72                 in = &file;
73         }
74
75         //Parse the gene order permutation
76         vector<Gene> g;
77         copy(istream_iterator<int>(*in), istream_iterator<int>(),
78              back_inserter(g));
79         GeneOrder go(g.begin(),g.end());
80
81         //Identify the model
82         ModelIdentifier mi(ann);
83         priority_queue<pair<double,Model> > pq = mi.identify(go);
84         if (detectModel){
85                 model = pq.top().second;
86         }
87         while (pq.size() > 0){
88                 cout << "Model: " << pq.top().second.name() << " score: " << pq.top().first << endl;
89                 pq.pop();
90         }
91         cout << "Using model: " << model.name() << endl;
92
93         cout << "Distance: " << inversionDistance(go) << endl;
94         //copy(go.begin(), go.end(), ostream_iterator<int>(cout, " "));
95         //cout << endl;
96
97         if (onlyIdentify){
98                 return EXIT_SUCCESS;
99         }
100         //Sort
101         GeneSorter so;
102         GeneSorter::ActionList al = so.sort(go,model);
103
104         //Print the result
105         double score = 0;
106
107         GeneOrder temp(go);
108         for (GeneSorter::ActionList::iterator sa = al.begin(); sa != al.end(); ++sa){
109                 cout << "Action: " << sa->toString() << " model score: " << model.score(*sa,temp) << endl;
110                 (*sa)(temp);
111                 score += model.score(*sa,temp);
112
113                 if (printPerm){
114                         copy(temp.begin(), temp.end(), ostream_iterator<int>(cout, " "));
115                         cout << endl;
116                 }
117         }
118         cout << "Avg score: " << score / al.size() << endl;
119
120         return EXIT_SUCCESS;
121 }