Debian Codegraph @

current/ml/Subset.java

raw symbols SCIP
1 package ml;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Random;
6
7 import shared.Shared;
8 import shared.Tools;
9 import structures.FloatList;
10
11 public class Subset {
12
13 public Subset(ArrayList<Sample> sampleList) {
14 final Sample[] x=new Sample[0];
15 final ArrayList<Sample> pos=new ArrayList<Sample>();
16 final ArrayList<Sample> neg=new ArrayList<Sample>();
17 samples=sampleList.toArray(x);
18 for(Sample s : samples) {
19
20 if(s.goal[0]>=0.5f) {
21 pos.add(s);
22 }else{
23 neg.add(s);
24 }
25 }
26 positive=pos.toArray(x);
27 negative=neg.toArray(x);
28 }
29
30 //Sorts the samples by error magnitude, then interleaves positive and negative errors
31 void sortSamples(float fraction, boolean allowMultithreadedSort) {
32 fraction=(Tools.min(fraction, 1f));
33 // for(Sample s : samples) {
34 // assert(s.calcPivot()==s.pivot); //Should be done in subthread
35 // s.setPivot();
36 // }
37 if(allowMultithreadedSort) {
38 Shared.sort(positive, 0, (int)Math.ceil(fraction*positive.length));
39 Shared.sort(negative, 0, (int)Math.ceil(fraction*negative.length));
40 }else {
41 Arrays.sort(positive, 0, (int)Math.ceil(fraction*positive.length));
42 Arrays.sort(negative, 0, (int)Math.ceil(fraction*negative.length));
43 }
44 assert(positive.length<2 || positive[0].pivot>=positive[1].pivot) : positive[0].pivot+", "+positive[1].pivot;
45 // {
46 // final int plim=(int)(fraction*positive.length);
47 // final int nlim=(int)(fraction*negative.length);
48 // for(int i=0; i<plim; i++) {positive[i].pivot=positive[i].calcPivot();}
49 // for(int i=0; i<nlim; i++) {negative[i].pivot=negative[i].calcPivot();}
50 // Shared.sort(positive, 0, plim-1);
51 // Shared.sort(negative, 0, nlim-1);
52 // assert(plim<2 || positive[0].pivot>=positive[1].pivot) : positive[0].pivot+", "+positive[1].pivot;
53 // }
54
55 int apos=0, ppos=0, npos=0;
56 while(apos<samples.length) {
57 if(npos<negative.length) {
58 samples[apos]=negative[npos];
59 apos++;
60 npos++;
61 }
62 if(ppos<positive.length) {
63 samples[apos]=positive[ppos];
64 apos++;
65 ppos++;
66 }
67 }
68 assert(apos==samples.length);
69 assert(ppos==positive.length);
70 assert(npos==negative.length);
71 }
72
73 //Sorts the samples by error magnitude, then interleaves positive and negative errors
74 void sortSamples2(float sortFraction, int useSamples, boolean allowMultithreadedSort, FloatList pivots) {
75 sortFraction=(Tools.min(sortFraction, 1f));
76 // for(Sample s : samples) {
77 // assert(s.calcPivot()==s.pivot); //Should be done in subthread
78 // s.setPivot();
79 // }
80
81 // System.err.println(sortFraction+", "+useSamples);
82 final int pSortLimit=(int)Math.ceil(sortFraction*positive.length);
83 final int nSortLimit=(int)Math.ceil(sortFraction*negative.length);
84 final int pSwapLimit=Tools.min((useSamples+1)/2, positive.length);
85 final int nSwapLimit=Tools.min((useSamples+1)/2, negative.length);
86
87
88 // assert(pSwapLimit<=pSortLimit && pSwapLimit>0) :
89 // "\n"+pSwapLimit+", "+pSortLimit+", "+positive.length+
90 // "\n"+nSwapLimit+", "+nSortLimit+", "+negative.length+
91 // "\n"+useSamples+", "+sortFraction;
92 // assert(nSwapLimit<=nSortLimit && nSwapLimit>0) :
93 // "\n"+pSwapLimit+", "+pSortLimit+", "+positive.length+
94 // "\n"+nSwapLimit+", "+nSortLimit+", "+negative.length+
95 // "\n"+useSamples+", "+sortFraction;
96
97 //****TODO: fraction should be different for positive and negative.
98 //Basically, the same number from each should get sorted.
99 if(Trainer.PIVOT_SORT) {
100 pivotSort(positive, pivots, pSortLimit, pSwapLimit, allowMultithreadedSort);
101 pivotSort(negative, pivots, nSortLimit, nSwapLimit, allowMultithreadedSort);
102 }else {
103 sort(positive, pivots, pSortLimit, allowMultithreadedSort);
104 sort(negative, pivots, nSortLimit, allowMultithreadedSort);
105 }
106
107 int apos=0, ppos=0, npos=0;
108 while(apos<samples.length) {//Honestly, they don't all need to be inserted most of the time...
109 if(npos<negative.length) {
110 samples[apos]=negative[npos];
111 apos++;
112 npos++;
113 }
114 if(ppos<positive.length) {
115 samples[apos]=positive[ppos];
116 apos++;
117 ppos++;
118 }
119 }
120 assert(apos==samples.length);
121 assert(ppos==positive.length);
122 assert(npos==negative.length);
123 }
124
125 private static final void sort(final Sample[] samples, final FloatList pivots,
126 final int lim, final boolean mt) {
127 if(mt) {
128 Shared.sort(samples, 0, lim);
129 }else {
130 Arrays.sort(samples, 0, lim);
131 }
132 assert(samples.length<2 || samples[0].pivot>=samples[1].pivot) : samples[0].pivot+", "+samples[1].pivot;
133 }
134
135 private static final void pivotSort(final Sample[] samples, final FloatList pivots,
136 int sortLim, final int swapLim, final boolean mt) {
137 assert(sortLim>0 && sortLim<=samples.length);
138 assert(swapLim>0 && swapLim<=samples.length);
139 sortLim=Tools.mid(sortLim, swapLim*2, samples.length);
140 // if(swapLim>=samples.length) {return;}
141 pivots.clear();
142 for(int i=0; i<sortLim; i++) {
143 // assert(s.calcPivot()==s.pivot);
144 // s.setPivot();//Should be done in subthread
145 pivots.add(samples[i].pivot);
146 }
147 if(mt) {
148 Shared.sort(pivots.array, 0, sortLim);
149 }else {
150 Arrays.sort(pivots.array, 0, sortLim);
151 }
152 // assert(swapLim<=sortLim && swapLim>0) : swapLim+", "+sortLim+", "+samples.length;
153 //Here, we ensure the pivot is within the sorted range; otherwise there was no point in sorting
154
155 final int cutoffLoc=Tools.max(sortLim-swapLim, 0);
156 final float cutoff=pivots.get(cutoffLoc);
157 for(int src=0, dst=0; src<samples.length && dst<swapLim; src++) {
158 // System.err.println(src+", "+dst+", "+swapLim);
159 final float p=samples[src].pivot;
160 if(p>=cutoff) {
161 Sample temp=samples[dst];
162 samples[dst]=samples[src];
163 samples[src]=temp;
164 dst++;
165 }
166 }
167 // assert(samples.length<2 || lim>=samples.length-1 || samples[0].pivot>=samples[lim].pivot) :
168 // samples[0].pivot+", "+samples[lim].pivot+", "+samples[samples.length-1].pivot;
169 }
170
171 void triage(long currentEpoch, long startTriage, float positiveTriage, float negativeTriage) {
172 assert(currentEpoch>=startTriage);
173 if(positiveTriage<=0 && negativeTriage<=0) {return;}
174 final int distance=500;
175 {
176 final int max=Math.round(positiveTriage*positive.length);
177 if(max>0){
178 for(Sample s : positive) {s.setPivot();;}
179 Shared.sort(positive);
180 // Tools.reverseInPlace(positive);
181 for(int i=0; i<max; i++) {positive[i].setEpoch(currentEpoch+distance);}//send it to the future
182 for(int i=Tools.max(max, positive.length-max); i<positive.length; i++) {
183 // assert(positive[i].epoch>=currentEpoch) : positive[i].epoch+", "+currentEpoch+", "+i;
184 positive[i].setEpoch(currentEpoch);//Not necessary, but resets the old triage victims.
185 }
186 }
187 }
188 {
189 final int max=Math.round(negativeTriage*negative.length);
190 if(max>0){
191 for(Sample s : negative) {s.setPivot();}
192 Shared.sort(negative);
193 // Tools.reverseInPlace(negative);
194 for(int i=0; i<max; i++) {negative[i].setEpoch(currentEpoch+distance);}//send it to the future
195 for(int i=Tools.max(max, negative.length-max); i<negative.length; i++) {
196 // assert(negative[i].epoch>=currentEpoch) : negative[i].epoch+", "+currentEpoch+", "+i;
197 negative[i].setEpoch(currentEpoch);//Not necessary, but resets the old triage victims.
198 }
199 }
200 }
201 }
202
203 void shuffle() {
204 // Random randy=new Random(numShuffles);
205 for(int i=0; i<samples.length; i++) {
206 int idx=randy.nextInt(samples.length);
207 Sample s=samples[idx];
208 samples[idx]=samples[i];
209 samples[i]=s;
210 }
211 numShuffles++;
212 }
213 public void reset() {
214 nextFullPassEpoch=-1;
215 numShuffles=0;
216 randy.setSeed(0);
217 }
218
219 final Sample[] samples;
220 final Sample[] positive;
221 final Sample[] negative;
222 private final Random randy=new Random(0);
223
224 int nextFullPassEpoch=-1;
225 int numShuffles=0;
226
227 }