예제 #1
0
    public void updateModelWeight(List<Sample> sampleList) {
      for (FeatureValue featureValue : featureMap.values()) {
        featureValue.setTempWeight(0);
      }

      for (Sample sample : sampleList) {
        Map<String, Double> probMap = predict(sample.getFeatures());
        for (String feature : sample.getFeatures()) {
          for (String label : labelSet) {
            this.setTempWeight(
                label,
                feature,
                this.getTempWeight(label, feature)
                    + probMap.get(label) * (1.0 / sampleList.size()));
          }
        }
      }

      // GIS;
      features.setLastWeight();
      for (FeatureValue featureValue : featureMap.values()) {
        featureValue.setModelWeight(
            featureValue.getModelWeight()
                + 1.0
                    / maxFeaturePerSample
                    * Math.log(featureValue.getEmpiricalWeight() / featureValue.getTempWeight()));
      }
    }
예제 #2
0
 // calculate p(y|x)=sum_i(alpha_i*f_i(x,y))/sum_y(sum_i(alpha_i*f_i(x,y))).
 public Map<String, Double> predict(List<String> x) {
   Map<String, Double> probMap = new HashMap<String, Double>();
   double sum = 0;
   for (String label : labelSet) {
     double prob = 0;
     for (String feature : x) {
       prob += features.getModelWeight(label, feature);
     }
     prob = Math.exp(prob);
     probMap.put(label, prob);
     sum += prob;
   }
   for (String label : probMap.keySet()) {
     probMap.put(label, probMap.get(label) / sum);
   }
   return probMap;
 }
예제 #3
0
 public boolean isConvergence(double threshold) {
   double sum = 0;
   for (FeatureValue featureValue : featureMap.values()) {
     sum +=
         (featureValue.getLastWeight() - featureValue.getModelWeight())
             * (featureValue.getLastWeight() - featureValue.getModelWeight());
   }
   return Math.sqrt(sum) <= threshold ? true : false;
 }
예제 #4
0
    public void addFeature(String label, String feature) {
      if (!featureMap.containsKey(new FeatureKey(label, feature))) {
        featureMap.put(new FeatureKey(label, feature), new FeatureValue());
      } else {
        this.updateCount(label, feature);
      }

      if (!featureCount.containsKey(feature)) {
        featureCount.put(feature, 1);
      } else {
        featureCount.put(feature, featureCount.get(feature) + 1);
      }
    }
예제 #5
0
  public Map generateMaps() {
    boolean generationSuccessful = false;
    while (generationSuccessful == false) {
      currentGeneration = new ArrayList<Map>();
      currentLowestFitness = null;
      secondBestFitness = null;
      for (int i = 0; i < 8; i++) {
        Map currMap = new Map();
        currMap.generateMap();
        currentGeneration.add(currMap);
      }
      setFitnesses();

      System.out.println(currentLowestFitness.getFitness());
      int xyz = 0;
      long time = System.currentTimeMillis();
      while (currentLowestFitness.getFitness() > 10 && xyz < 10000) {
        // System.out.println(currentGeneration.size());
        // größe größer 2
        int iterator = 0;
        int crossDivider = new Random().nextInt(currentLowestFitness.getCellList().size());
        Map childMap = new Map();
        ArrayList<Cell> childCellList = new ArrayList<Cell>();
        Random random = new Random();
        for (int i = 0; i < crossDivider; i++) {
          if (random.nextInt(100) >= 2) {
            childCellList.add(currentLowestFitness.getCellList().get(i));
          } else {
            childCellList.add(
                new Cell(
                    new Point(
                        random.nextInt(MAXIMUM_X_POSITION), random.nextInt(MAXIMUM_Y_POSITION)),
                    random.nextInt(Map.CELL_MAXIMUM_SIZE - 4) + 4));
          }
        }
        for (int i = crossDivider; i < secondBestFitness.getCellList().size(); i++) {
          if (random.nextInt(100) >= 2) {
            childCellList.add(secondBestFitness.getCellList().get(i));
          } else {
            childCellList.add(
                new Cell(
                    new Point(
                        random.nextInt(MAXIMUM_X_POSITION), random.nextInt(MAXIMUM_Y_POSITION)),
                    random.nextInt(Map.CELL_MAXIMUM_SIZE - 4) + 4));
          }
        }
        childMap.setCellList(childCellList);
        // System.out.printf("Elems: %s\n",
        // currentGeneration.keySet().stream().map(Ströing::valueOf).reduce((a,
        // b) -> String.format("%s %s", a, b)).get());
        // System.out.printf("Fitness: %f\n", childMap.getFitness());
        currentGeneration.remove(secondBestFitness);
        currentGeneration.add(childMap);
        setFitnesses();
        // System.out.println(currentLowestFitness.getFitness());
        xyz++;
      }
      System.out.println(
          "Number of Generations: " + xyz + " Time needed: " + (System.currentTimeMillis() - time));
      if (xyz < 10000) {
        generationSuccessful = true;
      }
    }
    return currentLowestFitness;
  }
예제 #6
0
 private void setFitnesses() {
   for (Map map : currentGeneration) {
     if (currentLowestFitness == null) {
       currentLowestFitness = map;
     } else if (secondBestFitness == null
         && map.getFitness() > currentLowestFitness.getFitness()) {
       secondBestFitness = map;
     } else if (secondBestFitness == null
         && map.getFitness() <= currentLowestFitness.getFitness()) {
       secondBestFitness = currentLowestFitness;
       currentLowestFitness = map;
     } else if (map.getFitness() < currentLowestFitness.getFitness()) {
       secondBestFitness = currentLowestFitness;
       currentLowestFitness = map;
     } else if (map.getFitness() > currentLowestFitness.getFitness()
         && map.getFitness() < secondBestFitness.getFitness()) {
       secondBestFitness = map;
     }
   }
 }
예제 #7
0
 public void setEmpiricalWeight(int sampleNumber) {
   for (FeatureValue featureValue : featureMap.values()) {
     featureValue.setEmpiricalWeight(1.0 * featureValue.getCount() / sampleNumber);
   }
 }
예제 #8
0
 public double getModelWeight(String label, String feature) {
   FeatureKey featureKey = new FeatureKey(label, feature);
   return featureMap.containsKey(featureKey)
       ? this.featureMap.get(featureKey).getModelWeight()
       : 0;
 }
예제 #9
0
 public void setTempWeight(String label, String feature, double weight) {
   FeatureKey featureKey = new FeatureKey(label, feature);
   if (featureMap.containsKey(featureKey)) {
     this.featureMap.get(featureKey).setTempWeight(weight);
   }
 }
예제 #10
0
 public void setLastWeight() {
   for (FeatureValue featureValue : featureMap.values()) {
     featureValue.setLastWeight();
   }
 }