Пример #1
0
  /* (non-Javadoc)
   * @see Environment.Environment#getPercept()
   */
  @Override
  public Percept getPercept() throws Exception {

    if (_bTrialOver) {
      throw new Exception("Trial over");
    }

    WumpusPercept p = new WumpusPercept();

    if (_tsWumpusMap.contains(currentLoc) || _tsPitMap.contains(currentLoc)) {
      p.setDead();
      _bAlive = false;
      _bTrialOver = true;
      return p;
    }

    Pair<Integer, Integer> left = leftGuy(currentLoc);
    Pair<Integer, Integer> right = rightGuy(currentLoc);
    Pair<Integer, Integer> top = topGuy(currentLoc);
    Pair<Integer, Integer> bottom = bottomGuy(currentLoc);

    if (_tsPitMap.contains(left)
        || _tsPitMap.contains(right)
        || _tsPitMap.contains(top)
        || _tsPitMap.contains(bottom)) {
      p.setBreezy();
    }

    if (_tsWumpusMap.contains(left)
        || _tsWumpusMap.contains(right)
        || _tsWumpusMap.contains(top)
        || _tsWumpusMap.contains(bottom)) {
      p.setSmelly();
    }

    if (_tsGoldMap.contains(currentLoc)) {
      p.setGlitter();
    }

    if (_bWumpusDied) {
      p.setScream();
      _bWumpusDied = false;
    }

    p.setLoc(currentLoc);

    return p;
  }
Пример #2
0
  public static void main(String[] args) {
    final int n = 4; // world is size nxn

    String simple_results = new String();
    String hybrid_results = new String();
    String risky_results = new String();

    for (int i = 1; i <= 40; i++) {
      System.out.println("World " + i + " being solved");

      WumpusSimpleAgent wa = new WumpusSimpleAgent(); // random movements
      WumpusHybridAgent wha = new WumpusHybridAgent(n, false);
      WumpusHybridAgent wha_risky = new WumpusHybridAgent(n, true);
      WumpusEnvironment we1 = WumpusEnvironment.getNewWumpusEnvironment(n, 1, 1, 1, 0.2, i);
      WumpusEnvironment we2 = WumpusEnvironment.getNewWumpusEnvironment(n, 1, 1, 1, 0.2, i);
      WumpusEnvironment we3 = WumpusEnvironment.getNewWumpusEnvironment(n, 1, 1, 1, 0.2, i);

      // random agent, we1
      while (true) {
        try {
          // System.out.println(we1.show());
          WumpusPercept p = (WumpusPercept) we1.getPercept();
          // System.out.println(p);
          if (p.isbDead()) {
            simple_results += we1.show();
            break;
          }
          Action act = wa.getAction(p);
          we1.takeAction((Action) act);

        } catch (Exception e) {
          System.out.println(e.getMessage());
          e.printStackTrace();
          // if trial is over, exception here
          simple_results += we1.show();
          break;
        }
      }

      // hybrid agent, we2
      while (true) {
        try {
          // System.out.println(we1.show());
          WumpusPercept p = (WumpusPercept) we2.getPercept();
          // System.out.println(p);
          if (p.isbDead()) {
            hybrid_results += we2.show();
            break;
          }
          Action act = wha.getAction(p);
          we2.takeAction((Action) act);

        } catch (Exception e) {
          System.out.println(e.getMessage());
          e.printStackTrace();
          // if trial is over, exception here
          hybrid_results += we2.show();
          break;
        }
      }

      // risky agent, we3
      while (true) {
        try {
          // System.out.println(we1.show());
          WumpusPercept p = (WumpusPercept) we3.getPercept();
          // System.out.println(p);
          if (p.isbDead()) {
            risky_results += we3.show();
            break;
          }
          Action act = wha_risky.getAction(p);
          we3.takeAction((Action) act);

        } catch (Exception e) {
          System.out.println(e.getMessage());
          e.printStackTrace();
          // if trial is over, exception here
          risky_results += we3.show();
          break;
        }
      }
      simple_results += '\n';
      hybrid_results += '\n';
      risky_results += '\n';
    }

    try {
      // write each string to a separate output file
      FileWriter simple_file = new FileWriter("SimpleResults.txt");
      FileWriter hybrid_file = new FileWriter("HybridResults.txt");
      FileWriter risky_file = new FileWriter("RiskyResults.txt");

      BufferedWriter out1 = new BufferedWriter(simple_file);
      out1.write(simple_results);
      out1.close();

      BufferedWriter out2 = new BufferedWriter(hybrid_file);
      out2.write(hybrid_results);
      out2.close();

      BufferedWriter out3 = new BufferedWriter(risky_file);
      out3.write(risky_results);
      out3.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }