1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
Ray
Copyright (C) 2011, 2012 Sébastien Boisvert
http://DeNovoAssembler.SourceForge.Net/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You have received a copy of the GNU General Public License
along with this program (gpl-3.0.txt).
see <http://www.gnu.org/licenses/>
*/
#include "NovaEngine.h"
#include <math.h>
#include <assert.h>
/**
* The implementation of this method is test-driven.
* It tries to generalize a lot of cases
*/
int NovaEngine::choose(vector<map<int,int> >*distances,set<int>*invalidChoices,bool show){
vector<double> novaScores;
int choices=distances->size();
int withElements=0;
int initialChoice=0;
for(int i=0;i<choices;i++){
int n=distances->at(i).size();
if(n>0){
withElements++;
initialChoice=i;
}
}
if(withElements==1)
return initialChoice;
if(withElements==0)
return IMPOSSIBLE_CHOICE;
if(show){
cout<<"Ray NovaEngine"<<endl;
cout<<"Choices: "<<choices<<endl;
}
vector<int> maximumValues;
int theMaximum=0;
for(int i=0;i<choices;i++){
int maximumValue=0;
for(map<int,int>::iterator j=distances->at(i).begin();j!=distances->at(i).end();j++){
int distance=j->first;
if(distance>maximumValue)
maximumValue=distance;
}
maximumValues.push_back(maximumValue);
if(maximumValue>theMaximum)
theMaximum=maximumValue;
}
bool allHaveAtLeast2=true;
for(int i=0;i<choices;i++){
int numberOfEntriesForI=distances->at(i).size();
if(numberOfEntriesForI < 2)
allHaveAtLeast2=false;
}
/** choose with the maximum value, if possible */
double multiplicator=1.4;
for(int i=0;i<choices;i++){
bool win=true;
int numberOfEntriesForI=distances->at(i).size();
/* an invalid choice can not win */
if(invalidChoices->count(i)>0)
continue;
for(int j=0;j<choices;j++){
if(i==j)
continue;
int numberOfEntriesForJ=distances->at(j).size();
/* probably a sequencing error */
if((numberOfEntriesForJ >= numberOfEntriesForI && numberOfEntriesForI == 1)
|| (numberOfEntriesForJ >= 2*numberOfEntriesForI && numberOfEntriesForI < 3)){
win=false;
break;
}
/* an invalid choice does not need to be tested against */
if(invalidChoices->count(j)>0)
continue;
if(multiplicator*maximumValues[j] >= maximumValues[i]){
win=false;
break;
}
}
if(win)
return i;
}
for(int i=0;i<choices;i++){
int n=distances->at(i).size();
vector<int> observedDistances;
vector<int> weights;
int totalWeight=0;
for(map<int,int>::reverse_iterator j=distances->at(i).rbegin(); j!=distances->at(i).rend();j++){
observedDistances.push_back(j->first);
weights.push_back(j->second);
totalWeight+=j->second;
}
double score=0;
map<int,int> coverage;
/** change the number of bins depending on the range of values */
int step=32;
if(allHaveAtLeast2)
step=128;
if(theMaximum> 2048 && allHaveAtLeast2)
step=256;
if(theMaximum>8192 && allHaveAtLeast2)
step=512;
for(int j=0;j<n;j++){
int distance=observedDistances[j];
int weight=weights[j];
coverage[distance/step]+=weight;
}
for(map<int,int>::iterator j=coverage.begin();j!=coverage.end();j++){
score++;
}
if(show){
cout<<"step= "<<step<<endl;
cout<<"Choice: "<<i+1<<endl;
cout<<" DataPoints: "<<n<<endl;
for(int j=0;j<n;j++){
cout<<" "<<observedDistances[j]<<" "<<weights[j]<<"";
}
cout<<endl;
cout<<" NovaScore: "<<score<<endl;
}
novaScores.push_back(score);
}
int selection=IMPOSSIBLE_CHOICE;
for(int i=0;i<choices;i++){
bool winner=true;
if(invalidChoices->count(i) > 0)
continue;
for(int j=0;j<choices;j++){
if(i==j)
continue;
if(invalidChoices->count(j)>0)
continue;
if(maximumValues[i]>=maximumValues[j]*100)
continue;
if(novaScores[j]==1 && novaScores[i]==2)
winner=false;
if(novaScores[j]>=novaScores[i])
winner=false;
}
if(winner){
selection=i;
break;
}
}
if(show){
if(selection==IMPOSSIBLE_CHOICE){
cout<<"NovaEngine Selection: IMPOSSIBLE_CHOICE"<<endl;
}else{
cout<<"NovaEngine Selection: "<<selection+1<<endl;
}
}
return selection;
}
|