Skip to content

Commit 621f063

Browse files
committed
add: process-D sampling by proximity
1 parent 0e6473f commit 621f063

File tree

3 files changed

+82
-29
lines changed

3 files changed

+82
-29
lines changed

src/main/java/ptrman/levels/retina/LineDetectorWithMultiplePoints.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
public class LineDetectorWithMultiplePoints {
1717
public List<ProcessA.Sample> samples = new ArrayList<>(); // actual samples which are "included" in the line
1818

19-
public IntList integratedSampleIndices;
20-
2119
// variable for the line drawing in the acceleration structure
2220
//public ArrayRealVector spatialAccelerationLineDirection; // can be null
2321
//public double spatialAccelerationLineLength; // can be null
@@ -44,10 +42,6 @@ public LineDetectorWithMultiplePoints(double xStep) {
4442
this.xStep = xStep;
4543
}
4644

47-
public boolean doesContainSampleIndex(int index) {
48-
return integratedSampleIndices.contains(index);
49-
}
50-
5145
/** compute sigmoid like activation function as described in
5246
* https://www.foundalis.com/res/Generalization_of_Hebbian_Learning_and_Categorization.pdf
5347
*/

src/main/java/ptrman/levels/retina/ProcessD.java

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
import org.apache.commons.math3.stat.regression.SimpleRegression;
1616
import org.eclipse.collections.api.IntIterable;
1717
import org.eclipse.collections.api.list.primitive.IntList;
18+
import org.eclipse.collections.api.tuple.primitive.IntIntPair;
1819
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
1920
import ptrman.Datastructures.Vector2d;
2021
import ptrman.bpsolver.HardParameters;
2122
import ptrman.bpsolver.Parameters;
2223
import ptrman.levels.retina.helper.ProcessConnector;
24+
import ptrman.math.ArrayRealVectorHelper;
2325
import ptrman.misc.Assert;
2426

2527
import java.util.*;
@@ -109,51 +111,101 @@ public void removeCandidatesBelowActivation(double threshold) {
109111
}
110112
}
111113

112-
// tries to sample a new line candidate
113-
public void sampleNew() {
114-
final double maxLength = Math.sqrt(squaredDistance(new double[]{imageSize.x, imageSize.y})); // max length of line
115-
116-
List<LineDetectorWithMultiplePoints> multiplePointsLineDetector = new ArrayList<>();
117114

118-
final List<ProcessA.Sample> workingSamples = inputSampleConnector.getWorkspace();
119-
120-
if (workingSamples.isEmpty()) {
121-
return;
115+
/**
116+
* selects random samples
117+
* @param source
118+
* @return
119+
*/
120+
public List<ProcessA.Sample> selectRandomSamples(List<ProcessA.Sample> source) {
121+
if (source.size() < 3) {
122+
return new ArrayList<>();
122123
}
123124

124125
// pick out random points
125126
int sampleIndex = 0; // NOTE< index in endosceletonPoint / workingSamples >
126127
IntArrayList allCandidateSampleIndices = new IntArrayList();
128+
for (final ProcessA.Sample iterationSample : source) {
129+
allCandidateSampleIndices.add(sampleIndex);
130+
sampleIndex++;
131+
}
132+
133+
IntList chosenCandidateSampleIndices = getRandomElements(allCandidateSampleIndices, 3, rng);
134+
return getSamplesByIndices(source, chosenCandidateSampleIndices);
135+
}
136+
137+
/**
138+
* tries to sample a new line candidate by picking random points
139+
*/
140+
public void sampleNewByRandom() {
141+
final double maxLength = Math.sqrt(squaredDistance(new double[]{imageSize.x, imageSize.y})); // max length of line
142+
143+
List<LineDetectorWithMultiplePoints> multiplePointsLineDetector = new ArrayList<>();
144+
145+
final List<ProcessA.Sample> workingSamples = inputSampleConnector.getWorkspace();
146+
147+
// filter valid points
148+
List<ProcessA.Sample> filteredSamples = new ArrayList<>();
127149
for (final ProcessA.Sample iterationSample : workingSamples) {
128150
boolean onlyAddEndoskeletonEnable = !(onlyEndoskeleton && iterationSample.type != ProcessA.Sample.EnumType.ENDOSCELETON);
129151
boolean isReferenced = iterationSample.refCount != 0;
130152
if(!isReferenced && onlyAddEndoskeletonEnable ) {
131-
allCandidateSampleIndices.add(sampleIndex);
153+
filteredSamples.add(iterationSample);
132154
}
133-
sampleIndex++;
134155
}
135156

157+
List<ProcessA.Sample> selectedSamples = selectRandomSamples(filteredSamples);
158+
tryCreateMultiLineDetector(maxLength, selectedSamples);
159+
}
136160

137-
IntList chosenCandidateSampleIndices = getRandomElements(allCandidateSampleIndices, 3, rng);
138-
List<ProcessA.Sample> selectedSamples = getSamplesByIndices(workingSamples, chosenCandidateSampleIndices);
161+
public double processDSampleByProximityProximity = 20.0; // maximal proximity of points to get considered for sampling by proximity
162+
163+
/**
164+
* tries to sample a new detector by proximity of points
165+
*/
166+
public void sampleNewByProximity() {
167+
final double maxLength = Math.sqrt(squaredDistance(new double[]{imageSize.x, imageSize.y})); // max length of line
168+
169+
final List<ProcessA.Sample> workingSamples = inputSampleConnector.getWorkspace();
139170

140-
tryCreateMultiLineDetector(maxLength, chosenCandidateSampleIndices, selectedSamples);
171+
if (workingSamples.size() < 2) {
172+
return;
173+
}
174+
175+
int centerPointIdx = rng.nextInt(workingSamples.size());
176+
IntIntPair centerPointPos = workingSamples.get(centerPointIdx).position;
177+
178+
// find all points in proximity
179+
List<ProcessA.Sample> proximitySamples = new ArrayList<>();
180+
for(ProcessA.Sample iSample : workingSamples) {
181+
double dist = ArrayRealVectorHelper.distance(centerPointPos, iSample.position);
182+
if (dist > processDSampleByProximityProximity) {
183+
continue;
184+
}
185+
186+
boolean onlyAddEndoskeletonEnable = !(onlyEndoskeleton && iSample.type != ProcessA.Sample.EnumType.ENDOSCELETON);
187+
boolean isReferenced = iSample.refCount != 0;
188+
if(!isReferenced && onlyAddEndoskeletonEnable ) {
189+
proximitySamples.add(iSample);
190+
}
191+
}
141192

193+
// try to create line detector by selecting random points from candidates
194+
List<ProcessA.Sample> selectedSamples = selectRandomSamples(proximitySamples);
195+
tryCreateMultiLineDetector(maxLength, selectedSamples);
142196
}
143197

144198
public double processDNumberOfPointsToActivationScale = 0.15; // how much does a point improve the scaling
145199

146-
private void tryCreateMultiLineDetector(
147-
double maxLength,
148-
IntList chosenCandidateSampleIndices, List<ProcessA.Sample> selectedSamples) {
200+
private void tryCreateMultiLineDetector(double maxLength, List<ProcessA.Sample> selectedSamples) {
149201
// commented check because we don't assume object id's anymore (because it was from Phaeaco for solving BP's)
150202
//final boolean doAllSamplesHaveId = doAllSamplesHaveObjectId(selectedSamples);
151203
//if (!doAllSamplesHaveId) {
152204
// return;
153205
//}
154206

155-
if (selectedSamples.size() == 0) {
156-
return; // special case
207+
if (selectedSamples.size() < 2) {
208+
return; // line is not defined
157209
}
158210

159211
// check if object ids are the same
@@ -180,14 +232,13 @@ private void tryCreateMultiLineDetector(
180232
}
181233
// else we are here
182234

183-
if(chosenCandidateSampleIndices.size() <= 2) { // the regression mse is not defined if it are only two points
235+
if(selectedSamples.size() <= 2) { // the regression mse is not defined if it are only two points
184236
return; // only create detector if we have at least three samples
185237
}
186238

187239

188240
// create new line detector
189241
LineDetectorWithMultiplePoints createdLineDetector = new LineDetectorWithMultiplePoints(lineDetectorInitialXStep);
190-
createdLineDetector.integratedSampleIndices = chosenCandidateSampleIndices;
191242
createdLineDetector.samples = selectedSamples;
192243

193244
Assert.Assert(areObjectIdsTheSameOfSamples(selectedSamples), "");
@@ -198,7 +249,7 @@ private void tryCreateMultiLineDetector(
198249

199250
boolean addCreatedLineDetector = false;
200251

201-
if (createdLineDetector.integratedSampleIndices.size() == 2) {
252+
if (createdLineDetector.samples.size() == 2) {
202253
createdLineDetector.mse = 0.0f;
203254

204255
createdLineDetector.n = regressionResult.n;
@@ -279,7 +330,8 @@ private static List<RetinaPrimitive> splitDetectorsIntoLines(Iterable<LineDetect
279330
* processing step
280331
*/
281332
public void step() {
282-
sampleNew();
333+
sampleNewByRandom();
334+
sampleNewByProximity();
283335
tryWiden();
284336
detectorsHarden();
285337
detectorsFadeActivation();

src/main/java/ptrman/math/ArrayRealVectorHelper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,11 @@ public static double distance(IntIntPair a, ArrayRealVector b) {
123123
double dy = b.getEntry(1) - a.getTwo();
124124
return Math.sqrt( dx*dx+dy*dy);
125125
}
126+
// TODO< rename class >
127+
/** cartesian distance */
128+
public static double distance(IntIntPair a, IntIntPair b) {
129+
double dx = b.getOne() - a.getOne();
130+
double dy = b.getTwo() - a.getTwo();
131+
return Math.sqrt( dx*dx+dy*dy);
132+
}
126133
}

0 commit comments

Comments
 (0)