13 #include "geneorder.h"
14 #include "modelidentifier.h"
15 #include "genesorter.h"
16 #include "threadgenesorter.h"
17 #include "sortaction.h"
18 #include "genealgorithms.h"
21 static bool g_cancel = false;
23 void handler(int signal)
26 if (signal == SIGINT){
27 cerr << "Interrupted" << endl;
31 /* signal(SIGINT, handler); */
34 int main(int argc, char** argv){
36 signal(SIGINT, handler);
37 string ann = "default.ann";
39 bool detectModel = true;
40 bool onlyIdentify = false;
41 bool printPerm = false;
43 //Parse command line arguments
46 while ((opt = getopt(argc, argv, "im:n:t:hp")) != -1) {
49 model = Model::modelFactory(optarg);
56 threads = atoi(optarg);
65 cout << "Usage: " << argv[0] << " [OPTION] [FILE]" << endl
66 << endl << " -m <model> Specifies which model to use for sorting: Whirl, X, Zipper, FatX or Cloud "
67 << endl << " -n <ann> Specifies which artificial neural network to use for identification. '" << ann << "' is used by default"
68 << endl << " -i Only identify the model, don't try to sort it"
69 << endl << " -p Print the resulting permutation at each sorting step"
70 << endl << " -h Prints this help message"
72 << "With no FILE, or if FILE is '-', stdin will be used"
77 cerr << "Usage: " << argv[0] << " [-n <ann>] [-h] [FILE]" << endl;
85 if (optind == argc || *argv[optind] == '-'){
88 file.open(argv[optind]);
90 cerr << "Could not open file: '" << argv[optind] << "'" << endl;
96 //Parse the gene order permutation
98 copy(istream_iterator<int>(*in), istream_iterator<int>(),
100 GeneOrder go(g.begin(),g.end());
103 ModelIdentifier mi(ann);
104 priority_queue<pair<double,Model> > pq = mi.identify(go);
106 model = pq.top().second;
108 while (pq.size() > 0){
109 cout << "Model: " << pq.top().second.name() << " score: " << pq.top().first << endl;
112 cout << "Using model: " << model.name() << endl;
114 size_t dist = inversionDistance(go);
115 cout << "Distance: " << dist << endl;
116 //copy(go.begin(), go.end(), ostream_iterator<int>(cout, " "));
123 ThreadGeneSorter so(model, threads);
125 cout << "Press CTRL+C to stop" << endl;
126 while (!g_cancel && so.running()){
127 double score = so.wait(5,1000);
128 cout << "\r" << "Solutions: " << so.size()
129 << " Best score: " << score / dist;
132 cout << endl << "DONE: Waiting for threads to stop." << endl;
136 ThreadGeneSorter::SolutionsQueue solutions = so.solutions();
138 for (int i = 0; i < 10 && solutions.size() > 0; ++i){
139 pair<double,GeneSorter::ActionList> s = solutions.top();
143 for (GeneSorter::ActionList::iterator sa = s.second.begin();
144 sa != s.second.end(); ++sa){
145 cout << sa->toString() << " ";
147 cout << endl << "Score: " << s.first / dist << endl;
152 GeneSorter::ActionList al = so.sort(go,model);
158 for (GeneSorter::ActionList::iterator sa = al.begin(); sa != al.end(); ++sa){
159 cout << "Action: " << sa->toString() << " model score: " << model.score(*sa,temp) << endl;
161 score += model.score(*sa,temp);
164 copy(temp.begin(), temp.end(), ostream_iterator<int>(cout, " "));
168 cout << "Avg score: " << score / al.size() << endl;