Skip to content

Commit 1885b1f

Browse files
committed
update sync/async
1 parent 1561715 commit 1885b1f

File tree

5 files changed

+148
-35
lines changed

5 files changed

+148
-35
lines changed
Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.mwg.experiments.kdtree;
22

33

4+
import org.mwg.Callback;
45
import org.mwg.experiments.smartgridprofiling.utility.GaussianProfile;
56
import org.mwg.ml.common.distance.GaussianDistance;
67

78
public class GaussianTreeNode extends GaussianProfile {
89

9-
KDNodeSync root;
10+
KDNode root;
1011

1112
public void setPrecisions(double[] precisions) {
1213
this.precisions = precisions;
@@ -15,61 +16,92 @@ public void setPrecisions(double[] precisions) {
1516
double[] precisions;
1617

1718

19+
1820
public GaussianTreeNode() {
1921
}
2022

2123

22-
public void internalLearn(final double[] values, final double[] features) {
24+
25+
26+
public void internalLearn(final double[] values, final double[] features, final Callback<Boolean> callback) {
2327
super.learn(values);
2428

2529
if (root == null) {
26-
root = new KDNodeSync();
30+
root = new KDNode();
2731
root.setDistance(new GaussianDistance(precisions));
2832
root.setThreshold(1.001);
2933

3034
GaussianProfile profile = new GaussianProfile();
3135
profile.learn(values);
32-
root.insert(features, profile);
36+
root.insert(features, profile, new Callback<Boolean>() {
37+
@Override
38+
public void on(Boolean result) {
39+
callback.on(true);
40+
}
41+
});
3342
} else {
34-
Object result = root.nearestWithinDistance(features);
35-
if (result != null) {
36-
GaussianProfile profile = (GaussianProfile) result;
37-
profile.learn(values);
38-
} else {
39-
GaussianProfile profile = new GaussianProfile();
40-
profile.learn(values);
41-
root.insert(features, profile);
42-
}
43-
43+
root.nearestWithinDistance(features, new Callback<Object>() {
44+
@Override
45+
public void on(Object result) {
46+
if (result != null) {
47+
GaussianProfile profile = (GaussianProfile) result;
48+
profile.learn(values);
49+
if (callback != null) {
50+
callback.on(true);
51+
}
52+
} else {
53+
GaussianProfile profile = new GaussianProfile();
54+
profile.learn(values);
55+
root.insert(features, profile, new Callback<Boolean>() {
56+
@Override
57+
public void on(Boolean result) {
58+
if (callback != null) {
59+
callback.on(true);
60+
}
61+
}
62+
});
63+
}
64+
65+
}
66+
});
4467
}
4568
}
4669

4770

48-
public int getNumNodes() {
71+
public int getNumNodes(){
4972
return root.getNum();
5073
}
5174

5275

53-
public double predictValue(double[] values) {
54-
76+
public void predictValue(double[] values, Callback<Double> callback){
77+
if(callback==null){
78+
return;
79+
}
5580

5681
double[] features = new double[values.length - 1];
5782
System.arraycopy(values, 0, features, 0, values.length - 1);
58-
if (root == null) {
59-
return 0;
60-
} else {
61-
Object result = root.nearestWithinDistance(features);
62-
if (result != null) {
63-
GaussianProfile profile = (GaussianProfile) result;
64-
double[] avg = profile.getAvg();
65-
Double res = avg[avg.length - 1];
66-
return res;
67-
} else {
68-
double[] avg = getAvg();
69-
Double res = avg[avg.length - 1];
70-
return res;
71-
}
83+
if(root==null){
84+
callback.on(null);
85+
return;
7286
}
87+
else {
88+
root.nearestWithinDistance(features, new Callback<Object>() {
89+
@Override
90+
public void on(Object result) {
91+
if (result != null) {
92+
GaussianProfile profile = (GaussianProfile) result;
93+
double[] avg = profile.getAvg();
94+
Double res = avg[avg.length - 1];
95+
callback.on(res);
96+
}
97+
else {
98+
double[] avg=getAvg();
99+
Double res= avg[avg.length-1];
100+
callback.on(res);
101+
}
102+
}
103+
});
73104

105+
}
74106
}
75-
}
107+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.mwg.experiments.kdtree;
2+
3+
4+
import org.mwg.experiments.smartgridprofiling.utility.GaussianProfile;
5+
import org.mwg.ml.common.distance.GaussianDistance;
6+
7+
public class GaussianTreeNodeSync extends GaussianProfile {
8+
9+
KDNodeSync root;
10+
11+
public void setPrecisions(double[] precisions) {
12+
this.precisions = precisions;
13+
}
14+
15+
double[] precisions;
16+
17+
18+
19+
public void internalLearn(final double[] values, final double[] features) {
20+
super.learn(values);
21+
22+
if (root == null) {
23+
root = new KDNodeSync();
24+
root.setDistance(new GaussianDistance(precisions));
25+
root.setThreshold(1.001);
26+
27+
GaussianProfile profile = new GaussianProfile();
28+
profile.learn(values);
29+
root.insert(features, profile);
30+
} else {
31+
Object result = root.nearestWithinDistance(features);
32+
if (result != null) {
33+
GaussianProfile profile = (GaussianProfile) result;
34+
profile.learn(values);
35+
} else {
36+
GaussianProfile profile = new GaussianProfile();
37+
profile.learn(values);
38+
root.insert(features, profile);
39+
}
40+
41+
}
42+
}
43+
44+
45+
public int getNumNodes() {
46+
return root.getNum();
47+
}
48+
49+
50+
public double predictValue(double[] values) {
51+
52+
53+
double[] features = new double[values.length - 1];
54+
System.arraycopy(values, 0, features, 0, values.length - 1);
55+
if (root == null) {
56+
return 0;
57+
} else {
58+
Object result = root.nearestWithinDistance(features);
59+
if (result != null) {
60+
GaussianProfile profile = (GaussianProfile) result;
61+
double[] avg = profile.getAvg();
62+
Double res = avg[avg.length - 1];
63+
return res;
64+
} else {
65+
double[] avg = getAvg();
66+
Double res = avg[avg.length - 1];
67+
return res;
68+
}
69+
}
70+
71+
}
72+
}

org.mwg.experiments.smartgridprofiling/src/main/java/org/mwg/experiments/kdtree/KDNodeTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.mwg.Graph;
66
import org.mwg.GraphBuilder;
77
import org.mwg.Node;
8+
import org.mwg.core.MWGResolver;
89
import org.mwg.core.scheduler.NoopScheduler;
910
import org.mwg.ml.MLPlugin;
1011
import org.mwg.ml.common.distance.EuclideanDistance;
@@ -26,7 +27,6 @@ public void KDInsertTest() {
2627
.withPlugin(new MLPlugin())
2728
.withScheduler(new NoopScheduler())
2829
.withMemorySize(1000000)
29-
.withOffHeapMemory()
3030
.build();
3131
graph.connect(new Callback<Boolean>() {
3232
@Override
@@ -90,13 +90,17 @@ public void on(Node[] result) {
9090
});
9191
graph.save(null);
9292
System.out.println("cache: "+graph.space().available());
93+
System.out.println("lookups: "+ MWGResolver.counter );
9394

95+
MWGResolver.counter=0;
9496

9597

9698
EuclideanDistance ed =new EuclideanDistance();
9799
double[] sum=new double[1];
98100
sum[0]=0;
99101

102+
103+
MWGResolver.counter=0;
100104
ts=System.nanoTime();
101105
for(int i=0;i<vecs.size();i++){
102106
double[] v1=vecs.get(i);
@@ -117,6 +121,8 @@ public void on(Node[] result) {
117121
time=tf-ts;
118122
time=time/1000000;
119123
System.out.println("Sum: "+sum[0]+" in "+time+" ms");
124+
System.out.println("lookups: "+ MWGResolver.counter );
125+
120126

121127

122128
}

org.mwg.experiments.smartgridprofiling/src/main/java/org/mwg/experiments/kdtree/KDNodeTestNOMWGSync.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.mwg.Callback;
55
import org.mwg.Graph;
66
import org.mwg.GraphBuilder;
7+
import org.mwg.core.CoreGraph;
8+
import org.mwg.core.MWGResolver;
79
import org.mwg.core.scheduler.NoopScheduler;
810
import org.mwg.ml.common.distance.EuclideanDistance;
911

@@ -79,8 +81,9 @@ public void on(Boolean result) {
7981
System.out.println("dist: " + formatter.format(new EuclideanDistance().measure(vec,key)));
8082
}
8183
System.out.println("cache: "+graph.space().available());
84+
System.out.println("lookups: "+ MWGResolver.counter );
8285

83-
86+
MWGResolver.counter=0;
8487

8588
EuclideanDistance ed =new EuclideanDistance();
8689
double[] sum=new double[1];
@@ -103,6 +106,7 @@ public void on(Boolean result) {
103106
System.out.println("Sum: "+sum[0]+" in "+time+" ms");
104107

105108

109+
106110
}
107111
});
108112
}

org.mwg.experiments.smartgridprofiling/src/main/java/org/mwg/experiments/mwgrelated/NoMWGParralelTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.mwg.experiments.mwgrelated;
22

33
import org.mwg.*;
4-
import org.mwg.core.scheduler.ExecutorScheduler;
54
import org.mwg.experiments.kdtree.GaussianTreeNode;
65
import org.mwg.importer.ImporterPlugin;
76
import org.mwg.task.Action;

0 commit comments

Comments
 (0)