Debian Codegraph @

current/align2/Solver.java

raw symbols SCIP
1 package align2;
2
3 import java.util.Arrays;
4
5 public class Solver {
6
7
8 public static final long bruteForce(int[] offsets, int[] lengths, int chunk, int minLists, int maxTotalLength){
9
10 int bits=offsets.length;
11 int max=(1<<bits)-1;
12
13 for(long i=0; i<=max; i++){
14 long x=evaluate(offsets, lengths, chunk, i);
15 }
16
17 assert(false);
18 return 0;
19 }
20
21
22 public static final void findWorstGreedy(final int[] offsets, final int[] lengths,
23 final int chunk, final int[] lists, int[] r){
24 assert(r!=null && r.length==2);
25
26 long min=Long.MAX_VALUE;
27 int worstIndex=-1;
28 for(int i=0; i<lists.length; i++){
29 long value=valueOfElement(offsets, lengths, 1f, chunk, lists, i);
30 if(value<min){
31 if(min<EARLY_TERMINATION_SCORE){//Can speed up greedy algo
32 r[0]=i;
33 r[1]=(value<Integer.MIN_VALUE ? Integer.MIN_VALUE : value>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)value);
34 return;
35 }
36 min=value;
37 worstIndex=i;
38 }
39 }
40 // if(min>0){worstIndex=-1;}
41 r[0]=worstIndex;
42 r[1]=(min<Integer.MIN_VALUE ? Integer.MIN_VALUE : min>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)min);
43 }
44
45
46 public static final void findWorstGreedy(final int[] offsets, final int[] lengths,
47 final float[] weights, final int chunk, final int[] lists, int[] r){
48 assert(r!=null && r.length==2);
49
50 long min=Long.MAX_VALUE;
51 int worstIndex=-1;
52 for(int i=0; i<lists.length; i++){
53 // for(int i=lists.length-1; i>=0; i--){
54 long value=valueOfElement(offsets, lengths, weights[i], chunk, lists, i);
55 if(value<min){
56 if(min<EARLY_TERMINATION_SCORE && i!=0){//Can speed up greedy algo
57 r[0]=i;
58 r[1]=(value<Integer.MIN_VALUE ? Integer.MIN_VALUE : value>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)value);
59 // System.out.print(".");
60 return;
61 }
62 min=value;
63 worstIndex=i;
64 }
65 }
66 // if(min>0){worstIndex=-1;}
67 r[0]=worstIndex;
68 r[1]=(min<Integer.MIN_VALUE ? Integer.MIN_VALUE : min>Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)min);
69 }
70
71
72 public static long valueOfElement(final int[] offsets, final int[] lengths, float keyWeight,
73 final int chunk, final int[] lists, int index){
74
75 final int numlists=lists.length;
76 if(numlists<1){return 0;}
77
78 final int prospect=lists[index];
79 if(lengths[prospect]==0){return -999999;}
80
81 long valuep=POINTS_PER_LIST+(POINTS_PER_LIST*2/lists.length)+((POINTS_PER_LIST*10)/lengths[prospect]);
82 long valuem=POINTS_PER_SITE*lengths[prospect];
83
84 if(prospect==0 || (prospect==offsets.length-1)){
85 valuep+=BONUS_POINTS_FOR_END_LIST;
86 }
87
88 if(numlists==1){
89 valuep+=(POINTS_FOR_TOTAL_LIST_WIDTH+POINTS_PER_BASE1)*chunk;
90 return ((long)(valuep*keyWeight))+valuem;
91 }
92
93
94 final int first=lists[0];
95 final int last=lists[numlists-1];
96
97 //Offsets of elements to the left and right of the prospect
98 // final int offL=(prospect==first ? - : offsets[lists[index-1]]);
99 // final int offP=offsets[prospect];
100 // final int offR=(prospect==last ? offsets[offsets.length-1] : offsets[lists[index+1]]);
101 // assert(offL<=offP);
102 // assert(offP<=offR);
103 // assert(offL<offR) : "\noffsets.length="+offsets.length+", lengths.length="+lengths.length+"\n"+
104 // ", chunk="+chunk+", lists.length="+lists.length+", index="+index+"\n"+
105 // ", offL="+offL+", offR="+offR+", prospect="+prospect+", first="+first+", last="+last+"\n"+
106 // ", valuep="+valuep+", valuem="+valuem+", weight="+keyWeight+"\n"+
107 // "offsets = "+Arrays.toString(offsets)+"\tlengths = "+Arrays.toString(lengths)+"\nlists = "+Arrays.toString(lists)+"\n";
108
109 final int offL=(prospect==first ? -1 : offsets[lists[index-1]]);
110 final int offP=offsets[prospect];
111 final int offR=(prospect==last ? offsets[offsets.length-1]+1 : offsets[lists[index+1]]);
112 assert(offL<=offP);
113 assert(offP<=offR);
114 assert(offL<offR) : "\noffsets.length="+offsets.length+", lengths.length="+lengths.length+"\n"+
115 ", chunk="+chunk+", lists.length="+lists.length+", index="+index+"\n"+
116 ", offL="+offL+", offR="+offR+", prospect="+prospect+", first="+first+", last="+last+"\n"+
117 ", valuep="+valuep+", valuem="+valuem+", weight="+keyWeight+"\n"+
118 "offsets = "+Arrays.toString(offsets)+"\tlengths = "+Arrays.toString(lengths)+"\nlists = "+Arrays.toString(lists)+"\n";
119
120 int oldLeftSpace=offP-offL;
121 int oldRightSpace=offR-offP;
122 int newSpace=offR-offL;
123
124 // int oldLeftSpace=Tools.max((offP-offL)-1, 0)+1;
125 // int oldRightSpace=Tools.max((offR-offP)-1, 0)+1;
126 // int newSpace=Tools.max(offR-offL;
127
128 long spaceScore=((oldLeftSpace*oldLeftSpace+oldRightSpace*oldRightSpace)-(newSpace*newSpace))*MULT_FOR_SPACING_PENALTY;
129 assert(spaceScore>0) : "\n"+spaceScore+", "+oldLeftSpace+", "+oldRightSpace+", "+newSpace+"\n"+
130 Arrays.toString(offsets)+"\nprospect="+prospect+"\n";
131 valuep+=spaceScore;
132
133 int uniquelyCovered;
134 if(prospect==first){
135 uniquelyCovered=offR-offP; //Technically, -1 should be added
136 }else if(prospect==last){
137 uniquelyCovered=offP-offL; //Technically, -1 should be added
138 }else{
139 int a=offL+chunk;
140 int b=offR-a;
141 uniquelyCovered=(b>0 ? b : 0);
142 }
143
144 if(prospect==first || prospect==last){
145 valuep+=(POINTS_PER_BASE1+POINTS_FOR_TOTAL_LIST_WIDTH)*uniquelyCovered;
146 }else{
147 valuep+=POINTS_PER_BASE1*uniquelyCovered;
148 }
149
150 return ((long)(valuep*keyWeight))+valuem;
151 }
152
153 public static int[] toBitList(final int key){
154 final int numlists=Integer.bitCount(key);
155 final int[] lists=new int[numlists];
156 for(int i=0, ptr=0; ptr<numlists; i++){
157 if((masks32[i]&key)!=0){
158 lists[ptr]=i;
159 ptr++;
160 }
161 }
162 return lists;
163 }
164
165 public static int[] toBitList(final long key){
166 final int numlists=Long.bitCount(key);
167 assert(numlists>0);
168 final int[] lists=new int[numlists];
169 for(int i=0, ptr=0; ptr<numlists; i++){
170 if((masks[i]&key)!=0){
171 lists[ptr]=i;
172 ptr++;
173 }
174 }
175 return lists;
176 }
177
178 public static long evaluate(int[] offsets, int[] lengths, final int chunk, final long key){
179
180 long score=0;
181
182 final int[] lists=toBitList(key);
183 final int numlists=lists.length;
184
185 final int first=lists[0];
186 final int last=lists[numlists-1];
187
188 score+=numlists*POINTS_PER_LIST;
189 for(int i=0; i<numlists; i++){
190 int list=lists[i];
191 score+=POINTS_PER_SITE*lengths[list];
192 }
193 if(first==0){score+=BONUS_POINTS_FOR_END_LIST;}
194 if(last==offsets.length-1){score+=BONUS_POINTS_FOR_END_LIST;}
195
196 score+=(POINTS_FOR_TOTAL_LIST_WIDTH*(offsets[last]-offsets[first]+chunk));
197
198 //TODO: Special case both ends
199 for(int i=1; i<numlists; i++){
200 int list1=lists[i-1];
201 int list2=lists[i];
202 int space=offsets[list2]-offsets[list1];
203 int uncovered=space>chunk ? space-chunk : 0;
204
205 score+=MULT_FOR_SPACING_PENALTY*(space*space);
206 score-=POINTS_PER_BASE1*uncovered;
207 }
208
209 if(first>0){
210 long x=offsets[first];
211 score+=MULT_FOR_SPACING_PENALTY*(x*x);
212 score-=POINTS_PER_BASE1*x;
213 }
214
215 if(last<(offsets.length-1)){
216 long x=offsets[offsets.length-1]-offsets[last];
217 score+=MULT_FOR_SPACING_PENALTY*(x*x);
218 score-=POINTS_PER_BASE1*x;
219 }
220
221 return score;
222 }
223
224 public static final int BASE_POINTS_PER_SITE=-50; //Used to set POINTS_PER_SITE
225 public static long POINTS_PER_SITE=-50; //TODO: Make private with a get() and set() function
226
227 public static final long MULT_FOR_SPACING_PENALTY=-30;
228
229 public static long EARLY_TERMINATION_SCORE=(POINTS_PER_SITE*2000); //TODO: Should be set dynamically
230
231 public static final long POINTS_PER_LIST=30000;
232 public static final long POINTS_PER_BASE1=6000; //Points for a base covered once
233 public static final long POINTS_PER_BASE2=1000;//POINTS_PER_BASE1/4; //Points for a base covered twice
234 public static final long BONUS_POINTS_FOR_END_LIST=40000; //Extra points for the first and last list
235 public static final long POINTS_FOR_TOTAL_LIST_WIDTH=5500; //multiplier for distance between first and last list
236
237 public static final long[] masks=new long[64];
238 public static final int[] masks32=new int[32];
239 static{
240 for(int i=0; i<masks.length; i++){masks[i]=(1L<<i);}
241 for(int i=0; i<masks32.length; i++){masks32[i]=(1<<i);}
242 }
243 }