Skip to content

Commit bf799d6

Browse files
authored
Merge pull request #205 from Zenmo/serialisation_with_private_fields
Serialisation with private fields
2 parents ee32e3f + 2e3e29e commit bf799d6

File tree

14 files changed

+226
-115
lines changed

14 files changed

+226
-115
lines changed

_alp/Agents/EnergyCoop/Variables.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,7 @@
20362036
<PublicFlag>false</PublicFlag>
20372037
<PresentationFlag>true</PresentationFlag>
20382038
<ShowLabel>true</ShowLabel>
2039-
<Properties SaveInSnapshot="true" AccessType="public" StaticVariable="false">
2039+
<Properties SaveInSnapshot="true" AccessType="default" StaticVariable="false">
20402040
<CollectionClass>ArrayList</CollectionClass>
20412041
<ElementClass>Actor</ElementClass>
20422042
<ValueElementClass>Object</ValueElementClass>
@@ -2054,7 +2054,7 @@
20542054
<PublicFlag>false</PublicFlag>
20552055
<PresentationFlag>true</PresentationFlag>
20562056
<ShowLabel>true</ShowLabel>
2057-
<Properties SaveInSnapshot="true" AccessType="public" StaticVariable="false">
2057+
<Properties SaveInSnapshot="true" AccessType="default" StaticVariable="false">
20582058
<CollectionClass>ArrayList</CollectionClass>
20592059
<ElementClass>Actor</ElementClass>
20602060
<ValueElementClass>Object</ValueElementClass>
@@ -2072,7 +2072,7 @@
20722072
<PublicFlag>false</PublicFlag>
20732073
<PresentationFlag>true</PresentationFlag>
20742074
<ShowLabel>true</ShowLabel>
2075-
<Properties SaveInSnapshot="true" AccessType="public" StaticVariable="false">
2075+
<Properties SaveInSnapshot="true" AccessType="private" StaticVariable="false">
20762076
<CollectionClass>ArrayList</CollectionClass>
20772077
<ElementClass>GridConnection</ElementClass>
20782078
<ValueElementClass>Object</ValueElementClass>
@@ -2090,7 +2090,7 @@
20902090
<PublicFlag>false</PublicFlag>
20912091
<PresentationFlag>true</PresentationFlag>
20922092
<ShowLabel>true</ShowLabel>
2093-
<Properties SaveInSnapshot="true" AccessType="public" StaticVariable="false">
2093+
<Properties SaveInSnapshot="true" AccessType="private" StaticVariable="false">
20942094
<CollectionClass>ArrayList</CollectionClass>
20952095
<ElementClass>GridConnection</ElementClass>
20962096
<ValueElementClass>Object</ValueElementClass>

_alp/Agents/EnergyModel/Code/Functions.java

Lines changed: 131 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,16 @@
401401
}
402402
}
403403

404-
f_setInitialValues();
405-
406404
v_isRapidRun = false;
407405

408406
double duration = System.currentTimeMillis() - startTime1;
409407

410408
traceln("*** headless run duration: "+ duration/1000 + " s ***");
411409

412410
traceln("Live-sim t_h after rapidRun: %s", t_h);
411+
if (b_isDeserialised) {
412+
traceln("Anylogic Model time(HOUR): %s", time(HOUR));
413+
}
413414
c_profiles.forEach(p -> p.updateValue(t_h));
414415
c_forecasts.forEach(p -> p.initializeForecast(t_h));
415416

@@ -495,11 +496,17 @@
495496
double topLevelElectricGridCapacity_kW = 0;
496497
boolean topLevelGridCapacitiesKnown = true;
497498

498-
// First make all links between GridNodes
499+
// First make all links between GridNodes
499500
for( GridNode GN : pop_gridNodes ) {
500501
GN.f_connectToParentNode();
501502
}
502503

504+
// First clear lists (needed after deserialisation)
505+
c_gridNodeExecutionList.clear();
506+
c_gridNodeExecutionListReverse.clear();
507+
c_gridNodesTopLevel.clear();
508+
c_gridNodesNotTopLevel.clear();
509+
503510
// Then build execution order list
504511
for( GridNode GN : pop_gridNodes ) {
505512
GridNode parentNode = findFirst(pop_gridNodes, p->p.p_gridNodeID.equals(GN.p_parentNodeID)); // Works as long as p_gridNodeID is not null. p_parentNodeID can be null no problemo.
@@ -524,6 +531,7 @@
524531
c_gridNodeExecutionListReverse = c_gridNodeExecutionList;
525532
Collections.reverse(c_gridNodeExecutionList);
526533

534+
527535
//Set cumulative toplevel grid values as energyModel values
528536
v_liveConnectionMetaData.physicalCapacity_kW = topLevelElectricGridCapacity_kW;
529537
v_liveConnectionMetaData.contractedDeliveryCapacity_kW = topLevelElectricGridCapacity_kW;
@@ -563,15 +571,15 @@ ArrayList<GridConnection> f_getActiveGridConnections()
563571
//v_hourOfYearStart=hourOfYearPerMonth[getMonth()] + (getDayOfMonth()-1)*24;
564572
t_h = p_runStartTime_h;
565573

574+
Date startDate = date();
575+
p_year = startDate.getYear() + 1900;
576+
566577
LocalDate localDate = LocalDate.of(p_year, 1, 1);
567578
v_dayOfWeek1jan = DayOfWeek.from(localDate).getValue();
568579
p_startOfWinterWeek_h = roundToInt(24 * (p_winterWeekNumber * 7 + (8-v_dayOfWeek1jan)%7)); // Week 49 is winterweek.
569580
p_startOfSummerWeek_h = roundToInt(24 * (p_summerWeekNumber * 7 + (8-v_dayOfWeek1jan)%7)); // Week 18 is summerweek.
570581

571582

572-
Date startDate = date();
573-
574-
p_year = startDate.getYear() + 1900;
575583

576584
int monthIdx = 0;
577585
while ( t_h > hourOfYearPerMonth[monthIdx] ) {
@@ -586,6 +594,8 @@ ArrayList<GridConnection> f_getActiveGridConnections()
586594
traceln("Month of year start: %s", monthIdx);
587595
startDate.setMonth(monthIdx);
588596
startDate.setDate(dayOfMonth);
597+
startDate.setHours(0);
598+
startDate.setMinutes(0);
589599
traceln("Startdate: %s", startDate);
590600
//startDate.set
591601
getExperiment().getEngine().setStartDate(startDate);
@@ -639,10 +649,6 @@ ArrayList<GridConnection> f_getActiveGridConnections()
639649
b_parallelizeConnectionOwners = true;
640650
}
641651

642-
// set initial values
643-
f_setInitialValues();
644-
645-
646652
b_isInitialized = true;
647653
/*ALCODEEND*/}
648654

@@ -671,14 +677,6 @@ ArrayList<GridNode> f_getGridNodesNotTopLevel()
671677
return this.c_gridNodesNotTopLevel;
672678
/*ALCODEEND*/}
673679

674-
double f_setInitialValues()
675-
{/*ALCODESTART::1722853692644*/
676-
// Starting prices
677-
//c_gridConnections.forEach(GC -> GC.v_electricityPriceLowPassed_eurpkWh = c_gridNodesTopLevel.get(0).v_currentParentNodalPrice_eurpkWh); // Initialize filtered prices for gridConnections, hoping to prevent or reduce initial settling excursions
678-
679-
680-
/*ALCODEEND*/}
681-
682680
double f_initializePause()
683681
{/*ALCODESTART::1722590514591*/
684682
for (GridConnection GC : UtilityConnections) {
@@ -1040,15 +1038,15 @@ EnergyCoop f_addProductionEnergyCarrier(OL_EnergyCarriers EC)
10401038
double f_startAfterDeserialisation()
10411039
{/*ALCODESTART::1753963201170*/
10421040
// Reconstruct the LiveData class
1043-
v_liveData = new J_LiveData(this);
1041+
/*v_liveData = new J_LiveData(this);
10441042
v_liveData.activeEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10451043
v_liveData.activeProductionEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10461044
v_liveData.activeConsumptionEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10471045
10481046
//v_liveConnectionMetaData = new J_ConnectionMetaData(this);
10491047
//v_liveAssetsMetaData = new J_AssetsMetaData(this);
10501048
v_liveData.connectionMetaData = v_liveConnectionMetaData;
1051-
v_liveData.assetsMetaData = v_liveAssetsMetaData;
1049+
v_liveData.assetsMetaData = v_liveAssetsMetaData;*/
10521050

10531051
v_liveData.resetLiveDatasets(p_runStartTime_h, p_runStartTime_h, p_timeStep_h);
10541052

@@ -1059,14 +1057,14 @@ EnergyCoop f_addProductionEnergyCarrier(OL_EnergyCarriers EC)
10591057

10601058
// Reconstruct the LiveData class in the EnergyCoops
10611059
for (EnergyCoop ec : pop_energyCoops) {
1062-
ec.v_liveData = new J_LiveData(ec);
1060+
/*ec.v_liveData = new J_LiveData(ec);
10631061
ec.v_liveData.activeEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10641062
ec.v_liveData.activeProductionEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10651063
ec.v_liveData.activeConsumptionEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10661064
10671065
ec.v_liveData.connectionMetaData = ec.v_liveConnectionMetaData;
10681066
ec.v_liveData.assetsMetaData = ec.v_liveAssetsMetaData;
1069-
1067+
*/
10701068
ec.v_liveData.resetLiveDatasets(p_runStartTime_h, p_runStartTime_h, p_timeStep_h);
10711069

10721070
ec.fm_currentProductionFlows_kW = new J_FlowsMap();
@@ -1079,35 +1077,62 @@ EnergyCoop f_addProductionEnergyCarrier(OL_EnergyCarriers EC)
10791077
List<GridConnection> allGridConnections = new ArrayList<>(c_gridConnections);
10801078
allGridConnections.addAll(c_pausedGridConnections);
10811079
for (GridConnection gc : allGridConnections) {
1082-
gc.v_liveData = new J_LiveData(gc);
1080+
/*gc.v_liveData = new J_LiveData(gc);
10831081
gc.v_liveData.activeEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10841082
gc.v_liveData.activeProductionEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10851083
gc.v_liveData.activeConsumptionEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY);
10861084
10871085
gc.v_liveData.connectionMetaData = gc.v_liveConnectionMetaData;
10881086
gc.v_liveData.assetsMetaData = gc.v_liveAssetsMetaData;
1089-
1087+
*/
10901088
gc.v_liveData.resetLiveDatasets(p_runStartTime_h, p_runStartTime_h, p_timeStep_h);
10911089

10921090
gc.fm_currentProductionFlows_kW = new J_FlowsMap();
10931091
gc.fm_currentConsumptionFlows_kW = new J_FlowsMap();
10941092
gc.fm_currentBalanceFlows_kW = new J_FlowsMap();
10951093
gc.fm_currentAssetFlows_kW = new J_ValueMap(OL_AssetFlowCategories.class);
10961094

1097-
for (J_EA j_ea : gc.c_energyAssets) {
1095+
/*for (J_EA j_ea : gc.c_energyAssets) {
10981096
gc.f_addEnergyCarriersAndAssetCategoriesFromEA(j_ea);
1099-
}
1097+
}*/
11001098
}
11011099

1102-
//v_liveData.activeEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY, OL_EnergyCarriers.HEAT, OL_EnergyCarriers.METHANE, OL_EnergyCarriers.DIESEL);
1103-
//v_liveData.activeProductionEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY, OL_EnergyCarriers.HEAT);
1104-
//v_liveData.activeConsumptionEnergyCarriers = EnumSet.of(OL_EnergyCarriers.ELECTRICITY, OL_EnergyCarriers.HEAT, OL_EnergyCarriers.METHANE, OL_EnergyCarriers.DIESEL);
1105-
//for (J_EA j_ea : c_energyAssets) {
1106-
//v_liveData.activeProductionEnergyCarriers.addAll(j_ea.activeProductionEnergyCarriers);
1107-
//v_liveData.activeConsumptionEnergyCarriers.addAll(j_ea.activeConsumptionEnergyCarriers);
1108-
//}
1100+
// Initialize time and date
1101+
//v_hourOfYearStart=hourOfYearPerMonth[getMonth()] + (getDayOfMonth()-1)*24;
1102+
t_h = p_runStartTime_h;
1103+
1104+
Date startDate = date();
1105+
p_year = startDate.getYear() + 1900;
1106+
1107+
LocalDate localDate = LocalDate.of(p_year, 1, 1);
1108+
v_dayOfWeek1jan = DayOfWeek.from(localDate).getValue();
1109+
p_startOfWinterWeek_h = roundToInt(24 * (p_winterWeekNumber * 7 + (8-v_dayOfWeek1jan)%7)); // Week 49 is winterweek.
1110+
p_startOfSummerWeek_h = roundToInt(24 * (p_summerWeekNumber * 7 + (8-v_dayOfWeek1jan)%7)); // Week 18 is summerweek.
1111+
1112+
int monthIdx = 0;
1113+
while ( t_h > hourOfYearPerMonth[monthIdx] ) {
1114+
monthIdx++;
1115+
if (monthIdx==hourOfYearPerMonth.length){
1116+
break;
1117+
}
1118+
}
1119+
1120+
int dayOfMonth = 1+(int)((t_h - hourOfYearPerMonth[monthIdx])/24.0);
1121+
traceln("Day of month start: %s", dayOfMonth);
1122+
traceln("Month of year start: %s", monthIdx);
1123+
startDate.setMonth(monthIdx);
1124+
startDate.setDate(dayOfMonth);
1125+
startDate.setHours(0);
1126+
startDate.setMinutes(0);
1127+
traceln("Startdate: %s", startDate);
1128+
//startDate.set
1129+
getExperiment().getEngine().setStartDate(startDate);
11091130

1131+
f_initializeForecasts();
1132+
1133+
f_initializeLiveDataSets();
11101134

1135+
b_isDeserialised = true;
11111136
/*ALCODEEND*/}
11121137

11131138
Pair<J_DataSetMap, J_DataSetMap> f_getPeakWeekDataSets()
@@ -1235,3 +1260,76 @@ Date f_getDate()
12351260
c_pausedGridConnections.forEach(GC -> GC.v_liveData.clearLiveDatasets());
12361261
/*ALCODEEND*/}
12371262

1263+
double f_initializeEngineAfterLoad()
1264+
{/*ALCODESTART::1758792939882*/
1265+
// Initialize time and date
1266+
//v_hourOfYearStart=hourOfYearPerMonth[getMonth()] + (getDayOfMonth()-1)*24;
1267+
t_h = p_runStartTime_h;
1268+
1269+
Date startDate = date();
1270+
p_year = startDate.getYear() + 1900;
1271+
1272+
LocalDate localDate = LocalDate.of(p_year, 1, 1);
1273+
v_dayOfWeek1jan = DayOfWeek.from(localDate).getValue();
1274+
p_startOfWinterWeek_h = roundToInt(24 * (p_winterWeekNumber * 7 + (8-v_dayOfWeek1jan)%7)); // Week 49 is winterweek.
1275+
p_startOfSummerWeek_h = roundToInt(24 * (p_summerWeekNumber * 7 + (8-v_dayOfWeek1jan)%7)); // Week 18 is summerweek.
1276+
1277+
int monthIdx = 0;
1278+
while ( t_h > hourOfYearPerMonth[monthIdx] ) {
1279+
monthIdx++;
1280+
if (monthIdx==hourOfYearPerMonth.length){
1281+
break;
1282+
}
1283+
}
1284+
1285+
int dayOfMonth = 1+(int)((t_h - hourOfYearPerMonth[monthIdx])/24.0);
1286+
traceln("Day of month start: %s", dayOfMonth);
1287+
traceln("Month of year start: %s", monthIdx);
1288+
startDate.setMonth(monthIdx);
1289+
startDate.setDate(dayOfMonth);
1290+
startDate.setHours(0);
1291+
startDate.setMinutes(0);
1292+
traceln("Startdate: %s", startDate);
1293+
//startDate.set
1294+
getExperiment().getEngine().setStartDate(startDate);
1295+
1296+
1297+
1298+
// Initialize all agents in the correct order, creating all connections. What about setting initial values? And how about repeated simulations?
1299+
1300+
/*f_buildGridNodeTree();
1301+
c_gridConnections.forEach(GC -> GC.f_initialize());
1302+
1303+
// Only relevant for deserialisation:
1304+
c_pausedGridConnections.forEach(GC -> GC.f_initialize());
1305+
1306+
pop_connectionOwners.forEach(CO -> CO.f_initialize());
1307+
pop_energyCoops.forEach(EC -> EC.f_initialize()); // Not yet robust when there is no supplier initialized!
1308+
1309+
1310+
1311+
// Initializing Live Data Class
1312+
v_liveAssetsMetaData.updateActiveAssetData(c_gridConnections);
1313+
for (GridConnection GC : c_gridConnections) {
1314+
v_liveData.activeEnergyCarriers.addAll(GC.v_liveData.activeEnergyCarriers);
1315+
v_liveData.activeConsumptionEnergyCarriers.addAll(GC.v_liveData.activeConsumptionEnergyCarriers);
1316+
v_liveData.activeProductionEnergyCarriers.addAll(GC.v_liveData.activeProductionEnergyCarriers);
1317+
}
1318+
1319+
// Loop over populations to check v_ispaused
1320+
f_initializePause();
1321+
1322+
for (GridNode GN : c_gridNodeExecutionList) {
1323+
GN.f_initializeGridnode();
1324+
}
1325+
1326+
v_liveData.connectionMetaData.contractedDeliveryCapacityKnown = false;
1327+
v_liveData.connectionMetaData.contractedFeedinCapacityKnown = false;
1328+
v_liveData.connectionMetaData.physicalCapacityKnown = false;
1329+
*/
1330+
f_initializeForecasts();
1331+
1332+
f_initializeLiveDataSets();
1333+
1334+
/*ALCODEEND*/}
1335+

_alp/Agents/EnergyModel/Code/Functions.xml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -348,23 +348,6 @@
348348
<ShowLabel>true</ShowLabel>
349349
<Body xmlns:al="http://anylogic.com"/>
350350
</Function>
351-
<Function AccessType="private" StaticFunction="false">
352-
<ReturnModificator>VOID</ReturnModificator>
353-
<ReturnType>double</ReturnType>
354-
<Id>1722853692644</Id>
355-
<Name><![CDATA[f_setInitialValues]]></Name>
356-
<Description><![CDATA[Function to set initial states at start of simulation.]]></Description>
357-
<X>730</X>
358-
<Y>173</Y>
359-
<Label>
360-
<X>10</X>
361-
<Y>0</Y>
362-
</Label>
363-
<PublicFlag>false</PublicFlag>
364-
<PresentationFlag>true</PresentationFlag>
365-
<ShowLabel>true</ShowLabel>
366-
<Body xmlns:al="http://anylogic.com"/>
367-
</Function>
368351
<Function AccessType="default" StaticFunction="false">
369352
<ReturnModificator>VOID</ReturnModificator>
370353
<ReturnType>double</ReturnType>
@@ -603,8 +586,8 @@
603586
<ReturnType>double</ReturnType>
604587
<Id>1753963201170</Id>
605588
<Name><![CDATA[f_startAfterDeserialisation]]></Name>
606-
<X>1096</X>
607-
<Y>-64</Y>
589+
<X>860</X>
590+
<Y>50</Y>
608591
<Label>
609592
<X>10</X>
610593
<Y>0</Y>
@@ -746,4 +729,21 @@
746729
<ShowLabel>true</ShowLabel>
747730
<Body xmlns:al="http://anylogic.com"/>
748731
</Function>
732+
<Function AccessType="public" StaticFunction="false">
733+
<ReturnModificator>VOID</ReturnModificator>
734+
<ReturnType>double</ReturnType>
735+
<Id>1758792939882</Id>
736+
<Name><![CDATA[f_initializeEngineAfterLoad]]></Name>
737+
<ExcludeFromBuild>true</ExcludeFromBuild>
738+
<X>870</X>
739+
<Y>-30</Y>
740+
<Label>
741+
<X>10</X>
742+
<Y>0</Y>
743+
</Label>
744+
<PublicFlag>false</PublicFlag>
745+
<PresentationFlag>true</PresentationFlag>
746+
<ShowLabel>true</ShowLabel>
747+
<Body xmlns:al="http://anylogic.com"/>
748+
</Function>
749749
</Functions>

0 commit comments

Comments
 (0)