Exemplo n.º 1
0
 public ParseMessage(String msg) {
   FMValue = msg.substring(3, 5); // first one is starting index,last one is end+1
   ToValue = msg.substring(9, 11); //
   NPVal = Character.getNumericValue(msg.charAt(15));
   HCVal = Character.getNumericValue(msg.charAt(20));
   msgNumber = Integer.parseInt(msg.substring(25, 28));
   int i = msg.indexOf("DATA");
   int j = msg.indexOf("VL");
   if (i != (j + 4)) {
     VLNode = msg.substring(j + 3, i);
   } else VLNode = "";
   DATA_Val = msg.substring(i + 5);
   if (NPVal == 2) {
     int k = msg.indexOf("registered as");
     RegisteredName = "" + msg.charAt(k + 14) + msg.charAt(k + 15);
   }
   if (NPVal == 6) {
     String str = new String(DATA_Val);
     arr = new CopyOnWriteArrayList<>();
     int m = 0;
     do {
       int k = str.indexOf(",");
       arr.add(new Node(str.substring(m, k)));
       str = str.substring(k + 1);
     } while (str.indexOf(",") != -1);
     arr.add(new Node(str));
   }
 }
Exemplo n.º 2
0
  /** function to evaluate tree */
  public double evaluate(Node ptr) {
    if (ptr.leftChild == null && ptr.rightChild == null) return Character.getNumericValue(ptr.data);
    else {
      double result = 0.0;
      double left = evaluate(ptr.leftChild);
      double right = evaluate(ptr.rightChild);
      char operator = ptr.data;

      switch (operator) {
        case '+':
          result = left + right;
          break;
        case '-':
          result = left - right;
          break;
        case '*':
          result = left * right;
          break;
        case '/':
          result = left / right;
          break;
        default:
          result = left + right;
          break;
      }
      return result;
    }
  }
Exemplo n.º 3
0
  public static void main(String args[]) throws Exception {
    try {
      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
      String n = (input.readLine());
      int number[] = new int[n.length()];
      for (int i = 0; i < n.length(); i++) {
        number[i] = Character.getNumericValue(n.charAt(i));
      }
      System.out.println(Arrays.toString(plus(number)));
    } catch (Exception e) {

    }
  }
Exemplo n.º 4
0
  /**
   * sets an appropiate response MTI.
   *
   * <p>i.e. 0100 becomes 0110<br>
   * i.e. 0201 becomes 0210<br>
   * i.e. 1201 becomes 1210<br>
   *
   * @exception ISOException on MTI not set or it is not a request
   */
  public void setResponseMTI() throws ISOException {
    if (!isRequest()) throw new ISOException("not a request - can't set response MTI");

    String mti = getMTI();
    char c1 = mti.charAt(3);
    char c2 = '0';
    switch (c1) {
      case '0':
      case '1':
        c2 = '0';
        break;
      case '2':
      case '3':
        c2 = '2';
        break;
      case '4':
      case '5':
        c2 = '4';
        break;
    }
    set(
        new ISOField(
            0, mti.substring(0, 2) + (Character.getNumericValue(getMTI().charAt(2)) + 1) + c2));
  }
Exemplo n.º 5
0
 /**
  * @return true if message "seems to be" a request
  * @exception ISOException on MTI not set
  */
 public boolean isRequest() throws ISOException {
   return Character.getNumericValue(getMTI().charAt(2)) % 2 == 0;
 }
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in);
    File file = new File("/users/Sriram/Desktop/2014-15/Neural Nets/weightsDiffAct.txt");
    if (!file.exists()) file.createNewFile();
    Scanner scan = new Scanner(file);
    Scanner scanIn =
        new Scanner(new File("/users/Sriram/Desktop/2014-15/Neural Nets/StarterInput.txt"));
    System.out.println("Type train or run");
    String t = in.next();
    double[][] iWeights1 = new double[Hidden][Input];
    double[][] hWeights1 = new double[Output][Hidden];
    if (t.equals("train")) {
      BufferedWriter bout = new BufferedWriter(new FileWriter(file));
      ReverseNetDiffAct autobot = new ReverseNetDiffAct(iWeights1, hWeights1);
      autobot.initializeWeights();

      // taking in the inputs
      cases = 4;
      double[][] ktrainingInputs1 = new double[cases][Input];
      // scans files and takes in the inputs, scans line and then by character.
      for (int n = 0; n < cases; n++) {
        for (int k = 0; k < Math.sqrt(Input); k++) {
          String line = scanIn.nextLine();
          for (int b = 0; b < Math.sqrt(Input); b++) {
            ktrainingInputs1[n][k * (int) (Math.sqrt(Input)) + b] =
                Character.getNumericValue(line.charAt(b));
            //
            // System.out.println(ktrainingInputs1[n][k*9+b]);
          }
        }
      }
      autobot.setInput(ktrainingInputs1);
      autobot.initOut(cases);
      autobot.initHid(cases);
      autobot.setOut();
      // setting ideal output and initializing real output
      // itargetOutput[0][k]

      autobot.changeWeights();
      iWeights1 = autobot.getIWeights();
      hWeights1 = autobot.getHWeights();
      for (int j = 0; j < Hidden; j++) {
        for (int k = 0; k < Input; k++) {
          bout.write(iWeights1[j][k] + "\n");
        }
      }

      for (int i = 0; i < Output; i++) {
        for (int j = 0; j < Hidden; j++) {
          bout.write(hWeights1[i][j] + "\n");
        }
      }
      bout.close();
      System.out.println("Done");
    } else if (t.equals("run")) {
      // getting the weights from the optimized weights file

      for (int j = 0; j < Hidden; j++) {
        for (int k = 0; k < Input; k++) {
          iWeights1[j][k] = scan.nextDouble();
        }
      }

      for (int i = 0; i < Output; i++) {
        for (int j = 0; j < Hidden; j++) {
          hWeights1[i][j] = scan.nextDouble();
        }
      }

      // getting in all the inputs that will be propogated to check output
      cases = 4;
      ReverseNetDiffAct autobot = new ReverseNetDiffAct(iWeights1, hWeights1);
      // need to set cases first, because of constructor
      double[][] ktrainingInputs1 = new double[cases][Input];
      autobot.initOut(cases);
      autobot.initHid(cases);
      for (int n = 0; n < cases; n++) {
        for (int k = 0; k < Math.sqrt(Input); k++) {
          String line = scanIn.nextLine();
          for (int b = 0; b < Math.sqrt(Input); b++) {
            ktrainingInputs1[n][k * (int) (Math.sqrt(Input)) + b] =
                Character.getNumericValue(line.charAt(b));
            // System.out.println(ktrainingInputs1[n][k*9+b]);
          }
        }
      }
      autobot.setInput(ktrainingInputs1);
      // setting ideal output

      autobot.propagateNet(cases); // with 5 cases
      double[][] irealOutput1 = autobot.getOutput();
      for (int n = 0; n < cases; n++) {
        for (int i = 0; i < Output; i++) {
          System.out.println("Case " + n + " Output " + i + ": " + irealOutput1[n][i]);
        }
      }
      System.out.println("Done");
    }
  }
Exemplo n.º 7
0
  // given the file type argument, this method reads the file into a vector
  public static int readFileIntoInputVector(String inputRepresentation) {
    int target = 0; // target integer
    String digitAsString = "";

    try {
      if (inputRepresentation.equalsIgnoreCase("bmp")) {
        String currentLine = "CurrentLine";
        if (firstRead) {
          fileReader.readLine(); // skip first few lines, as they are not relevant
          fileReader.readLine();
          currentLine = fileReader.readLine();
          firstRead = false; // only skip lines on initial entry into the file
        }
        if (currentLine == null) return -2; // ensures the file is being read
        for (int i = 0; i < 32; i++) { // for each 32 rows
          currentLine = fileReader.readLine();
          if (currentLine == null) return -2;
          currentLine = currentLine.trim(); // removes whitespace
          char[] inputSplitter =
              currentLine.toCharArray(); // separates each digit into its own array element

          for (int j = 0; j < inputSplitter.length; j++) {
            inputs.addElement(
                new Double(Character.getNumericValue(inputSplitter[j]))); // add the line to inputs
          }
        }
        currentLine = fileReader.readLine();
        target = Character.getNumericValue(currentLine.charAt(1)); // get solution into target
      } else if (inputRepresentation.equalsIgnoreCase("ndsi")) {
        String currentLine = fileReader.readLine();
        if (currentLine == null) return -2; // ensures file is being read
        currentLine =
            currentLine.substring(
                2, currentLine.length() - 2); // leaves out parentheses and whitespace
        String[] inputSplitter =
            currentLine.split(
                " "); // fills array with actual values from line (they are delimited by whitespace
        // initially)
        for (int i = 0; i < inputSplitter.length; i++) {
          inputs.addElement(
              new Double(Double.parseDouble(inputSplitter[i]))); // add the line to inputs
        }

        currentLine = fileReader.readLine();
        currentLine =
            currentLine.substring(
                2, currentLine.length() - 2); // leaves out parentheses and whitespace
        String[] outputSplitter =
            currentLine.split(
                " "); //// fills array with actual values from line (they are delimited by
        // whitespace initially)
        for (int i = 0; i < outputSplitter.length; i++) {
          if (outputSplitter[i].equals("1.0"))
            target = i; // sets target to solution value for this problem
        }
      } else if (inputRepresentation.equalsIgnoreCase("dsi")) {
        String currentLine = fileReader.readLine();
        if (currentLine == null) return -2; // ensures file is being read
        String[] inputSplitter =
            currentLine.split(
                ","); // file has values delimited by commas, so fill array with actual values
        for (int i = 0; i < inputSplitter.length - 1; i++) {
          inputs.addElement(
              new Double(
                  Double.parseDouble(inputSplitter[i])
                      / 16)); // effectively converts the file into an ndsi
        }
        target = Integer.parseInt(inputSplitter[inputSplitter.length - 1]);
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
    return target;
  }
Exemplo n.º 8
0
  public static void main(String args[]) {
    int PC, SP, IR, AC, X, Y;
    int[] memory = new int[1000];
    SP = IR = AC = X = Y = 0;
    PC = -1;

    // Gets "MainMemory" class from working directory.
    String directory = System.getProperty("user.dir") + "\\bin\\ MainMemory";

    Scanner in = new Scanner(System.in);

    try {
      int x, y, index = 0;
      String z = "";
      Runtime rt = Runtime.getRuntime();

      Process proc = rt.exec("java -cp " + directory);
      // Process proc = rt.exec("cat Hello.java");

      InputStream os = proc.getInputStream();

      IR = 0;

      // GRABS INSTRUCTION OUT OF STREAM
      while ((x = os.read()) != -1) {

        try {
          if (x > 47 && x < 58) {
            y = Character.getNumericValue(x);

            z += y;

          } else if (x == 10 && z != "") {

            x = Integer.parseInt(z);
            y = 0;
            z = "";
            // System.out.println(x);

            memory[index] = x;
            index++;
          } else if (x == 13) {
            break;
          }
        } catch (Exception e) {
          System.out.println("ERROR: Error while reading instructions.");
          System.exit(1);
        }
      }

      // PERFORMS INSTRUCTIONS
      for (int k = 0; k < index; k++) {

        // System.out.println(k + " == " + memory[k]);
        IR = memory[k];

        switch (IR) {
          case 1:
            // System.out.println("LOAD VALUE"); //READS IN THE VALUE
            PC++;
            k++;
            AC = memory[k];
            PC++;
            break;

          case 2:
            // System.out.println("LOAD ADDR"); //READS VALUE FROM AN ADDRESS
            PC++;

            AC = memory[memory[k + 1]];

            k++;

            break;

          case 3:
            // System.out.println("STORE ADDR"); //WRITES THE VALUE OF AC TO THE ADDRESS
            PC++;

            memory[memory[k + 1]] = AC;

            k++;

            try {
              File file = new File(System.getProperty("user.dir") + "\\src\\program.txt");
              FileWriter fw = new FileWriter(file);
              BufferedWriter out = new BufferedWriter(fw);

              for (int i = 0; i < index; i++) {
                out.write("" + memory[i]);
                out.newLine();
              }
              out.flush();
              out.close();
            } catch (IOException e) {
              System.out.println("ERROR: Could not rewrite program");
            }

            break;

          case 4:
            // System.out.println("ADD X"); //ADDS X TO AC
            PC++;
            AC += X;
            break;

          case 5:
            // System.out.println("ADD Y"); //ADDS Y TO AC
            PC++;
            AC += Y;
            break;

          case 6:
            // System.out.println("SUB X"); //SUBSTRACTS X FROM AC
            PC++;
            AC -= X;
            break;

          case 7:
            // System.out.println("SUB Y"); //SUBTRACTS Y FROM AC
            PC++;
            AC -= Y;
            break;

          case 8:
            // System.out.println("GET PORT"); //RECEIVES EITHER INTEGER OR CHARACTER DEPENDING ON
            // PORT NUMBER, 1 OR 2
            PC++;
            k++;
            if (memory[k] == 1) {
              PC++;
              // System.out.println("PORT = READ INT"); //RECEIVES INTEGER FROM USER AND PLACES IT
              // INTO AC
              AC = in.nextInt();
            } else if (memory[k] == 2) // RECEIVES CHARACTER FROM USER AND PLACES IT INTO AC
            {
              PC++;
              // System.out.println("PORT = READ CHAR");
              AC = (int) in.next(".{1}").charAt(0);
            }

            break;

          case 9:
            // System.out.println("PUT PORT"); //PRINTS EITHER INTEGER OR CHARACTER DEPENDING ON
            // PORT NUMBER, 1 OR 2
            PC++;
            k++;
            if (memory[k] == 1) {
              // System.out.println("PORT = WRITE INT"); PRINTS OUT INTEGER VALUE FROM AC
              PC++;
              System.out.print(AC);

            } else if (memory[k] == 2) {
              // System.out.println("PORT = WRITE CHAR"); //PRINTS OUT CHARACTER VALUE FROM AC
              PC++;

              System.out.print((char) AC);
            }

            break;

          case 10:
            // System.out.println("COPYTO X"); //COPIES VALUE FROM X INTO AC
            PC++;
            X = AC;
            break;

          case 11:
            // System.out.println("COPYTO Y"); //COPIES VALUE FROM Y INTO AC
            PC++;
            Y = AC;
            break;

          case 12:
            // System.out.println("COPYFROM X"); //COPIES VALUE FROM X INTO AC
            PC++;
            AC = X;
            break;

          case 13:
            // System.out.println("COPYFROM Y"); //COPIES VALUE FROM Y INTO AC
            PC++;
            AC = Y;
            break;

          case 14:
            // System.out.println("JUMP ADDR"); //JUMPS TO AN ADDRESS
            PC++;

            k = memory[k + 1] - 1;

            break;

          case 15:
            // System.out.println("JUMPIFEQUAL ADDR"); //JUMPS TO AN ADDRESS IF AC IS EQUAL TO ZERO
            PC++;

            if (AC == 0) {
              PC++;
              k = memory[k + 1] - 1;
            } else {
              PC++;
              k++;
            }

            break;

          case 16:
            // System.out.println("JUMPIFNOTEQUAL ADDR"); //JUMPS TO AN ADDRESS IF AC IS NOT EQUAL
            // TO ZERO
            PC++;

            if (AC != 0) {
              PC++;
              k = memory[k + 1] - 1;

            } else {
              PC++;
              k++;
            }

            break;

          case 17:
            // System.out.println("CALL ADDR"); // PUSH RETURN ADDRESS INTO STACK, AND JUMPS AN
            // ADDRESS
            PC++;

            SP = PC;

            k = memory[k + 1] - 1;

            PC++;

            break;

          case 18:
            // System.out.println("RETURN"); //POPS RETURN ADDRESS FROM STACK, AND JUMPS TO IT
            PC++;

            k = SP + 1;
            break;

          case 19:
            // System.out.println("INC X"); //INCREMENTS VALUE IN X BY ONE
            PC++;
            X++;
            break;

          case 20:
            // System.out.println("DEC X"); //DECREMENTS VALUE IN X BY ONE
            PC++;
            X--;
            break;

          case 30:
            // System.out.println("END"); //ENDS PROCESS
            PC++;
            X = 0;
            Y = 0;
            AC = 0;

            k = index;

            try {
              os.close();

            } catch (Exception e) {

            }
            break;
        }
        // System.out.println("\nProgram Counter: " + PC);
      }

      // PROCESS ENDS

      proc.waitFor();

      in.close();

      int exitVal = proc.exitValue();

      System.out.println("\nProcess exited: " + exitVal);

    } catch (Throwable t) {

      t.printStackTrace();
    }

    System.exit(1);
  }