From b114e934c008781788ff9fb294efd14dbaba5052 Mon Sep 17 00:00:00 2001 From: ElijahRosal <148596994+ElijahRosal@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:01:24 -0800 Subject: [PATCH 1/7] Has the beta version of the algorithm --- PowerTester.java | 227 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 223 insertions(+), 4 deletions(-) diff --git a/PowerTester.java b/PowerTester.java index a1367b1..a00b358 100644 --- a/PowerTester.java +++ b/PowerTester.java @@ -1,5 +1,224 @@ +import java.util.Scanner; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.io.*; + public class PowerTester { - public static void main(String[] args) { - System.out.println("WEEEEEEEEEEEEEEEEe"); - } -} \ No newline at end of file + static ArrayList wholeArea = new ArrayList<>(); + static ArrayList locationList = new ArrayList<>(); + static ArrayList> divArea = new ArrayList<>(); + static ArrayList onApps = new ArrayList<>(); + static ArrayList smartOnApps = new ArrayList<>(); + static Scanner scnr = new Scanner(System.in); + + public static void readAppFile(String file) { + try { + Scanner scnr = new Scanner(new File(file)); + while (scnr.hasNext()) { + String placeholder = scnr.nextLine(); + try (Scanner parse = new Scanner(placeholder)) { + parse.useDelimiter(","); + long location = parse.nextLong(); + String name = parse.next(); + int powerUse = parse.nextInt(); + double state = parse.nextDouble(); + boolean type = parse.nextBoolean(); + double percent = parse.nextDouble(); + locationList.add(location); + if (type) { + SmartAppliance smartApp = new SmartAppliance(type, powerUse, state, location, name, percent); + wholeArea.add(smartApp); + } else { + Appliance App = new Appliance(type, powerUse, state, location, name); + wholeArea.add(App); + } + } + } + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public static void simulation() { + int totalWattage = 0; + int maxWattage = 0; + int timeSteps = 0; + int T = 0; + System.out.println("Enter total allowed wattage:"); + maxWattage = scnr.nextInt(); + while (maxWattage < 0) { + System.out.println("Please input a positive integer: "); + maxWattage = scnr.nextInt(); + } + + System.out.println("Enter desired amount of time steps for simulation:"); + timeSteps = scnr.nextInt(); + while (timeSteps <= 0) { + System.out.println("Please input a positive integer: "); + timeSteps = scnr.nextInt(); + } + + for (int i = 0; i < wholeArea.size(); ++i) { + wholeArea.get(i).updateStatus(); + if (wholeArea.get(i).isOn() && wholeArea.get(i).getType()) { + totalWattage += wholeArea.get(i).getOnWattage(); + //smartOnApps.add((SmartAppliance) wholeArea.get(i)); + + } else if (wholeArea.get(i).isOn()) { + totalWattage += wholeArea.get(i).getOnWattage(); + //onApps.add(wholeArea.get(i)); + } + } + + for (int i = 0; i < locationList.size(); ++i) { + for (int j = i+1; j < locationList.size(); ++j) { + if (locationList.get(i).equals(locationList.get(j))) { + locationList.remove(j); + --j; + } + } + } + // intialize the divArea arrayList with the locations and appliances in each location + for (int i = 0; i < locationList.size(); ++i) { + ArrayList locale = new ArrayList<>(); + for (int j = 0; j < wholeArea.size(); ++j) { + if (locationList.get(i).equals(wholeArea.get(j).getLocation()) && wholeArea.get(j).isOn()) { + locale.add(wholeArea.get(j)); + } + } + divArea.add(locale); + } + + + System.out.println("total Watt: " + totalWattage); + while (totalWattage > maxWattage) { + totalWattage = 0; + smartOnApps.clear(); + onApps.clear(); + + boolean count = false; + + for (int i = 0; i < divArea.size(); ++i) { + ArrayList currentLocation = divArea.get(i); + for (int j = 0; j < currentLocation.size(); ++j) { + if (currentLocation.get(j).getType()) { + smartOnApps.add((SmartAppliance) currentLocation.get(j)); + } else { + onApps.add(currentLocation.get(j)); + } + } + } + // checker if all Smart Appliances have been set to low + if (T < smartOnApps.size()) { + System.out.println(T); + ++T; + } else { + count = true; + } + + // once count is true, the simulation will start browning out the locations with the least amount of appliances that are on + if (count) { + + int applianceCount = divArea.get(0).size(); + int locationLow = 0; + for (int i = 0; i < divArea.size(); ++i) { + if (applianceCount > divArea.get(i).size()) { + //System.out.println("Low: "+divArea.get(i).size()); + applianceCount = divArea.get(i).size(); + locationLow = i; + } + } + + locationList.remove(locationLow); + divArea.remove(locationLow); + smartOnApps.clear(); + onApps.clear(); + + for (int i = 0; i < divArea.size(); ++i) { + ArrayList currentLocation = divArea.get(i); + for (int j = 0; j < currentLocation.size(); ++j) { + if (currentLocation.get(j).getType()) { + smartOnApps.add((SmartAppliance) currentLocation.get(j)); + } else { + onApps.add(currentLocation.get(j)); + } + } + } + T = smartOnApps.size(); + } + + + for (int i = 0; i < smartOnApps.size(); ++i) { + smartOnApps.get(i).setOnLow(true); + } + + // comparator and collections to sort the Smart Appliances from lowest to highest wattage + Comparator comparator = Comparator.comparing(SmartAppliance::getOnWattage); + + Collections.sort(smartOnApps, comparator); + + for (int i = T; i < smartOnApps.size(); ++i) { + smartOnApps.get(i).setOnLow(false); + } + + for (int i = 0; i < onApps.size(); ++i) { + totalWattage += onApps.get(i).getOnWattage(); + } + + for (int i = 0; i < smartOnApps.size(); ++i) { + //System.out.println("Smart" + smartOnApps.get(i).getOnWattage()); + totalWattage += smartOnApps.get(i).getOnWattage(); + } + + System.out.println("total Watt: " + totalWattage); + } + } + + public static void main(String[] args) { + + //User interactive part + String option1; + + while(true){// Application menu to be displayed to the user. + System.out.println("Select an option:"); + System.out.println("Type \"A\" Add an appliance"); + System.out.println("Type \"D\" Delete an appliance"); + System.out.println("Type \"L\" List the appliances"); + System.out.println("Type \"F\" Read Appliances from a file"); + System.out.println("Type \"S\" To Start the simulation"); + System.out.println("Type \"Q\" Quit the program"); + option1 = scnr.nextLine(); + + if (option1.equals("A")) { + + } + else if (option1.equals("D")) { + + } + else if (option1.equals("L")) { + + } + else if (option1.equals("F")) { + System.out.println("Enter the file path: "); + String filePath = scnr.nextLine(); + readAppFile(filePath); + } + else if (option1.equals("S")) { + if (wholeArea.size() <= 0) { + System.out.println("Please add appliances first, either manually or from a file."); + } else { + simulation(); + } + } + else if (option1.equals("Q")) { + System.exit(0); + } + else { + System.out.println("Invalid Input"); + } + + } + } + +} From ea7e14ddd10d0015877e07d5fb7f5d2a8e858dda Mon Sep 17 00:00:00 2001 From: ElijahRosal <148596994+ElijahRosal@users.noreply.github.com> Date: Sat, 9 Dec 2023 23:24:12 -0800 Subject: [PATCH 2/7] updated the simulation method and added some outputs --- PowerTester.java | 248 +++++++++++++++++++++++++++++------------------ 1 file changed, 152 insertions(+), 96 deletions(-) diff --git a/PowerTester.java b/PowerTester.java index a00b358..cc930ad 100644 --- a/PowerTester.java +++ b/PowerTester.java @@ -11,6 +11,38 @@ public class PowerTester { static ArrayList onApps = new ArrayList<>(); static ArrayList smartOnApps = new ArrayList<>(); static Scanner scnr = new Scanner(System.in); + static String folderName = "PowerGrid"; + static String directoryPath = "C:/"; + + public static void fileWrite(int time) { + // Names for the file path + String fileName = "TimeStep " + (time+1) + ".txt"; + + // Create the folder + File directory = new File(directoryPath, folderName); + directory.mkdirs(); + + // Combine the directory path and file name to create the complete file path + String filePath = directoryPath + folderName + "/" + fileName; + try { + // Create a FileWriter object to write to the file + FileWriter writer = new FileWriter(filePath); + + // Write and print the current time step + for (int i = 0; i < divArea.size(); ++i) { + ArrayList locale = divArea.get(i); + for (int j = 0; j < locale.size(); ++j) { + writer.write(locale.get(j).toString()); + writer.write(System.lineSeparator()); + } + + } + writer.close(); + } catch (IOException e) { + // Handle IO exception + e.printStackTrace(); + } + } public static void readAppFile(String file) { try { @@ -22,29 +54,39 @@ public static void readAppFile(String file) { long location = parse.nextLong(); String name = parse.next(); int powerUse = parse.nextInt(); - double state = parse.nextDouble(); + double prob = parse.nextDouble(); boolean type = parse.nextBoolean(); double percent = parse.nextDouble(); locationList.add(location); if (type) { - SmartAppliance smartApp = new SmartAppliance(type, powerUse, state, location, name, percent); + SmartAppliance smartApp = new SmartAppliance(type, powerUse, prob, location, name, percent); wholeArea.add(smartApp); } else { - Appliance App = new Appliance(type, powerUse, state, location, name); + Appliance App = new Appliance(type, powerUse, prob, location, name); wholeArea.add(App); } } } + + for (int i = 0; i < locationList.size(); ++i) { + for (int j = i+1; j < locationList.size(); ++j) { + if (locationList.get(i).equals(locationList.get(j))) { + locationList.remove(j); + --j; + } + } + } + } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + System.out.println("File not found. Try again."); } } public static void simulation() { - int totalWattage = 0; + int maxWattage = 0; int timeSteps = 0; - int T = 0; + //ArrayList totalLocations = (ArrayList) locationList.clone(); + System.out.println("Enter total allowed wattage:"); maxWattage = scnr.nextInt(); while (maxWattage < 0) { @@ -59,79 +101,47 @@ public static void simulation() { timeSteps = scnr.nextInt(); } - for (int i = 0; i < wholeArea.size(); ++i) { - wholeArea.get(i).updateStatus(); - if (wholeArea.get(i).isOn() && wholeArea.get(i).getType()) { - totalWattage += wholeArea.get(i).getOnWattage(); - //smartOnApps.add((SmartAppliance) wholeArea.get(i)); - - } else if (wholeArea.get(i).isOn()) { - totalWattage += wholeArea.get(i).getOnWattage(); - //onApps.add(wholeArea.get(i)); - } - } + ///////////////////////////////////////// + //////// Going through each time step + /////////////////////////////////////////////////////////// - for (int i = 0; i < locationList.size(); ++i) { - for (int j = i+1; j < locationList.size(); ++j) { - if (locationList.get(i).equals(locationList.get(j))) { - locationList.remove(j); - --j; - } - } - } - // intialize the divArea arrayList with the locations and appliances in each location - for (int i = 0; i < locationList.size(); ++i) { - ArrayList locale = new ArrayList<>(); - for (int j = 0; j < wholeArea.size(); ++j) { - if (locationList.get(i).equals(wholeArea.get(j).getLocation()) && wholeArea.get(j).isOn()) { - locale.add(wholeArea.get(j)); + for ( int k = 0; k < timeSteps; ++k ) { + divArea.clear(); + ArrayList totalLocations = (ArrayList) locationList.clone(); + int totalWattage = 0; + int locationCounter = 0; + int T = 0; + // Loop to initialise whether an appliance is on or off + for (int i = 0; i < wholeArea.size(); ++i) { + wholeArea.get(i).setIsOn(false); + wholeArea.get(i).updateStatus(); + if (wholeArea.get(i).isOn() && wholeArea.get(i).getType()) { + totalWattage += wholeArea.get(i).getOnWattage(); + //smartOnApps.add((SmartAppliance) wholeArea.get(i)); + + } else if (wholeArea.get(i).isOn()) { + totalWattage += wholeArea.get(i).getOnWattage(); + //onApps.add(wholeArea.get(i)); } } - divArea.add(locale); - } - - - System.out.println("total Watt: " + totalWattage); - while (totalWattage > maxWattage) { - totalWattage = 0; - smartOnApps.clear(); - onApps.clear(); - boolean count = false; - - for (int i = 0; i < divArea.size(); ++i) { - ArrayList currentLocation = divArea.get(i); - for (int j = 0; j < currentLocation.size(); ++j) { - if (currentLocation.get(j).getType()) { - smartOnApps.add((SmartAppliance) currentLocation.get(j)); - } else { - onApps.add(currentLocation.get(j)); + // intialize the divArea arrayList with the appliances in each location + for (int i = 0; i < locationList.size(); ++i) { + ArrayList locale = new ArrayList<>(); + for (int j = 0; j < wholeArea.size(); ++j) { + if (locationList.get(i).equals(wholeArea.get(j).getLocation()) && wholeArea.get(j).isOn()) { + locale.add(wholeArea.get(j)); } } + divArea.add(locale); } - // checker if all Smart Appliances have been set to low - if (T < smartOnApps.size()) { - System.out.println(T); - ++T; - } else { - count = true; - } - - // once count is true, the simulation will start browning out the locations with the least amount of appliances that are on - if (count) { - - int applianceCount = divArea.get(0).size(); - int locationLow = 0; - for (int i = 0; i < divArea.size(); ++i) { - if (applianceCount > divArea.get(i).size()) { - //System.out.println("Low: "+divArea.get(i).size()); - applianceCount = divArea.get(i).size(); - locationLow = i; - } - } + ///////////////////////////////////////// + //////// Start of the process for turning appliances off + /////////////////////////////////////////////////////////// + System.out.println("Total Wattage: " + totalWattage); + while (totalWattage > maxWattage) { - locationList.remove(locationLow); - divArea.remove(locationLow); + totalWattage = 0; smartOnApps.clear(); onApps.clear(); @@ -145,41 +155,87 @@ public static void simulation() { } } } - T = smartOnApps.size(); - } - - - for (int i = 0; i < smartOnApps.size(); ++i) { - smartOnApps.get(i).setOnLow(true); - } - - // comparator and collections to sort the Smart Appliances from lowest to highest wattage - Comparator comparator = Comparator.comparing(SmartAppliance::getOnWattage); - - Collections.sort(smartOnApps, comparator); + + for (int i = 0; i < smartOnApps.size(); ++i) { + smartOnApps.get(i).setOnLow(true); + } + + // checker if all Smart Appliances have been set to low + if (T < smartOnApps.size()) { + ++T; + // comparator amd collection to sort the Smart Appliances from lowest to highest wattage + Comparator comparator = Comparator.comparing(SmartAppliance::getOnWattage); + + Collections.sort(smartOnApps, comparator); - for (int i = T; i < smartOnApps.size(); ++i) { - smartOnApps.get(i).setOnLow(false); + for (int i = T; i < smartOnApps.size(); ++i) { + smartOnApps.get(i).setOnLow(false); + } + + } else { + + ++locationCounter; + int applianceCount = divArea.get(0).size(); + int locationLow = 0; + + // for loop to find the location with the least amount of On Appliances + + for (int i = 0; i < divArea.size(); ++i) { + if (applianceCount > divArea.get(i).size()) { + applianceCount = divArea.get(i).size(); + locationLow = i; + } + } + totalLocations.remove(locationLow); + divArea.remove(locationLow); + smartOnApps.clear(); + onApps.clear(); + + for (int i = 0; i < divArea.size(); ++i) { + ArrayList currentLocation = divArea.get(i); + for (int j = 0; j < currentLocation.size(); ++j) { + if (currentLocation.get(j).getType()) { + smartOnApps.add((SmartAppliance) currentLocation.get(j)); + } else { + onApps.add(currentLocation.get(j)); + } + } + } + T = smartOnApps.size(); + } + + for (int i = 0; i < onApps.size(); ++i) { + totalWattage += onApps.get(i).getOnWattage(); + } + + for (int i = 0; i < smartOnApps.size(); ++i) { + totalWattage += smartOnApps.get(i).getOnWattage(); + } + + } - for (int i = 0; i < onApps.size(); ++i) { - totalWattage += onApps.get(i).getOnWattage(); - } + ///////////////////////////////////////// + //////// Printing data from the Time Step + /////////////////////////////////////////////////////////// - for (int i = 0; i < smartOnApps.size(); ++i) { - //System.out.println("Smart" + smartOnApps.get(i).getOnWattage()); - totalWattage += smartOnApps.get(i).getOnWattage(); - } + System.out.println("Total Wattage After: " + totalWattage); + System.out.println("Ammount of Smart Appliances set to Low: " + T); + System.out.println("Ammount of Locations Browned Out:" + locationCounter); + System.out.println(); + fileWrite(k); - System.out.println("total Watt: " + totalWattage); } } + public static void addApplication() { + System.out.println("Filler"); + } + public static void main(String[] args) { //User interactive part String option1; - while(true){// Application menu to be displayed to the user. System.out.println("Select an option:"); System.out.println("Type \"A\" Add an appliance"); @@ -191,7 +247,7 @@ public static void main(String[] args) { option1 = scnr.nextLine(); if (option1.equals("A")) { - + addApplication(); } else if (option1.equals("D")) { From a68a7a70730b1833a6406165e4a263f52d350934 Mon Sep 17 00:00:00 2001 From: ElijahRosal <148596994+ElijahRosal@users.noreply.github.com> Date: Sun, 10 Dec 2023 16:23:14 -0800 Subject: [PATCH 3/7] Update PowerTester.java to add better outputs --- PowerTester.java | 76 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/PowerTester.java b/PowerTester.java index cc930ad..7b7a984 100644 --- a/PowerTester.java +++ b/PowerTester.java @@ -13,8 +13,7 @@ public class PowerTester { static Scanner scnr = new Scanner(System.in); static String folderName = "PowerGrid"; static String directoryPath = "C:/"; - - public static void fileWrite(int time) { + public static void fileWrite(int time, int imapact, int[] active, int[] smart) { // Names for the file path String fileName = "TimeStep " + (time+1) + ".txt"; @@ -78,14 +77,15 @@ public static void readAppFile(String file) { } } catch (FileNotFoundException e) { - System.out.println("File not found. Try again."); + System.out.println("File not found"); } } public static void simulation() { int maxWattage = 0; int timeSteps = 0; - //ArrayList totalLocations = (ArrayList) locationList.clone(); + int[] locationPoints = new int[locationList.size()]; + int[] smartPoints = new int[locationList.size()]; System.out.println("Enter total allowed wattage:"); maxWattage = scnr.nextInt(); @@ -106,11 +106,13 @@ public static void simulation() { /////////////////////////////////////////////////////////// for ( int k = 0; k < timeSteps; ++k ) { + System.out.println("/////////////// Time Step: " + (k+1) + " ///////////////"); divArea.clear(); ArrayList totalLocations = (ArrayList) locationList.clone(); int totalWattage = 0; int locationCounter = 0; int T = 0; + int appCount = 0; // Loop to initialise whether an appliance is on or off for (int i = 0; i < wholeArea.size(); ++i) { wholeArea.get(i).setIsOn(false); @@ -136,9 +138,9 @@ public static void simulation() { divArea.add(locale); } ///////////////////////////////////////// - //////// Start of the process for turning appliances off + //////// Start of the process for turning appliances off or low /////////////////////////////////////////////////////////// - System.out.println("Total Wattage: " + totalWattage); + System.out.println("Total Starting Wattage: " + totalWattage); while (totalWattage > maxWattage) { totalWattage = 0; @@ -186,6 +188,7 @@ public static void simulation() { locationLow = i; } } + appCount += divArea.get(locationLow).size(); totalLocations.remove(locationLow); divArea.remove(locationLow); smartOnApps.clear(); @@ -215,17 +218,71 @@ public static void simulation() { } + + for (int i = 0; i < locationList.size(); ++i) { + long location = locationList.get(i); + for (int h = 0; h < smartOnApps.size(); ++h) { + if (smartOnApps.get(h).getOnLow() && smartOnApps.get(h).getLocation() == location) { + smartPoints[i] += 1; + } + } + for (int j = 0; j < totalLocations.size(); ++j) { + if (location == totalLocations.get(j)) { + locationPoints[i] += 1; + } + } + } ///////////////////////////////////////// //////// Printing data from the Time Step /////////////////////////////////////////////////////////// - System.out.println("Total Wattage After: " + totalWattage); System.out.println("Ammount of Smart Appliances set to Low: " + T); - System.out.println("Ammount of Locations Browned Out:" + locationCounter); + System.out.println("Ammount of Locations Browned Out: " + locationCounter); + System.out.println("Amount of Appliances Turned Off: " + appCount); System.out.println(); - fileWrite(k); + } + + ///////////////////////////////////////// + //////// Printing Summary of all Time Steps + /////////////////////////////////////////////////////////// + System.out.println("/////////////// Summary ///////////////"); + int impactLocation = 0; + int active = locationPoints[0]; + int smart = 0; + for (int i = 0; i < locationPoints.length; ++i) { + if (active > locationPoints[i]) { + active = locationPoints[i]; + } + } + for ( int i = 0; i < smartPoints.length; ++i) { + if (active == locationPoints[i] && smart <= smartPoints[i]) { + smart = smartPoints[i]; + impactLocation = i; + + } + } + if (smart == 0 && active == 0) { + int brown = 0; + for (int i = 0; i < locationPoints.length; ++i) { + if (locationPoints[i] == 0) { + brown += 1; + } + } + System.out.println(brown + " locations were browned out every time for all " + timeSteps + " runs"); + + } else if (timeSteps == locationPoints[impactLocation]) { + + System.out.println("Most Impacted Location: " + locationList.get(impactLocation) + " with " + smartPoints[impactLocation] + + " total appliances set to low over the course of " + timeSteps + " runs"); + + } else { + + System.out.println("Most Impacted Location: " + locationList.get(impactLocation) + " with " + (timeSteps-locationPoints[impactLocation]) + + " total brown outs and " + smartPoints[impactLocation] + " total appliances set to low over the course of " + timeSteps + " runs" ); } + System.out.println(); + } public static void addApplication() { @@ -236,6 +293,7 @@ public static void main(String[] args) { //User interactive part String option1; + readAppFile("C:/PowerGrid/app.txt"); while(true){// Application menu to be displayed to the user. System.out.println("Select an option:"); System.out.println("Type \"A\" Add an appliance"); From f081870502025c762fbe017ed4feb0e418ea34ad Mon Sep 17 00:00:00 2001 From: ElijahRosal <148596994+ElijahRosal@users.noreply.github.com> Date: Sun, 10 Dec 2023 16:24:10 -0800 Subject: [PATCH 4/7] Update PowerTester.java --- PowerTester.java | 1 - 1 file changed, 1 deletion(-) diff --git a/PowerTester.java b/PowerTester.java index 7b7a984..09626f4 100644 --- a/PowerTester.java +++ b/PowerTester.java @@ -293,7 +293,6 @@ public static void main(String[] args) { //User interactive part String option1; - readAppFile("C:/PowerGrid/app.txt"); while(true){// Application menu to be displayed to the user. System.out.println("Select an option:"); System.out.println("Type \"A\" Add an appliance"); From 3b23224fe7bb166e680ee113dbb4778adea01ccd Mon Sep 17 00:00:00 2001 From: ElijahRosal <148596994+ElijahRosal@users.noreply.github.com> Date: Sun, 10 Dec 2023 17:01:07 -0800 Subject: [PATCH 5/7] added methods to add or delete appliances --- PowerTester.java | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/PowerTester.java b/PowerTester.java index 09626f4..cfdf394 100644 --- a/PowerTester.java +++ b/PowerTester.java @@ -286,13 +286,46 @@ public static void simulation() { } public static void addApplication() { - System.out.println("Filler"); + System.out.println("Type \"true\" if the appliance is smart:"); + boolean type = scnr.nextBoolean(); + System.out.println("Enter the appliance's location ID:"); + long location = scnr.nextLong(); + System.out.println("Enter the name of the appliance:"); + String name = scnr.nextLine(); + System.out.println("Enter the appliance's watt usage:"); + int watts = scnr.nextInt(); + System.out.println("Enter the appliance's probability of being on:"); + double prob = scnr.nextDouble(); + System.out.println("Enter the appliance's location ID:"); + if (type) { + System.out.println("Enter the appliance's low usage percent (1.0-0.0):"); + double low = scnr.nextDouble(); + SmartAppliance newSmart = new SmartAppliance(type, watts, prob, location, name, low); + wholeArea.add(newSmart); + } else { + Appliance newApp = new Appliance(type, watts, prob, location, name); + wholeArea.add(newApp); + } + System.out.println("Successfully added your appliance"); + + } + + public static void deleteApp() { + System.out.println("Enter the index number of the appliance to delete:"); + int input = scnr.nextInt(); + if (input >= wholeArea.size() || input < 0) { + System.out.println("Enter a valid index number:"); + input = scnr.nextInt(); + } + wholeArea.remove(input); + System.out.println("Successfully removed appliance"); } public static void main(String[] args) { //User interactive part String option1; + readAppFile("C:/PowerGrid/app.txt"); while(true){// Application menu to be displayed to the user. System.out.println("Select an option:"); System.out.println("Type \"A\" Add an appliance"); @@ -307,7 +340,7 @@ public static void main(String[] args) { addApplication(); } else if (option1.equals("D")) { - + deleteApp(); } else if (option1.equals("L")) { From 040682aefdf13e7d457146d9345833f522cbbe03 Mon Sep 17 00:00:00 2001 From: ElijahRosal <148596994+ElijahRosal@users.noreply.github.com> Date: Sun, 10 Dec 2023 17:47:12 -0800 Subject: [PATCH 6/7] Updated writing to file methods and outputs --- PowerTester.java | 130 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 91 insertions(+), 39 deletions(-) diff --git a/PowerTester.java b/PowerTester.java index cfdf394..277dcfe 100644 --- a/PowerTester.java +++ b/PowerTester.java @@ -13,9 +13,8 @@ public class PowerTester { static Scanner scnr = new Scanner(System.in); static String folderName = "PowerGrid"; static String directoryPath = "C:/"; - public static void fileWrite(int time, int imapact, int[] active, int[] smart) { - // Names for the file path - String fileName = "TimeStep " + (time+1) + ".txt"; + static String fileName = "Simulation.txt"; + public static void fileWrite(int time, int preWatt, int postWatt, int sLow, int locationCount, int appCount) { // Create the folder File directory = new File(directoryPath, folderName); @@ -25,17 +24,22 @@ public static void fileWrite(int time, int imapact, int[] active, int[] smart) { String filePath = directoryPath + folderName + "/" + fileName; try { // Create a FileWriter object to write to the file - FileWriter writer = new FileWriter(filePath); + FileWriter writer = new FileWriter(filePath, true); // Write and print the current time step - for (int i = 0; i < divArea.size(); ++i) { - ArrayList locale = divArea.get(i); - for (int j = 0; j < locale.size(); ++j) { - writer.write(locale.get(j).toString()); - writer.write(System.lineSeparator()); - } - - } + writer.write("/////////////// Time Step: " + (time+1) + " ///////////////"); + writer.write(System.lineSeparator()); + writer.write("Total Starting Wattage: " + preWatt); + writer.write(System.lineSeparator()); + writer.write("Total Wattage After: " + postWatt); + writer.write(System.lineSeparator()); + writer.write("Ammount of Smart Appliances set to Low: " + sLow); + writer.write(System.lineSeparator()); + writer.write("Ammount of Locations Browned Out: " + locationCount); + writer.write(System.lineSeparator()); + writer.write("Amount of Appliances Turned Off: " + appCount); + writer.write(System.lineSeparator()); + writer.write(System.lineSeparator()); writer.close(); } catch (IOException e) { // Handle IO exception @@ -106,7 +110,6 @@ public static void simulation() { /////////////////////////////////////////////////////////// for ( int k = 0; k < timeSteps; ++k ) { - System.out.println("/////////////// Time Step: " + (k+1) + " ///////////////"); divArea.clear(); ArrayList totalLocations = (ArrayList) locationList.clone(); int totalWattage = 0; @@ -140,7 +143,8 @@ public static void simulation() { ///////////////////////////////////////// //////// Start of the process for turning appliances off or low /////////////////////////////////////////////////////////// - System.out.println("Total Starting Wattage: " + totalWattage); + int preWattage = totalWattage; + while (totalWattage > maxWattage) { totalWattage = 0; @@ -233,23 +237,30 @@ public static void simulation() { } } ///////////////////////////////////////// - //////// Printing data from the Time Step + //////// Printing data from the Time Step to console and file /////////////////////////////////////////////////////////// + System.out.println("/////////////// Time Step: " + (k+1) + " ///////////////"); + System.out.println("Total Starting Wattage: " + preWattage); System.out.println("Total Wattage After: " + totalWattage); System.out.println("Ammount of Smart Appliances set to Low: " + T); System.out.println("Ammount of Locations Browned Out: " + locationCounter); System.out.println("Amount of Appliances Turned Off: " + appCount); System.out.println(); + fileWrite(k, preWattage, totalWattage, T, locationCounter, appCount); } ///////////////////////////////////////// - //////// Printing Summary of all Time Steps + //////// Printing Summary of all Time Steps to console and file /////////////////////////////////////////////////////////// System.out.println("/////////////// Summary ///////////////"); + ArrayList brownLocation = new ArrayList<>(); int impactLocation = 0; int active = locationPoints[0]; int smart = 0; for (int i = 0; i < locationPoints.length; ++i) { + if (locationPoints[i] == 0) { + brownLocation.add(i); + } if (active > locationPoints[i]) { active = locationPoints[i]; } @@ -261,27 +272,58 @@ public static void simulation() { } } - if (smart == 0 && active == 0) { - int brown = 0; - for (int i = 0; i < locationPoints.length; ++i) { - if (locationPoints[i] == 0) { - brown += 1; - } - } - System.out.println(brown + " locations were browned out every time for all " + timeSteps + " runs"); - - } else if (timeSteps == locationPoints[impactLocation]) { - - System.out.println("Most Impacted Location: " + locationList.get(impactLocation) + " with " + smartPoints[impactLocation] + - " total appliances set to low over the course of " + timeSteps + " runs"); - - } else { - - System.out.println("Most Impacted Location: " + locationList.get(impactLocation) + " with " + (timeSteps-locationPoints[impactLocation]) + - " total brown outs and " + smartPoints[impactLocation] + " total appliances set to low over the course of " + timeSteps + " runs" ); - - } - System.out.println(); + // Create the folder + File directory = new File(directoryPath, folderName); + directory.mkdirs(); + + // Combine the directory path and file name to create the complete file path + String filePath = directoryPath + folderName + "/" + fileName; + + try { + // Create a FileWriter object to write to the file + FileWriter writer = new FileWriter(filePath, true); + writer.write("/////////////// Summary ///////////////"); + writer.write(System.lineSeparator()); + if (smart == 0 && active == 0) { + int brown = 0; + for (int i = 0; i < locationPoints.length; ++i) { + if (locationPoints[i] == 0) { + brown += 1; + } + } + writer.write(brown + " locations were browned out every time for all " + timeSteps + " runs"); + writer.write(System.lineSeparator()); + System.out.println(brown + " locations were browned out every time for all " + timeSteps + " runs"); + System.out.println("List of those locations: "); + writer.write("List of those locations: "); + writer.write(System.lineSeparator()); + for (int i = 0; i < brownLocation.size(); ++i) { + System.out.println(locationList.get(brownLocation.get(i))); + writer.write(Long.toString(locationList.get(brownLocation.get(i)))); + writer.write(System.lineSeparator()); + } + + } else if (timeSteps == locationPoints[impactLocation]) { + writer.write("Most Impacted Location: " + locationList.get(impactLocation) + " with " + smartPoints[impactLocation] + + " total appliances set to low over the course of " + timeSteps + " runs"); + System.out.println("Most Impacted Location: " + locationList.get(impactLocation) + " with " + smartPoints[impactLocation] + + " total appliances set to low over the course of " + timeSteps + " runs"); + + } else { + writer.write("Most Impacted Location: " + locationList.get(impactLocation) + " with " + (timeSteps-locationPoints[impactLocation]) + + " total brown outs and " + smartPoints[impactLocation] + " total appliances set to low over the course of " + timeSteps + " runs"); + System.out.println("Most Impacted Location: " + locationList.get(impactLocation) + " with " + (timeSteps-locationPoints[impactLocation]) + + " total brown outs and " + smartPoints[impactLocation] + " total appliances set to low over the course of " + timeSteps + " runs" ); + } + + System.out.println(); + writer.close(); + } catch (IOException e) { + // Handle IO exception + e.printStackTrace(); + } + + } @@ -325,7 +367,18 @@ public static void main(String[] args) { //User interactive part String option1; - readAppFile("C:/PowerGrid/app.txt"); + + //clear file before use + String file = directoryPath + folderName + "/" + fileName; + try { + // Create a FileWriter object to write to the file + FileWriter writer = new FileWriter(file); + writer.close(); + } catch (IOException e) { + // Handle IO exception + e.printStackTrace(); + } + while(true){// Application menu to be displayed to the user. System.out.println("Select an option:"); System.out.println("Type \"A\" Add an appliance"); @@ -366,5 +419,4 @@ else if (option1.equals("Q")) { } } - } From 248831fe74630968b81d4c61287bcca44f046eec Mon Sep 17 00:00:00 2001 From: ElijahRosal <148596994+ElijahRosal@users.noreply.github.com> Date: Sun, 10 Dec 2023 19:23:48 -0800 Subject: [PATCH 7/7] updated some variable types --- PowerTester.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/PowerTester.java b/PowerTester.java index 277dcfe..b7d1799 100644 --- a/PowerTester.java +++ b/PowerTester.java @@ -6,7 +6,7 @@ public class PowerTester { static ArrayList wholeArea = new ArrayList<>(); - static ArrayList locationList = new ArrayList<>(); + static ArrayList locationList = new ArrayList<>(); static ArrayList> divArea = new ArrayList<>(); static ArrayList onApps = new ArrayList<>(); static ArrayList smartOnApps = new ArrayList<>(); @@ -54,7 +54,7 @@ public static void readAppFile(String file) { String placeholder = scnr.nextLine(); try (Scanner parse = new Scanner(placeholder)) { parse.useDelimiter(","); - long location = parse.nextLong(); + int location = parse.nextInt(); String name = parse.next(); int powerUse = parse.nextInt(); double prob = parse.nextDouble(); @@ -111,7 +111,7 @@ public static void simulation() { for ( int k = 0; k < timeSteps; ++k ) { divArea.clear(); - ArrayList totalLocations = (ArrayList) locationList.clone(); + ArrayList totalLocations = (ArrayList) locationList.clone(); int totalWattage = 0; int locationCounter = 0; int T = 0; @@ -224,7 +224,7 @@ public static void simulation() { for (int i = 0; i < locationList.size(); ++i) { - long location = locationList.get(i); + int location = locationList.get(i); for (int h = 0; h < smartOnApps.size(); ++h) { if (smartOnApps.get(h).getOnLow() && smartOnApps.get(h).getLocation() == location) { smartPoints[i] += 1; @@ -299,7 +299,7 @@ public static void simulation() { writer.write(System.lineSeparator()); for (int i = 0; i < brownLocation.size(); ++i) { System.out.println(locationList.get(brownLocation.get(i))); - writer.write(Long.toString(locationList.get(brownLocation.get(i)))); + writer.write(Integer.toString(locationList.get(brownLocation.get(i)))); writer.write(System.lineSeparator()); } @@ -331,7 +331,7 @@ public static void addApplication() { System.out.println("Type \"true\" if the appliance is smart:"); boolean type = scnr.nextBoolean(); System.out.println("Enter the appliance's location ID:"); - long location = scnr.nextLong(); + int location = scnr.nextInt(); System.out.println("Enter the name of the appliance:"); String name = scnr.nextLine(); System.out.println("Enter the appliance's watt usage:");