From c97d42893570888081a9344ab8ad0aa7eb5f40fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= <63239207+Klagarge@users.noreply.github.com> Date: Sun, 11 Jun 2023 18:34:35 +0200 Subject: [PATCH] Do a bit SmartControler but regulation won't really work --- .../java/ch/hevs/isi/MinecraftController.java | 13 +- src/main/java/ch/hevs/isi/SmartControl.java | 177 ++++++++++++------ 2 files changed, 128 insertions(+), 62 deletions(-) diff --git a/src/main/java/ch/hevs/isi/MinecraftController.java b/src/main/java/ch/hevs/isi/MinecraftController.java index 66da3ec..a01547e 100644 --- a/src/main/java/ch/hevs/isi/MinecraftController.java +++ b/src/main/java/ch/hevs/isi/MinecraftController.java @@ -1,5 +1,7 @@ package ch.hevs.isi; +import ch.hevs.isi.core.DataPoint; +import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.db.DatabaseConnector; import ch.hevs.isi.field.FieldConnector; import ch.hevs.isi.utils.Utility; @@ -124,9 +126,16 @@ public class MinecraftController { } if(!stateGrid) { System.out.println("Grid is not fine"); - System.exit(0); + //System.exit(0); } else { - System.out.println("Grid is fine"); + //System.out.println("Grid is fine"); + } + FloatDataPoint score = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("SCORE"); + if(score != null) { + if(score.getValue() >= 100) { + System.out.println("Game finished with score: " + score.getValue()); + System.exit(0); + } } Thread.sleep(SmartControl.TIME_BETWEEN_UPDATES); } diff --git a/src/main/java/ch/hevs/isi/SmartControl.java b/src/main/java/ch/hevs/isi/SmartControl.java index 5b39be3..c63209d 100644 --- a/src/main/java/ch/hevs/isi/SmartControl.java +++ b/src/main/java/ch/hevs/isi/SmartControl.java @@ -6,10 +6,12 @@ import ch.hevs.isi.core.FloatDataPoint; public class SmartControl { - private static final float BATTERY_LEVEL_LOW = 0.1f; - private static final float BATTERY_LEVEL_HIGH = 0.9f; - private static final float BATTERY_POWER_MAX = 2500f; + private static final float BATTERY_LEVEL_LOW = 0.4f; + private static final float BATTERY_LEVEL_HIGH = 0.85f; + private static final float BATTERY_POWER_MAX = 1000f; private static final float COAL_AMOUNT_DAY = (1f-0.2f)/3f; + private static final float COAL_POWER_MAX = 500f; + private static final float FACTORY_POWER_MAX = 1000f; private static final float GRID_VOLTAGE_MIN = 750f; private static final float GRID_VOLTAGE_MAX = 900f; private static final float GRID_VOLTAGE_NOMINAL = 800f; @@ -17,7 +19,7 @@ public class SmartControl { private static final float PV_PANEL_NOMINAL = 1000f; private static final float PV_PANEL_NOMINAL_RAINING = 500f; - public static final long TIME_BETWEEN_UPDATES = 10; + public static final long TIME_BETWEEN_UPDATES = 20; @@ -30,21 +32,6 @@ public class SmartControl { private FloatDataPoint coalAmount; - private void defineDataPoints(){ - clock = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("CLOCK_FLOAT"); - battChrg = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("BATT_CHRG_FLOAT"); - battPwrIn = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("BATT_P_FLOAT"); - remoteCoalSp = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_COAL_SP"); - remoteFactorySp = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_FACTORY_SP"); - coalAmount = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("COAL_AMOUNT"); - gridVoltage = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("GRID_U_FLOAT"); - } - - float getValue(FloatDataPoint fdp){ - if(fdp == null) return 0f; - return fdp.getValue(); - } - // Emergency data points private FloatDataPoint gridVoltage; @@ -53,16 +40,45 @@ public class SmartControl { private float igpCoalAmount = 0f; // Time variables - private int day = 0; - private float previousTime; - final float kpiFactory = 1f; - final float kpiCoal = 1f; - + private int day = 2; // TODO : Change to 1 + private boolean dayChanged = false; + private float previousCoalAmount = 0f; + private float initialTime; + final float kpiFactory = 0.5f; + final float kpiCoal = 0.5f; + final float kpiCoalBatt = 0.8f; + private void defineDataPoints(){ + do { + try { + Thread.sleep(20); + } catch (InterruptedException e) { + e.printStackTrace(); + } + clock = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("CLOCK_FLOAT"); + battChrg = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("BATT_CHRG_FLOAT"); + battPwrIn = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("BATT_P_FLOAT"); + remoteCoalSp = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_COAL_SP"); + remoteFactorySp = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_FACTORY_SP"); + coalAmount = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("COAL_AMOUNT"); + gridVoltage = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("GRID_U_FLOAT"); + System.out.println("Test if data points are defined"); + } while (!testIfDataPointsAreDefined()); + } + private boolean testIfDataPointsAreDefined(){ + if(clock == null) return false; + if(battChrg == null) return false; + if(battPwrIn == null) return false; + if(remoteCoalSp == null) return false; + if(remoteFactorySp == null) return false; + if(coalAmount == null) return false; + if(gridVoltage == null) return false; + return true; + } private float getTimeOfTheDay(){ - float timeOfTheDay = getValue(clock); + float timeOfTheDay = clock.getValue(); timeOfTheDay %= 1; timeOfTheDay *= 24; return timeOfTheDay; @@ -74,9 +90,9 @@ public class SmartControl { FloatDataPoint wind = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("WIND_P_FLOAT"); FloatDataPoint coal = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("COAL_P_FLOAT"); - producerPower += getValue(solar); - producerPower += getValue(wind); - producerPower += getValue(coal); + producerPower += solar.getValue(); + producerPower += wind.getValue(); + producerPower += coal.getValue(); return producerPower; } @@ -87,9 +103,9 @@ public class SmartControl { FloatDataPoint factory = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("FACTORY_P_FLOAT"); FloatDataPoint bunker = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("BUNKER_P_FLOAT"); - consumerPower += getValue(home); - consumerPower += getValue(factory); - consumerPower += getValue(bunker); + consumerPower += home.getValue(); + consumerPower += factory.getValue(); + consumerPower += bunker.getValue(); return consumerPower; } @@ -97,23 +113,23 @@ public class SmartControl { boolean gridIsFine = true; // Grid voltage is too high - if(getValue(gridVoltage) > (GRID_VOLTAGE_MAX-GRID_VOLTAGE_TOLERANCE)) { + if(gridVoltage.getValue() > (GRID_VOLTAGE_MAX-GRID_VOLTAGE_TOLERANCE)) { gridIsFine = false; new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(false); new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(false); remoteCoalSp.setValue(0f); - if(getValue(gridVoltage) > GRID_VOLTAGE_MAX) { + if(gridVoltage.getValue() > GRID_VOLTAGE_MAX) { remoteFactorySp.setValue(1f); } } // Grid voltage is too low - if(getValue(gridVoltage) < (GRID_VOLTAGE_MIN+GRID_VOLTAGE_TOLERANCE)) { + if(gridVoltage.getValue() < (GRID_VOLTAGE_MIN+GRID_VOLTAGE_TOLERANCE)) { gridIsFine = false; new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(true); new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(true); remoteCoalSp.setValue(1f); - if(getValue(gridVoltage) < GRID_VOLTAGE_MIN) { + if(gridVoltage.getValue() < GRID_VOLTAGE_MIN) { remoteFactorySp.setValue(0f); } } @@ -176,57 +192,96 @@ public class SmartControl { default: break; } - igpCoalAmountForTheDay += day; + igpCoalAmountForTheDay += (3-day); igpCoalAmountForTheDay /= 3f; + igpCoalAmount = igpCoalAmountForTheDay; } public SmartControl() { - defineDataPoints(); - - previousTime = getTimeOfTheDay(); // start the solar and wind power plants new BooleanDataPoint("REMOTE_SOLAR_SW", true).setValue(true); new BooleanDataPoint("REMOTE_WIND_SW", true).setValue(true); + new FloatDataPoint("REMOTE_COAL_SP", true).setValue(0f); + new FloatDataPoint("REMOTE_FACTORY_SP", true).setValue(0f); + defineDataPoints(); + + initialTime = getTimeOfTheDay(); } public boolean run() { - defineDataPoints(); - float spCoal = getValue(remoteCoalSp); - float spFactory = getValue(remoteFactorySp); + float spCoal = remoteCoalSp.getValue(); + float spFactory = remoteFactorySp.getValue(); - if (getTimeOfTheDay() < previousTime) { - previousTime = getTimeOfTheDay(); + if (getTimeOfTheDay() < 1f && !dayChanged) { + dayChanged = true; day++; - day %= 3; + } + if (getPeriodOfDay() > 23f) { + dayChanged = false; + } + if(coalAmount.getValue() > previousCoalAmount) { + day = 1; } defineIGPbatteryLevel(); defineIGPcoalAmount(day); - if (getValue(battChrg) < igpBatteryLevel) { - spFactory -= (igpBatteryLevel - getValue(battChrg)) * kpiFactory; - spCoal += (igpBatteryLevel - getValue(battChrg)) * kpiCoal; + new FloatDataPoint("IGP_BATTERY_LEVEL", true).setValue(igpBatteryLevel); + new FloatDataPoint("IGP_COAL_AMOUNT", true).setValue(igpCoalAmount); + + String debug = "Day " + day + " - "; + //debug += "IGP battery level: " + igpBatteryLevel; + //debug += " IGP coal amount: " + igpCoalAmount; + // debug += " - "; + + + float battDiff = battChrg.getValue() - igpBatteryLevel; + new FloatDataPoint("BATT_DIFF", true).setValue(battDiff); + debug += "Batt. diff.: " + battDiff + " - "; + + float igpEnergyBattery = battDiff * BATTERY_POWER_MAX / 0.4f; // Over energy in battery + new FloatDataPoint("IGP_ENERGY_BATTERY", true).setValue(igpEnergyBattery); + debug += "IGP en. batt.: " + igpEnergyBattery + " - "; + + float powerDiff = getProducerPower() - getConsumerPower(); + new FloatDataPoint("POWER_DIFF", true).setValue(powerDiff); + debug += "Power diff: " + powerDiff + " - "; + + float balance = powerDiff + igpEnergyBattery; // Energy to use + new FloatDataPoint("BALANCE", true).setValue(balance); + debug += "Balance: " + balance + " - "; + + float coalDiff = coalAmount.getValue() - igpCoalAmount; // Over coal in storage + new FloatDataPoint("COAL_DIFF", true).setValue(coalDiff); + //debug += "Coal diff.: " + coalDiff + " - "; + + + if(balance < 0){ // Produce more energy or consume less + if(coalDiff > 0){ // Produce more energy + spCoal += kpiCoal * (balance / COAL_POWER_MAX); + debug += "Produce more energy"; + } else { // Consume less energy + spFactory -= kpiFactory * (balance / FACTORY_POWER_MAX); + debug += "Consume less energy"; + } + } else { // Produce less energy or consume more + if(coalDiff > 0){ // Produce less energy + spCoal -= kpiCoal * (balance / COAL_POWER_MAX); + debug += "Produce less energy"; + } else { // Consume more energy + spFactory += kpiFactory * (balance / FACTORY_POWER_MAX); + debug += "Consume more energy"; + } + } - if (getValue(battChrg) > igpBatteryLevel) { - spFactory += (getValue(battChrg) - igpBatteryLevel) * kpiFactory; - spCoal -= (getValue(battChrg) - igpBatteryLevel) * kpiCoal; - } - if (getValue(coalAmount) < igpCoalAmount) { - spFactory -= (igpCoalAmount - getValue(coalAmount)) * kpiFactory; - spCoal += (igpCoalAmount - getValue(coalAmount)) * kpiCoal; - } - if (getValue(coalAmount) > igpCoalAmount) { - spFactory += (getValue(coalAmount) - igpCoalAmount) * kpiFactory; - spCoal -= (getValue(coalAmount) - igpCoalAmount) * kpiCoal; - } if (spFactory > 1f) spFactory = 1f; if (spFactory < 0f) spFactory = 0f; @@ -236,6 +291,8 @@ public class SmartControl { remoteFactorySp.setValue(spFactory); remoteCoalSp.setValue(spCoal); + System.out.println(debug); + return checkIfGridIsFine(); } }