Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Aggregator.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.common.aggregator;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. /**
  26. * Aggregates a stream of classifications and performs averaging in order to
  27. * smooth out results. Each classification supplied to the aggregator is used
  28. * to alter the 'probabilities' of that classification holding. These
  29. * probabilities are calculated hierarchically, and the aggregator can in some
  30. * cases produce a result that is not in itself a normal activity because of
  31. * this (for example, it may classify unclear data as
  32. * <code>CLASSIFIED/VEHICLE</code>, rather than as a sub-type as expected).
  33. *
  34. * @author chris
  35. */
  36. public class Aggregator {
  37. static final double DELTA = 0.25;
  38. static final double THRESHOLD = 0.5;
  39. private final HashMap<String, HashMap<String, Double>> scores
  40. = new HashMap<String, HashMap<String, Double>>() {{
  41. put("", new HashMap<String, Double>() {{
  42. put("null", 0.5d);
  43. put("CLASSIFIED", 0.5d);
  44. }});
  45. put("CLASSIFIED", new HashMap<String, Double>() {{
  46. put("null", 0.2d);
  47. put("DANCING", 0.2d);
  48. put("WALKING", 0.2d);
  49. put("VEHICLE", 0.2d);
  50. put("IDLE", 0.2d);
  51. }});
  52. put("CLASSIFIED/WALKING", new HashMap<String, Double>() {{
  53. put("null", 0.5d);
  54. put("STAIRS", 0.5d);
  55. }});
  56. put("CLASSIFIED/VEHICLE", new HashMap<String, Double>() {{
  57. put("null", 0.333d);
  58. put("CAR", 0.333d);
  59. put("BUS", 0.333d);
  60. }});
  61. put("CLASSIFIED/IDLE", new HashMap<String, Double>() {{
  62. put("null", 0.333d);
  63. put("STANDING", 0.333d);
  64. put("SITTING", 0.333d);
  65. }});
  66. put("CLASSIFIED/WALKING/STAIRS", new HashMap<String, Double>() {{
  67. put("null", 0.333d);
  68. put("UP", 0.333d);
  69. put("DOWN", 0.333d);
  70. }});
  71. }};
  72. public void addClassification(final String classification) {
  73. String path = "";
  74. for (String part : classification.split("/")) {
  75. if (!scores.containsKey(path)) {
  76. throw new RuntimeException("Path not found: " + path
  77. + " (classification: " + classification + ")");
  78. }
  79. updateScores(scores.get(path), part);
  80. path = path + (path.length() == 0 ? "" : "/") + part;
  81. }
  82. if (scores.containsKey(path)) {
  83. // This classification has children which we're not using
  84. // e.g. we've received CLASSIFIED/WALKING, but we're not walking
  85. // up or down stairs
  86. updateScores(scores.get(path), "null");
  87. }
  88. }
  89. void updateScores(final Map<String, Double> map, final String target) {
  90. for (Map.Entry<String, Double> entry : map.entrySet()) {
  91. //Log.d(getClass().getName(), "Score for " + entry.getKey() + " was: " + entry.getValue());
  92. entry.setValue(entry.getValue() * (1 - DELTA));
  93. if (entry.getKey().equals(target)) {
  94. entry.setValue(entry.getValue() + DELTA);
  95. }
  96. //Log.d(getClass().getName(), "Score for " + entry.getKey() + " is now: " + entry.getValue());
  97. }
  98. }
  99. public String getClassification() {
  100. String path = "";
  101. do {
  102. final Map<String, Double> map = scores.get(path);
  103. double best = THRESHOLD;
  104. String bestPath = "null";
  105. for (Map.Entry<String, Double> entry : map.entrySet()) {
  106. if (entry.getValue() >= best) {
  107. best = entry.getValue();
  108. bestPath = entry.getKey();
  109. }
  110. }
  111. path = path + (path.length() == 0 ? "" : "/") + bestPath;
  112. } while (scores.containsKey(path));
  113. return path.replaceAll("(^CLASSIFIED)?/?null$", "");
  114. }
  115. }