예제 #1
0
  public Map<Match, Map<String, Map<Object, Double>>> predictMatches(Match[] matches)
      throws Exception {
    Map<Match, Map<String, Map<Object, Double>>> retval =
        new TreeMap<Match, Map<String, Map<Object, Double>>>();
    LinkedList<Match> nMatches = new LinkedList<Match>();
    for (Match match : matches) {
      if (teams.get(match.country, match.home) != null
          && teams.get(match.country, match.away) != null) nMatches.add(match);
    }
    matches = new Match[nMatches.size()];
    int i = 0;
    for (Match match : nMatches) {
      matches[i] = match;
      i++;
    }
    for (Match match : matches) {
      try {
        Map<String, Map<Object, Double>> toPut = new TreeMap<String, Map<Object, Double>>();
        // Match to predict
        double[] toPredict = this.getKamp(match.home, match.away, match.country);

        // Dataset to use
        List<DataEntry> trainingset = this.getTrainingSet(match.country, match.division);
        if (Setup.NORMALIZE_DATA) {
          Normalizer norm = this.normalizers.get(this.getDataKey(match.country, match.division));
          norm.apply(toPredict);
        }
        // The classification
        // Find number of neighbours
        int neighbours = 10; // Default
        try {
          neighbours = getBestKForNearestNeighbour(match.country, match.division);
        } catch (SQLException e1) {
          Log.e(e1);
        }
        // Find kernel
        Estimator est = null;
        try {
          est = getBestKernelForNaiveBayes(match.country, match.division);
        } catch (SQLException e1) {
          Log.e(e1);
        }

        toPut.put("lda", this.predictMatchLDA(trainingset, toPredict));
        toPut.put("nb", this.predictMatchNB(trainingset, toPredict, est));
        toPut.put("nn", this.predictMatchNN(trainingset, toPredict, neighbours));
        retval.put(match, toPut);
      } catch (Exception e) {
        Log.e(e);
      }
    }
    return retval;
  }
예제 #2
0
  public synchronized void load() throws SQLException {
    // And calculate
    Connection connection = getDb();
    Statement stmt = connection.createStatement();
    ResultSet scoreSet = stmt.executeQuery("SELECT hold,division,land from klubber");
    while (scoreSet.next()) {
      // Fetch info
      String land = scoreSet.getString("land");
      String hold = scoreSet.getString("hold");
      int division = scoreSet.getInt("division");
      double rating = Setup.pointStart / (Math.pow(Setup.divisionsforskel, division));
      Team team = new Team(hold, land, division, rating, Setup.matchesToStore);
      teams.addTeam(team);
    }
    stmt.close();
    // Load the matches
    stmt = connection.createStatement();
    String matchesSQL = "SELECT * FROM kampe order by dato asc";
    ResultSet r = stmt.executeQuery(matchesSQL);
    while (r.next()) {
      try {
        // Fetch info
        String country = r.getString("land");
        Team away = teams.get(country, r.getString("udehold"));
        int division = r.getInt("division");
        int goalsAway = r.getInt("udemaal");
        int goalsHome = r.getInt("hjemmemaal");
        String seasonStartYear = r.getString("season_start_year");
        Team home = teams.get(country, r.getString("hjemmehold"));
        int round = r.getInt("runde");
        Kamp match = new Kamp(home, away, goalsHome, goalsAway, round, seasonStartYear);
        // Find result
        Udfald y = null;
        if (goalsHome > goalsAway) y = Udfald.HJEMME;
        else if (goalsHome == goalsAway) y = Udfald.UAFGJORT;
        else y = Udfald.UDE;
        double[] features = getKamp(home, away, country);
        DataEntry matchInfo = new DataEntry(features, y);
        matchInfo.addExtra("season_start_year", seasonStartYear);
        String key = getDataKey(country, division + "");
        synchronized (this) {
          LinkedList<DataEntry> matchData = dataset.get(key);
          if (matchData == null) {
            matchData = new LinkedList<DataEntry>();
            dataset.put(key, matchData);
          }
          // If enough matches and so on, update
          if (home.totalplayed >= Setup.TRAINING_ROUNDS
              && away.totalplayed >= Setup.TRAINING_ROUNDS // Total played at least
              && round >= Setup.antalRunder // Total played in start of each season
          ) {
            matchData.add(matchInfo);
          }
        }

        // And update a lot of information
        // Skal altid ske. Til træning af klubbernes point
        teams.opdaterPoint(home, away, goalsHome, goalsAway, division);

        // The rest
        home.addLatestHomeMatch(match);
        home.addLatestMatch(match);
        away.addLatestAwayMatch(match);
        away.addLatestMatch(match);
        home.addInternal(match);
        away.addInternal(match);
        home.totalplayed++;
        away.totalplayed++;
      } catch (Exception e) {
        Log.e(e);
      }
    }
    r.close();
    stmt.close();

    // Filter out noise if needed
    if (Setup.FILTER_DATA) {
      for (String key : dataset.keySet()) {
        LinkedList<DataEntry> matches = dataset.get(key);
        Setup.FILTER_HANDLER.applyFilters(matches);
      }
    }
    // Prepare normalizer
    for (String key : dataset.keySet()) {
      Normalizer normalizer = new Normalizer(dataset.get(key), Normalizer.Method.NORMALIZATION);
      normalizers.put(key, normalizer);
      if (Setup.NORMALIZE_DATA) normalizer.apply(dataset.get(key));
    }
  }