Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bordeaux.services; |
| 18 | |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 20 | import android.location.Location; |
| 21 | import android.text.format.Time; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import java.util.ArrayList; |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 25 | import java.util.HashMap; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 26 | import java.util.HashSet; |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.Map; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * ClusterManager incrementally indentify representitve clusters from the input location |
| 32 | * stream. Clusters are updated online using leader based clustering algorithm. The input |
| 33 | * locations initially are kept by the clusters. Periodially, a cluster consolidating |
| 34 | * procedure is carried out to refine the cluster centers. After consolidation, the |
| 35 | * location data are released. |
| 36 | */ |
| 37 | public class ClusterManager { |
| 38 | |
| 39 | private static String TAG = "ClusterManager"; |
| 40 | |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 41 | private static float LOCATION_CLUSTER_RADIUS = 50; // meter |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 42 | |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 43 | private static float SEMANTIC_CLUSTER_RADIUS = 100; // meter |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 44 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 45 | // Consoliate location clusters (and check for new semantic clusters) |
| 46 | // every 30 minutes (1800 seconds). |
| 47 | private static final long CONSOLIDATE_INTERVAL = 1800; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 48 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 49 | // Prune away clusters that are stayed for less than 3 minutes (180 seconds) |
| 50 | private static long LOCATION_CLUSTER_THRESHOLD = 180; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 51 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 52 | // A location cluster can be labeled as a semantic cluster if it has been |
| 53 | // stayed for at least 10 minutes (600 seconds) within a day. |
| 54 | private static final long SEMANTIC_CLUSTER_THRESHOLD = 600; // seconds |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 55 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 56 | // Reset location cluters every 6 hours (21600 seconds). |
| 57 | private static final long LOCATION_REFRESH_PERIOD = 21600; // seconds |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 58 | |
| 59 | private static String UNKNOWN_LOCATION = "Unknown Location"; |
| 60 | |
| 61 | private static String HOME = "Home"; |
| 62 | |
| 63 | private static String OFFICE = "Office"; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 64 | |
| 65 | private Location mLastLocation = null; |
| 66 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 67 | private long mClusterDuration; |
| 68 | |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 69 | private long mTimeRef = 0; |
| 70 | |
| 71 | private long mSemanticClusterCount = 0; |
| 72 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 73 | private ArrayList<LocationCluster> mLocationClusters = new ArrayList<LocationCluster>(); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 74 | |
| 75 | private ArrayList<SemanticCluster> mSemanticClusters = new ArrayList<SemanticCluster>(); |
| 76 | |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 77 | private AggregatorRecordStorage mStorage; |
| 78 | |
| 79 | private static String SEMANTIC_TABLE = "SemanticTable"; |
| 80 | |
| 81 | private static String SEMANTIC_ID = "ID"; |
| 82 | |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 83 | private static final String SEMANTIC_LONGITUDE = "Longitude"; |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 84 | |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 85 | private static final String SEMANTIC_LATITUDE = "Latitude"; |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 86 | |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 87 | private static final String[] SEMANTIC_COLUMNS = |
| 88 | new String[]{ SEMANTIC_ID, |
| 89 | SEMANTIC_LONGITUDE, |
| 90 | SEMANTIC_LATITUDE, |
| 91 | TimeStatsAggregator.WEEKEND, |
| 92 | TimeStatsAggregator.WEEKDAY, |
| 93 | TimeStatsAggregator.MORNING, |
| 94 | TimeStatsAggregator.NOON, |
| 95 | TimeStatsAggregator.AFTERNOON, |
| 96 | TimeStatsAggregator.EVENING, |
| 97 | TimeStatsAggregator.NIGHT, |
| 98 | TimeStatsAggregator.LATENIGHT }; |
| 99 | |
| 100 | private static final int mFeatureValueStart = 3; |
| 101 | private static final int mFeatureValueEnd = 10; |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 102 | |
| 103 | public ClusterManager(Context context) { |
| 104 | mStorage = new AggregatorRecordStorage(context, SEMANTIC_TABLE, SEMANTIC_COLUMNS); |
| 105 | |
| 106 | loadSemanticClusters(); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public void addSample(Location location) { |
| 110 | float bestClusterDistance = Float.MAX_VALUE; |
| 111 | int bestClusterIndex = -1; |
| 112 | long lastDuration; |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 113 | long currentTime = location.getTime() / 1000; // measure time in seconds |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 114 | |
| 115 | if (mLastLocation != null) { |
| 116 | // get the duration spent in the last location |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 117 | long duration = (location.getTime() - mLastLocation.getTime()) / 1000; |
| 118 | mClusterDuration += duration; |
| 119 | |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 120 | Log.v(TAG, "sample duration: " + duration + |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 121 | ", number of clusters: " + mLocationClusters.size()); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 122 | |
| 123 | // add the last location to cluster. |
| 124 | // first find the cluster it belongs to. |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 125 | for (int i = 0; i < mLocationClusters.size(); ++i) { |
| 126 | float distance = mLocationClusters.get(i).distanceToCenter(mLastLocation); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 127 | Log.v(TAG, "clulster " + i + " is within " + distance + " meters"); |
| 128 | if (distance < bestClusterDistance) { |
| 129 | bestClusterDistance = distance; |
| 130 | bestClusterIndex = i; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // add the location to the selected cluster |
| 135 | if (bestClusterDistance < LOCATION_CLUSTER_RADIUS) { |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 136 | mLocationClusters.get(bestClusterIndex).addSample(mLastLocation, duration); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 137 | } else { |
| 138 | // if it is far away from all existing clusters, create a new cluster. |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 139 | LocationCluster cluster = new LocationCluster(mLastLocation, duration); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 140 | // move the center of the new cluster if its covering region overlaps |
| 141 | // with an existing cluster. |
| 142 | if (bestClusterDistance < 2 * LOCATION_CLUSTER_RADIUS) { |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 143 | Log.v(TAG, "move away activated"); |
| 144 | cluster.moveAwayCluster(mLocationClusters.get(bestClusterIndex), |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 145 | ((float) 2 * LOCATION_CLUSTER_RADIUS)); |
| 146 | } |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 147 | mLocationClusters.add(cluster); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 148 | } |
| 149 | } else { |
| 150 | mTimeRef = currentTime; |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 151 | |
| 152 | if (mLocationClusters.isEmpty()) { |
| 153 | mClusterDuration = 0; |
| 154 | } |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | long collectDuration = currentTime - mTimeRef; |
| 158 | Log.e(TAG, "collect duration: " + collectDuration); |
| 159 | if (collectDuration > CONSOLIDATE_INTERVAL) { |
| 160 | // TODO : conslidation takes time. move this to a separate thread later. |
| 161 | consolidateClusters(collectDuration); |
| 162 | mTimeRef = currentTime; |
| 163 | } |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 164 | mLastLocation = location; |
| 165 | } |
| 166 | |
| 167 | private void consolidateClusters(long duration) { |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 168 | LocationCluster cluster; |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 169 | for (int i = mLocationClusters.size() - 1; i >= 0; --i) { |
| 170 | cluster = mLocationClusters.get(i); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 171 | cluster.consolidate(duration); |
| 172 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 173 | // remove transit clusters |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 174 | if (!cluster.passThreshold(LOCATION_CLUSTER_THRESHOLD)) { |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 175 | mLocationClusters.remove(cluster); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | // merge clusters whose regions are overlapped. note that after merge |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 180 | // cluster center changes but cluster size remains unchanged. |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 181 | for (int i = mLocationClusters.size() - 1; i >= 0; --i) { |
| 182 | cluster = mLocationClusters.get(i); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 183 | for (int j = i - 1; j >= 0; --j) { |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 184 | float distance = mLocationClusters.get(j).distanceToCluster(cluster); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 185 | if (distance < LOCATION_CLUSTER_RADIUS) { |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 186 | mLocationClusters.get(j).absorbCluster(cluster); |
| 187 | mLocationClusters.remove(cluster); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 191 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 192 | // check if new semantic clusters are found |
| 193 | if (findNewSemanticClusters() && |
| 194 | mClusterDuration < LOCATION_REFRESH_PERIOD) { |
| 195 | saveSemanticClusters(); |
| 196 | } |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 197 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 198 | if (mClusterDuration > LOCATION_REFRESH_PERIOD) { |
| 199 | updateSemanticClusters(); |
| 200 | mClusterDuration = 0; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private boolean findNewSemanticClusters() { |
| 205 | // select candidate location clusters |
| 206 | ArrayList<LocationCluster> candidates = new ArrayList<LocationCluster>(); |
| 207 | for (LocationCluster cluster : mLocationClusters) { |
| 208 | if (!cluster.hasSemanticId() && |
| 209 | cluster.passThreshold(SEMANTIC_CLUSTER_THRESHOLD)) { |
| 210 | candidates.add(cluster); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // assign each candidate to a semantic cluster |
| 215 | boolean foundNewClusters = false; |
| 216 | for (LocationCluster candidate : candidates) { |
| 217 | // find the closest semantic cluster |
| 218 | float bestClusterDistance = Float.MAX_VALUE; |
| 219 | SemanticCluster bestCluster = null; |
| 220 | for (SemanticCluster cluster : mSemanticClusters) { |
| 221 | float distance = cluster.distanceToCluster(candidate); |
| 222 | Log.v(TAG, "distance to semantic cluster: " + cluster.getSemanticId()); |
| 223 | |
| 224 | if (distance < bestClusterDistance) { |
| 225 | bestClusterDistance = distance; |
| 226 | bestCluster = cluster; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // if candidate doesn't belong to any semantic cluster, create a new |
| 231 | // semantic cluster |
| 232 | if (bestClusterDistance > SEMANTIC_CLUSTER_RADIUS) { |
| 233 | // if it is far away from all existing clusters, create a new cluster. |
| 234 | bestCluster = new SemanticCluster(candidate, mSemanticClusterCount++); |
| 235 | String id = bestCluster.getSemanticId(); |
| 236 | |
| 237 | // Add new semantic cluster to the list. |
| 238 | mSemanticClusters.add(bestCluster); |
| 239 | |
| 240 | foundNewClusters = true; |
| 241 | } |
| 242 | candidate.setSemanticId(bestCluster.getSemanticId()); |
| 243 | } |
| 244 | candidates.clear(); |
| 245 | return foundNewClusters; |
| 246 | } |
| 247 | |
| 248 | private void updateSemanticClusters() { |
| 249 | synchronized (mSemanticClusters) { |
| 250 | // initialize semanticMap |
| 251 | HashMap<String, ArrayList<BaseCluster> > semanticMap = |
| 252 | new HashMap<String, ArrayList<BaseCluster> >(); |
| 253 | for (SemanticCluster cluster : mSemanticClusters) { |
| 254 | String semanticId = cluster.getSemanticId(); |
| 255 | semanticMap.put(cluster.getSemanticId(), new ArrayList<BaseCluster>()); |
| 256 | semanticMap.get(semanticId).add(cluster); |
| 257 | } |
| 258 | |
| 259 | // assign each candidate to a semantic cluster |
| 260 | for (LocationCluster cluster : mLocationClusters) { |
| 261 | if (cluster.hasSemanticId()) { |
| 262 | semanticMap.get(cluster.getSemanticId()).add(cluster); |
| 263 | } |
| 264 | } |
| 265 | // reset location clusters. |
| 266 | mLocationClusters.clear(); |
| 267 | mLastLocation = null; |
| 268 | mTimeRef = 0; |
| 269 | |
| 270 | // use candidates semantic cluster |
| 271 | BaseCluster newCluster = new BaseCluster(); |
| 272 | for (ArrayList<BaseCluster> clusterList : semanticMap.values()) { |
| 273 | SemanticCluster semanticCluster = (SemanticCluster) clusterList.get(0); |
| 274 | |
| 275 | if (clusterList.size() > 1) { |
| 276 | newCluster.setCluster(clusterList.get(1)); |
| 277 | for (int i = 2; i < clusterList.size(); i++) { |
| 278 | newCluster.absorbCluster(clusterList.get(i)); |
| 279 | } |
| 280 | semanticCluster.absorbCluster(newCluster); |
| 281 | } else { |
| 282 | // cluster with no new candidate |
| 283 | Log.e(TAG, "semantic cluster with no new location clusters: " + |
| 284 | semanticCluster); |
| 285 | } |
| 286 | } |
| 287 | } |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 288 | saveSemanticClusters(); |
| 289 | } |
| 290 | |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 291 | private void loadSemanticClusters() { |
| 292 | List<Map<String, String> > allData = mStorage.getAllData(); |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 293 | HashMap<String, Long> histogram = new HashMap<String, Long>(); |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 294 | |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 295 | synchronized (mSemanticClusters) { |
| 296 | mSemanticClusters.clear(); |
| 297 | for (Map<String, String> map : allData) { |
| 298 | String semanticId = map.get(SEMANTIC_ID); |
| 299 | double longitude = Double.valueOf(map.get(SEMANTIC_LONGITUDE)); |
| 300 | double latitude = Double.valueOf(map.get(SEMANTIC_LATITUDE)); |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 301 | SemanticCluster cluster = |
| 302 | new SemanticCluster(semanticId, longitude, latitude); |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 303 | |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 304 | histogram.clear(); |
| 305 | for (int i = mFeatureValueStart; i <= mFeatureValueEnd; i++) { |
| 306 | String featureValue = SEMANTIC_COLUMNS[i]; |
| 307 | if (map.containsKey(featureValue)) { |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 308 | histogram.put(featureValue, Long.valueOf(map.get(featureValue))); |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 309 | } |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 310 | } |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 311 | cluster.setHistogram(histogram); |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 312 | mSemanticClusters.add(cluster); |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 313 | } |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 314 | mSemanticClusterCount = mSemanticClusters.size(); |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 315 | Log.e(TAG, "load " + mSemanticClusterCount + " semantic clusters."); |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 316 | } |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | private void saveSemanticClusters() { |
| 320 | HashMap<String, String> rowFeatures = new HashMap<String, String>(); |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 321 | |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 322 | mStorage.removeAllData(); |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 323 | synchronized (mSemanticClusters) { |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 324 | for (SemanticCluster cluster : mSemanticClusters) { |
| 325 | rowFeatures.clear(); |
| 326 | rowFeatures.put(SEMANTIC_ID, cluster.getSemanticId()); |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 327 | |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 328 | rowFeatures.put(SEMANTIC_LONGITUDE, |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 329 | String.valueOf(cluster.getCenterLongitude())); |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 330 | rowFeatures.put(SEMANTIC_LATITUDE, |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 331 | String.valueOf(cluster.getCenterLatitude())); |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 332 | |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 333 | HashMap<String, Long> histogram = cluster.getHistogram(); |
| 334 | for (Map.Entry<String, Long> entry : histogram.entrySet()) { |
| 335 | rowFeatures.put(entry.getKey(), String.valueOf(entry.getValue())); |
| 336 | } |
| 337 | mStorage.addData(rowFeatures); |
Ruei-sung Lin | 78a66d9 | 2012-08-29 10:30:03 -0700 | [diff] [blame^] | 338 | Log.e(TAG, "saving semantic cluster: " + rowFeatures); |
Ruei-sung Lin | 83954e8 | 2012-08-28 18:00:48 -0700 | [diff] [blame] | 339 | } |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 340 | } |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 343 | public String getSemanticLocation() { |
Ruei-sung Lin | c7c9cf1 | 2012-08-28 11:35:19 -0700 | [diff] [blame] | 344 | String label = LocationStatsAggregator.UNKNOWN_LOCATION; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 345 | |
Ruei-sung Lin | c7c9cf1 | 2012-08-28 11:35:19 -0700 | [diff] [blame] | 346 | // instead of using the last location, try acquiring the latest location. |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 347 | if (mLastLocation != null) { |
Ruei-sung Lin | 5d42ffa | 2012-08-23 16:01:57 -0700 | [diff] [blame] | 348 | // TODO: use fast neatest neighbor search speed up location search |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 349 | synchronized (mSemanticClusters) { |
| 350 | for (SemanticCluster cluster: mSemanticClusters) { |
| 351 | if (cluster.distanceToCenter(mLastLocation) < SEMANTIC_CLUSTER_RADIUS) { |
| 352 | return cluster.getSemanticId(); |
| 353 | } |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | } |
| 357 | return label; |
| 358 | } |
Wei Hua | 9c3a7dc | 2012-08-28 13:46:31 -0700 | [diff] [blame] | 359 | |
| 360 | public List<String> getClusterNames() { |
| 361 | ArrayList<String> clusters = new ArrayList<String>(); |
| 362 | synchronized (mSemanticClusters) { |
| 363 | for (SemanticCluster cluster: mSemanticClusters) { |
| 364 | clusters.add(cluster.getSemanticId()); |
| 365 | } |
| 366 | } |
| 367 | return clusters; |
| 368 | } |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame] | 369 | } |