import java.io.*; import java.util.*; public class App { public static void main(String args[]) { // Initialize start double w = 0.70, c1 = 0.20, c2 = 0.60; int n = 5, E = 4; Map> cpMap = new HashMap<>(); Map> cfMap = new HashMap<>(); List lbpList = new ArrayList<>(); lbpList = initializeMap(lbpList, n); List lbfList = new ArrayList<>(); lbfList = initializeMap(lbfList, n); double GBF = 0.0; double GBP = 0.0; List vList = new ArrayList<>(); // Initialize end Random random = new Random(); List r1List = new ArrayList<>(); r1List = getRandomizeArr(r1List, random, n); List r2List = new ArrayList<>(); r2List = getRandomizeArr(r2List, random, n); List CPList = getCPArr(r1List, false, n); List VList = getCPArr(r2List, true, n); cpMap = createInitialMap(CPList); String basePath = "C:\\Program Files\\RPA\\standard"; String rpaAestusPath = basePath + "\\Aestus.cfg"; String changingValue = "change_me"; //triggerRPAandReturnValue(basePath); List cfMapValList = new ArrayList<>(); for (Integer mapKey : cpMap.keySet()) { for (double elem : cpMap.get(mapKey)) { if (mapKey != 0) { // rpa dosyasini güncelle try { changeMeOperation(elem, rpaAestusPath, changingValue); changingValue = String.valueOf(elem); System.out.print("Changing value : " + changingValue + " "); } catch (FileNotFoundException e) { e.printStackTrace(); } // rpa outputunu islet ve bul // CF(1,1) cfMapValList.add(triggerRPAandReturnValue(basePath)); } } // cfmap i burda doldur if (mapKey != 0) { cfMap.put(mapKey, cfMapValList); lbfList = updateLBF(cfMapValList, lbfList); GBF = getGBF(lbfList); GBP = getGBP(lbfList, lbpList, GBF); lbpList = updateLBP(cfMapValList, lbfList); vList = calculateVelocity(vList, w, c1,c2, r1List, r2List, cpMap.get(mapKey), lbpList, GBP, n); } } // Traverse the map for (Integer mapKey : cfMap.keySet()) { System.out.print(cfMap.get(mapKey)); } } public static List calculateNewCPListVals(List prevCPListVals) { } public static List calculateVelocity(List vList, double w, double c1, double c2, List r1List, List r2List, List cpMapValList, List lbpList, double gbp, int n) { List newVList = new ArrayList<>(); for (int i = 0; i < n; i++) { newVList.add((w * vList.get(i)) + ((c1 * r1List.get(i)) * (lbpList.get(i) - cpMapValList.get(i))) + ((c2 * r2List.get(i)) * (gbp - cpMapValList.get(i)))); } return newVList; } public static double getGBP(List LBFlist, List LBPlist, double GBF) { int index = 0; for (int i = 0; i < LBFlist.size(); i++) { if (GBF == LBFlist.get(i)) { index = i; break; } } return LBPlist.get(index); } public static double getGBF(List list) { double best = 0.0; for (Double elem : list) { if (elem > best) { best = elem; } } return best; } public static List updateLBP(List cfList, List lbfList) { List newLbpList = new ArrayList<>(); for (int i = 0; i < cfList.size(); i++) { if (cfList.get(i) > lbfList.get(i)) { newLbpList.add(cfList.get(getPosition(cfList, cfList.get(i)))); } else if (cfList.get(i) < lbfList.get(i)) { newLbpList.add(lbfList.get(getPosition(lbfList, lbfList.get(i)))); } } return newLbpList; } public static List updateLBF(List cfList, List lbfList) { List newLbfList = new ArrayList<>(); for (int i = 0; i < cfList.size(); i++) { if (cfList.get(i) > lbfList.get(i)) { newLbfList.add(cfList.get(i)); } else if (cfList.get(i) < lbfList.get(i)) { newLbfList.add(lbfList.get(i)); } } return newLbfList; } public static List initializeMap(List list, int n) { for (int i = 0; i < n; i++) { list.add(0.0); } return list; } public static int getPosition(List list, double searched) { int position = -1; for (int i = 0; i < list.size(); i++) { if (list.get(i) == searched) { position = i; break; } } return position; } public static void changeMeOperation(double val, String rpaPath, String searchedVal) throws FileNotFoundException { Scanner scan = new Scanner(new File(rpaPath)); StringBuilder sb = new StringBuilder(); while (scan.hasNext()) { sb.append(scan.next()); } String mainContent = sb.toString(); String modified; int searchedIndex; if (mainContent.contains(searchedVal)) { searchedIndex = mainContent.indexOf(searchedVal); modified = mainContent.substring(0, searchedIndex); modified += val + ";"; modified += mainContent.substring(searchedIndex + searchedVal.length() + 1); try { BufferedWriter writer = new BufferedWriter(new FileWriter(rpaPath)); writer.write(modified); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } public static Double triggerRPAandReturnValue(String basePath) { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.directory(new File(basePath)); processBuilder.command("cmd.exe", "/c", "rpac.exe -i " + "Aestus.cfg" + " -o " + "outputata"); String searchedVal; try { Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; String content = ""; while ((line = reader.readLine()) != null) { // Shows the whole terminal output System.out.println(line); content += line; } process.destroy(); int searchedIndex; searchedVal = "Specific impulse (vac):"; if (content.contains(searchedVal)) { searchedIndex = content.indexOf(searchedVal); searchedVal = content.substring(searchedIndex + searchedVal.length(), searchedIndex + searchedVal.length() + 20); searchedVal = searchedVal.trim(); } int exitCode = process.waitFor(); System.out.println("RPA Returned : " + searchedVal); System.out.println("\nExited with error code : " + exitCode); reader.close(); return Double.parseDouble(searchedVal); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return 0.0; } public static Map> createInitialMap(List list) { Map> hMap = new HashMap<>(); hMap.put(0, list); hMap.put(1, list); return hMap; } public static List getRandomizeArr(List list, Random random, int n) { list.clear(); for (int i = 0; i < n; i++) { list.add(random.nextDouble()); } return list; } //true for v public static List getCPArr(List list, boolean isV, int n) { // bu if v ler için List newList = new ArrayList<>(); if (isV) { for (int i = 0; i < n; i++) { // her ihtimale karsi burda newList.add(list.get(i)); } } else { for (int i = 0; i < n; i++) { newList.add(list.get(i) * 2); } } return newList; } }