Esempio n. 1
0
  private void checkSPOTIsInstalled() throws InPUTException {

    REXP spotInstalled = runCommand("is.element('SPOT', installed.packages()[,1])", true);
    if (!spotInstalled.asBool().isTRUE())
      throw new InPUTException(
          "In order to use InPUT tuning extension, you have to install the SPOT package for R: \"install.packages('SPOT')\" in the R console.");
  }
Esempio n. 2
0
  /**
   * Method checks if Column is a existing column in R.
   *
   * @param col the column to check
   * @return true if column exists else false
   */
  protected Boolean checkRColumn(String col) {
    REXP rCols = eval("colnames(" + RVAR + ")", false);
    String[] cols = rCols.asStringArray();
    List<String> collist = Arrays.asList(cols);

    if (collist.contains(col)) {
      return true;
    }
    return false;
  }
Esempio n. 3
0
  /**
   * Sends a single command to R.
   *
   * @param sz The R command
   * @param szDescription The Description showing up before the output
   */
  private String[] sendCommand2R(String sz) {
    // set command
    if (sz.length() == 0) {
      return null;
    }

    // parse
    long lParsed = re.rniParse(sz, 1);

    // eval
    long lEvaluated = re.rniEval(lParsed, 0);

    // generate REXP
    if (lEvaluated > 0) {
      REXP exp = new REXP(re, lEvaluated);

      String[] szArr = exp.asStringArray();
      if (szArr != null) {
        return szArr;
      }

      double[] dArr = exp.asDoubleArray();
      if (dArr != null) {
        String[] szDArr = new String[dArr.length];
        for (int cnt = 0; cnt < dArr.length; ++cnt) {
          szDArr[cnt] = String.valueOf(dArr[cnt]);
        }
        return szDArr;
      }

      int[] iArr = exp.asIntArray();
      if (iArr != null) {
        String[] szIArr = new String[iArr.length];
        for (int cnt = 0; cnt < iArr.length; ++cnt) {
          szIArr[cnt] = String.valueOf(iArr[cnt]);
        }
        return szIArr;
      }
    }
    return null;
  }
  /**
   * Method for fitting a Baysian Network.
   *
   * @param network a Baysian Network to fit
   * @param fitted a REXP fitted object
   */
  @SuppressWarnings({"unused", "rawtypes"})
  public static void fit(Network network, REXP fitted) {
    if (fitted == null) {
      return;
    }
    network.setFitted();
    ArrayList<Node> nodes;
    if (network != null) {
      nodes = (ArrayList<Node>) network.getNodes();
    } else {
      nodes = new ArrayList<Node>();
      Vector size = fitted.asVector().getNames();
      //// add code
    }

    // Step 5 add cpts to nodes
    for (int i = 0; i < nodes.size(); i++) {
      Node node = nodes.get(i); // the node to edit
      double[] da;
      da = fitted.asVector().at(nodes.get(i).getNodeName()).asVector().at("prob").asDoubleArray();
      node.setCpt(da);
    }
  }
Esempio n. 5
0
 @Override
 public double[] asDoubles() {
   return r.asDoubleArray();
 }
Esempio n. 6
0
 @Override
 public String[] asStrings() {
   return r.asStringArray();
 }
Esempio n. 7
0
 @Override
 public int[] asIntegers() {
   return r.asIntArray();
 }
Esempio n. 8
0
 @Override
 public int asInteger() {
   return r.asInt();
 }
Esempio n. 9
0
 @Override
 public double[][] asDoubleMatrix() {
   return r.asDoubleMatrix();
 }
Esempio n. 10
0
 @Override
 public double asDouble() {
   return r.asDouble();
 }
Esempio n. 11
0
  /**
   * Method for converting a REXP object into a BN Network.
   *
   * @param data the REXP object containing the dataset.
   * @param network the REXP object to convert
   * @return a BNNetwork object
   */
  public static Network toBnNetwork(REXP data, REXP network) {

    List<Node> nodelist = new ArrayList<Node>(); // List of Nodes
    // Error Check
    if (data == null || network == null) {
      return new Network("null", nodelist);
    }

    // Temporary Hashmap for storing nodes
    HashMap<String, Node> nodemap = new HashMap<String, Node>();
    REXP rexp1 = network.asVector().at("nodes"); // Nodes
    @SuppressWarnings("unchecked") // Suppressing Type check
    Vector<String> nodeNames = rexp1.asVector().getNames(); // Node Names

    // Step 1 create nodes and put in HashMap
    for (int i = 0; i < nodeNames.size(); i++) {
      Node node = new Node();
      String nodeName = nodeNames.get(i);
      node.setNodeName(nodeName);
      nodemap.put(nodeName, node); // add node to HashMap
      nodelist.add(node); // add node to List
    }

    // Step 2 add parents to nodes
    for (int i = 0; i < nodeNames.size(); i++) {
      Node node = nodemap.get(nodeNames.get(i)); // node to edit
      String[] parents = rexp1.asVector().at(i).asVector().at("parents").asStringArray();
      // comment to previous line: array of parentnames
      for (int j = 0; j < parents.length; j++) {
        Node pnode = nodemap.get(parents[j]); // parent node
        node.addParentnode(pnode); // add parent node to node
      }
    }

    // Step 3 add children to nodes
    for (int i = 0; i < nodeNames.size(); i++) {
      Node node = nodemap.get(nodeNames.get(i)); // node to edit
      String[] children = rexp1.asVector().at(i).asVector().at("children").asStringArray();
      // comment to previous line: array of childrennames
      for (int j = 0; j < children.length; j++) {
        Node cnode = nodemap.get(children[j]); // child node
        node.addChildnode(cnode); // add child node to node
      }
    }

    // Step 4  get levels
    for (int i = 0; i < nodeNames.size(); i++) {
      String rfactor = data.asVector().at(i).asFactor().toString(); // the RFactor as a String
      Node node = nodemap.get(nodeNames.get(i)); // the node to edit
      int end = rfactor.indexOf(')'); // getting level String
      String temp = rfactor.substring(POSITION, end); // getting level String
      String[] temp2 = temp.split(","); // splitting String
      for (int j = 0; j < temp2.length; j++) {
        String str = temp2[j].substring(1, temp2[j].length() - 1);
        node.addNodeLevel(str); // add level to node
      }
    }

    // Step 5 get algorithm on BN Network
    String algorithm = network.asVector().at("learning").asVector().at("algo").asString();

    // Step 6 add Arcs to BN Network
    ArrayList<Association> arcs = new ArrayList<Association>();
    String[] temp = network.asVector().at("arcs").asStringArray();
    for (int i = 0; i < temp.length / 2; i++) {
      arcs.add(new Association(nodemap.get(temp[temp.length / 2 + i]), nodemap.get(temp[i])));
    }

    // Return BNNetwork
    return new Network(algorithm, nodelist, arcs);
  }
Esempio n. 12
0
  // Run prediction method on data based on file name
  public StockInfo[] runPrediction(Map<String, StockInfo[]> dataToProcess) {
    double[] resultArrayOpen = null;
    double[] resultArrayHigh = null;
    double[] resultArrayLow = null;
    double[] resultArrayClose = null;
    double[] resultArrayVolume = null;

    StockInfo[] predictedDataArray = null;

    try {
      logger.log("prediction will start");
      // Flat the data to perform prediction on it
      StockInfo[] flatStockInfo = this.flatMapOfData(dataToProcess);
      Collections.reverse(Arrays.asList(flatStockInfo));

      REXP x;
      RVector v;

      // Check for installed packages
      x = re.eval("installed.packages()");
      v = x.asVector();
      String[] packages = x.asStringArray();
      boolean isForecastInstalled = false;
      logger.log("<R> getting installed packages");
      for (int index = 0; index < packages.length && isForecastInstalled == false; index++) {
        logger.log("<R> has installed " + packages[index]);
        if (packages[index] != null && packages[index].compareTo("forecast") == 0) {
          isForecastInstalled = true;
        }
      }

      // If forecast needs to be installed
      if (isForecastInstalled == false) {
        logger.log("<R> will set repos");

        // Set CRAN
        re.eval("r <- getOption(\"repos\")");
        re.eval("r[\"CRAN\"] <- \"http://cran.us.r-project.org\"");
        re.eval("options(repos = r)");
        re.eval("rm(r)");

        // Install forecast
        re.eval("install.packages(\"forecast\")");

        logger.log("<R> will install forecast package");
      }
      // Load forecast library
      re.eval("library(\"forecast\")");
      logger.log("<R> loaded forecast");

      // Make prediction for Open value -----------------------
      // Load data into R
      logger.log("<R> loading data into R");

      StringBuilder builder = new StringBuilder("inputData <- c(");
      for (int index = 0; index < flatStockInfo.length; index++) {
        builder.append(flatStockInfo[index].open);
        if (index != flatStockInfo.length - 1) {
          builder.append(",");
        } else {
          builder.append(")");
        }
      }
      String stringFromBuilder = builder.toString();
      re.eval(stringFromBuilder);
      // Create time series from data
      logger.log("<R> forecasting open values BestFit");
      re.eval("temporalData <- ts(inputData, frequency=365)");
      // Forecast data
      re.eval("forecastData <- forecast(temporalData, h=30)");
      // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)");
      // re.eval("forecastData <- forecast(arimaModel, h=30)");
      x = re.eval("forecastData");
      v = x.asVector();
      x = (REXP) v.elementAt(1); // instead of 3
      resultArrayOpen = x.asDoubleArray();

      // Make prediction for High value ------------------------
      builder = new StringBuilder("inputData <- c(");
      for (int index = 0; index < flatStockInfo.length; index++) {
        builder.append(flatStockInfo[index].high);
        if (index != flatStockInfo.length - 1) {
          builder.append(",");
        } else {
          builder.append(")");
        }
      }
      stringFromBuilder = builder.toString();
      re.eval(stringFromBuilder);
      // Create time series from data
      logger.log("<R> forecasting high values BestFit");
      re.eval("temporalData <- ts(inputData, frequency=365)");
      // Forecast data
      re.eval("forecastData <- forecast(temporalData, h=30)");
      // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)");
      // re.eval("forecastData <- forecast(arimaModel, h=30)");
      x = re.eval("forecastData");
      v = x.asVector();
      x = (REXP) v.elementAt(1);
      resultArrayHigh = x.asDoubleArray();

      // Make prediction for Low value ------------------------
      builder = new StringBuilder("inputData <- c(");
      for (int index = 0; index < flatStockInfo.length; index++) {
        builder.append(flatStockInfo[index].low);
        if (index != flatStockInfo.length - 1) {
          builder.append(",");
        } else {
          builder.append(")");
        }
      }
      stringFromBuilder = builder.toString();
      re.eval(stringFromBuilder);
      // Create time series from data
      logger.log("<R> forecasting low values BestFit");
      re.eval("temporalData <- ts(inputData, frequency=365)");
      // Forecast data
      re.eval("forecastData <- forecast(temporalData, h=30)");
      // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)");
      // re.eval("forecastData <- forecast(arimaModel, h=30)");
      x = re.eval("forecastData");
      v = x.asVector();
      x = (REXP) v.elementAt(1);
      resultArrayLow = x.asDoubleArray();

      // Make prediction for Close value ------------------------
      builder = new StringBuilder("inputData <- c(");
      for (int index = 0; index < flatStockInfo.length; index++) {
        builder.append(flatStockInfo[index].close);
        if (index != flatStockInfo.length - 1) {
          builder.append(",");
        } else {
          builder.append(")");
        }
      }
      stringFromBuilder = builder.toString();
      re.eval(stringFromBuilder);
      // Create time series from data
      logger.log("<R> forecasting close values BestFit");
      re.eval("temporalData <- ts(inputData, frequency=365)");
      // Forecast data
      re.eval("forecastData <- forecast(temporalData, h=30)");
      // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)");
      // re.eval("forecastData <- forecast(arimaModel, h=30)");
      x = re.eval("forecastData");
      v = x.asVector();
      x = (REXP) v.elementAt(1);
      resultArrayClose = x.asDoubleArray();

      // Make prediction for Close value ------------------------
      builder = new StringBuilder("inputData <- c(");
      for (int index = 0; index < flatStockInfo.length; index++) {
        builder.append(flatStockInfo[index].volume);
        if (index != flatStockInfo.length - 1) {
          builder.append(",");
        } else {
          builder.append(")");
        }
      }
      stringFromBuilder = builder.toString();
      re.eval(stringFromBuilder);
      // Create time series from data
      re.eval("temporalData <- ts(inputData, frequency=365)");
      // Forecast data
      re.eval("forecastData <- forecast(temporalData, h=30)");
      // re.eval("arimaModel <- auto.arima(temporalData, max.p=5, max.q=5, max.P=5, max.Q=5)");
      // re.eval("forecastData <- forecast(arimaModel, h=30)");
      x = re.eval("forecastData");
      v = x.asVector();
      x = (REXP) v.elementAt(1);
      resultArrayVolume = x.asDoubleArray();

      // Create a single StockInfo[] for all data
      StockInfo predictedData;
      predictedDataArray = new StockInfo[30];

      Date lastDate = flatStockInfo[flatStockInfo.length - 1].date;
      Calendar c = Calendar.getInstance();
      c.setTime(lastDate);
      c.add(Calendar.DATE, 1);

      logger.log("<R> values for forecasted data");

      SimpleDateFormat dateFormat = new SimpleDateFormat();
      dateFormat.applyPattern("dd/MM/YYYY");

      // For all days that were predicted
      for (int index = 0; index < 30; index++) {
        predictedData = new StockInfo();
        predictedData.open = (float) resultArrayOpen[index];

        float maxHigh =
            (float)
                StrictMath.max(
                    resultArrayClose[index],
                    StrictMath.max(resultArrayHigh[index], resultArrayOpen[index]));
        predictedData.high = maxHigh;

        float minLow =
            (float)
                StrictMath.min(
                    resultArrayClose[index],
                    StrictMath.min(resultArrayLow[index], resultArrayOpen[index]));
        predictedData.low = minLow;

        predictedData.close = (float) resultArrayClose[index];
        predictedData.volume = (int) resultArrayVolume[index];

        while (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
            || c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
          c.add(Calendar.DATE, 1);
        }
        predictedData.date = c.getTime();

        predictedDataArray[index] = predictedData;

        logger.log(
            "<R> stock prediction "
                + dateFormat.format(predictedData.date.getTime())
                + " open: "
                + predictedData.open
                + " high: "
                + predictedData.high
                + " low: "
                + predictedData.low
                + " close: "
                + predictedData.close);
        c.add(Calendar.DATE, 1);
      }

    } catch (Exception e) {
      logger.logException(e);
    }

    return predictedDataArray;
  }
Esempio n. 13
0
 public SpotDES initializeDesign() throws InPUTException {
   REXP designs = runCommand("inputConfig$alg.currentDesign", true);
   return new SpotDES(designs.asVector(), paramIds, inputROI);
 }
Esempio n. 14
0
 public void retrieveNextDesign() {
   REXP expr = runCommand("colnames(inputConfig$alg.currentDesign)", true);
   paramIds = expr.asStringArray();
 }