Esempio n. 1
0
  public static WumpusEnvironment getNewWumpusEnvironment(
      final int n,
      final int nWumpus,
      final int nGold,
      final int nArrows,
      double pitProb,
      long seed) {
    WumpusEnvironment we = new WumpusEnvironment();

    we._rand = new Random(seed);
    we._nSize = n;
    we._bAlive = true;
    we._tsGoldMap = new TreeSet<Pair<Integer, Integer>>();
    we._tsPitMap = new TreeSet<Pair<Integer, Integer>>();
    we._tsWumpusMap = new TreeSet<Pair<Integer, Integer>>();
    we.currentLoc = new Pair<Integer, Integer>(0, 0);
    we._nArrow = nArrows;
    we._bWumpusDied = false;
    we._bTrialOver = false;
    we._nHasGold = 0;
    we._nTime = 0;
    we._nFacing = DIRECTION.DIRUP;

    while (true) {
      int x = we._rand.nextInt(n);
      int y = we._rand.nextInt(n);
      if (x == 0 && y == 0) {
        continue;
      }

      we._tsGoldMap.add(new Pair<Integer, Integer>(x, y));
      if (we._tsGoldMap.size() == nGold) {
        break;
      }
    }

    while (true) {
      int x = we._rand.nextInt(n);

      int y = we._rand.nextInt(n);
      if (x == 0 && y == 0) {
        continue;
      }

      we._tsWumpusMap.add(new Pair<Integer, Integer>(x, y));
      if (we._tsWumpusMap.size() == nWumpus) {
        break;
      }
    }

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i == 0 && j == 0) {
          continue;
        }
        double d = we._rand.nextDouble();
        if (d <= pitProb) {
          we._tsPitMap.add(new Pair<Integer, Integer>(i, j));
        }
      }
    }

    return we;
  }
Esempio n. 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();
    }
  }