Commit 171a86f1 authored by Jan Mayer's avatar Jan Mayer
Browse files

Merge branch 'dev-branch' into 'main'

resolved sim-longer-than-profile bug

See merge request !4
parents ac9966a0 db6f65fd
Pipeline #34215 passed with stages
in 1 minute and 51 seconds
......@@ -69,15 +69,16 @@ public class CSVConsumer extends Consumer implements CurrentTimeStepSubscriber {
*/
@Override
public List<Double> getHeatProfile(int timeStep, int mpcHorizon) {
return heatProfile.subList(timeStep, timeStep + mpcHorizon);
return heatProfile.subList(timeStep % heatProfile.size(), timeStep % heatProfile.size() + mpcHorizon);
}
/**
* @return Electricity profile
*/
@Override
public List<Double> getElectricityProfile(int timeStep, int mpcHorizon) {
return electricityProfile.subList(timeStep, timeStep + mpcHorizon);
return electricityProfile.subList(timeStep % electricityProfile.size(), timeStep % electricityProfile.size() + mpcHorizon);
}
/**
......@@ -85,25 +86,7 @@ public class CSVConsumer extends Consumer implements CurrentTimeStepSubscriber {
* replaced into a distinct init helper class.
*/
private void setProfiles(String csvFile) {
try {
FileManager fm = new FileManager();
if (csvFile.isEmpty()) {
OriginalCSVHandler ocsv;
csvFile = "CONSUMPTIONEXAMPLE0"; // Necessary in case parsing fails
ocsv = new OriginalCSVHandler(fm.getBuffer(csvFile), topologyConfig);
electricityProfile = ocsv.getElectricityProfile();
heatProfile = ocsv.getHeatProfile();
} else {
// We handle the original way (old data format) first
BufferedReader br = fm.getBuffer(csvFile);
OriginalCSVHandler ocsv;
ocsv = new OriginalCSVHandler(br, topologyConfig);
electricityProfile = ocsv.getElectricityProfile();
heatProfile = ocsv.getHeatProfile();
}
} catch (IOException | ParseException e) {
System.out.println("Reading new format of " + csvFile);
try {
// If the first reader does not work, we try a second format style, that is
// specified as another scenario
......@@ -112,6 +95,7 @@ public class CSVConsumer extends Consumer implements CurrentTimeStepSubscriber {
BufferedReader br = fm.getBuffer(csvFile);
String[] columnNames = new String[] { "Heat", "Electricity" };
TimedData timedConsumerData = new TimedData(br, columnNames);
// Interpolation
TimedDataHandler tdh = new TimedDataHandler(timedConsumerData, topologyConfig, columnNames);
electricityProfile = tdh.get(columnNames[0]);
heatProfile = tdh.get(columnNames[1]);
......@@ -121,8 +105,17 @@ public class CSVConsumer extends Consumer implements CurrentTimeStepSubscriber {
// We append the first part of the profile again to the end to make sure that
// MPC data is available.
int requiredProfileWithMPC = topologyConfig.getNrSteps() + topologyConfig.getNrStepsMPC() - 1;
int requiredProfileWithMPC = topologyConfig.getNrSteps();
System.out.println(" required == " + requiredProfileWithMPC);
if (electricityProfile.size() < requiredProfileWithMPC) {
double scaler = requiredProfileWithMPC / electricityProfile.size();
System.out.format("The simulated period is %.2f times longer than the input demand profile. "
+ "The profile will be duplicated to match the simulation period. %n", scaler);
for (int k = 0; k < (int)Math.ceil(scaler); k++) {
electricityProfile.addAll(electricityProfile);
heatProfile.addAll(heatProfile);
}
electricityProfile.addAll(electricityProfile.subList(0, topologyConfig.getNrStepsMPC()));
heatProfile.addAll(heatProfile.subList(0, topologyConfig.getNrStepsMPC()));
}
......@@ -130,11 +123,9 @@ public class CSVConsumer extends Consumer implements CurrentTimeStepSubscriber {
} catch (Exception e2) {
SimulationProgress.getInstance().setStatus(Status.ERROR, getClass() + " - wrong format");
SimulationStarter.stopSimulationStatic();
e.printStackTrace();
e2.printStackTrace();
}
}
}
@Override
public void update(int currentTimeStep) {
......
......@@ -54,13 +54,18 @@ public class OriginalCSVHandler {
*/
// Read-in of consumption values for every minute (in the example files)
while ((row = br.readLine()) != null) {
// && !((row = br.readLine()).startsWith("#"))
while ((row = br.readLine()) != null ) {
// get values
buffer = row.split(";");
consumptionBuffer[0] += nf.parse(buffer[0]).doubleValue() / stepLengthInMinutes;
consumptionBuffer[1] += nf.parse(buffer[1]).doubleValue() / stepLengthInMinutes;
rowIndex++;
System.out.println(br.readLine());
// Sum up consumption over the number of minutes per time step
if ((rowIndex >= (k + 1) * stepLengthInMinutes) && (buffer.length != 0)) {
for (int j = 0; j < buffer.length; j++) {
......@@ -79,7 +84,9 @@ public class OriginalCSVHandler {
}
br.close();
// Calculate the consumption for one day longer than necessary because of MPC
// horizon
// the heat profile of one day is copied for n_days; ( k = N_STEPS/N_Days )
......
......@@ -37,6 +37,8 @@ public class TimedDataHandler extends LinkedHashMap<String, ArrayList<Double>> {
long duration = Duration.between(start, end).toMillis() / 1000;
long NSteps = duration / intervalInSeconds_long;
// System.out.println("NSteps = " + NSteps + ", duration = " + duration);
int counterOfIndexProfile = 0;
for (int i = 0; i < NSteps; i++) {
......
......@@ -29,6 +29,7 @@ public class ParametersTypeAdapter implements JsonSerializer<Parameters>, JsonDe
int simulationSteps = obj.get("stepsPerDay").getAsInt();
int mpcHorizon = obj.get("mpcHorizon").getAsInt();
System.out.println("MPC read from file:" + mpcHorizon );
int days = obj.get("days").getAsInt();
String optCriteria = obj.get("optCriteria").getAsString();
String optimizer = obj.get("optimizer").getAsString();
......
......@@ -102,8 +102,7 @@ public class Parameters {
Price heatBuyingPrice, Price co2Emissions) {
setSimulationName("InteractiveMEMAP");
setStepsPerDay(simulationSteps);
// Initially setter is not called, so that the prices are not updated
this.mpcHorizon = 2;
setMPCHorizon(mpcHorizon);
setDays(days);
setOptimizer(optimizer);
setOptCriteria(optCriteria);
......@@ -142,11 +141,11 @@ public class Parameters {
public void setMPCHorizon(int mpcHorizon) {
this.mpcHorizon = mpcHorizon;
maxBuyLimit.updateMPCHorizon(mpcHorizon);
elecBuyingPrice.updateMPCHorizon(mpcHorizon);
elecSellingPrice.updateMPCHorizon(mpcHorizon);
heatBuyingPrice.updateMPCHorizon(mpcHorizon);
co2Emissions.updateMPCHorizon(mpcHorizon);
if (maxBuyLimit !=null) maxBuyLimit.updateMPCHorizon(mpcHorizon);
if (elecBuyingPrice !=null) elecBuyingPrice.updateMPCHorizon(mpcHorizon);
if (elecSellingPrice !=null) elecSellingPrice.updateMPCHorizon(mpcHorizon);
if (heatBuyingPrice !=null) heatBuyingPrice.updateMPCHorizon(mpcHorizon);
if (co2Emissions !=null) co2Emissions.updateMPCHorizon(mpcHorizon);
setSaved(false);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment