Skip to content

Commit 11b4ddc

Browse files
phiedwbqth29
andauthored
fix broken test on windows (#1507)
* add some logs to debug windows Signed-off-by: Philippe Edwards <philippe.edwards@rte-france.com> * Revert "add some logs to debug windows" This reverts commit a46e2fb. * add some logs to debug windows Signed-off-by: Philippe Edwards <philippe.edwards@rte-france.com> * only keep actions changes Signed-off-by: Philippe Edwards <philippe.edwards@rte-france.com> * Revert "only keep actions changes" This reverts commit 96b0715. * rerun all, add stack trace print Signed-off-by: Philippe Edwards <philippe.edwards@rte-france.com> * remove try catch, keep logs Signed-off-by: Philippe Edwards <philippe.edwards@rte-france.com> * remove prints Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> * remove one line of testing Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> * print contingencies details Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> * print contingencies details 2 Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> * prints everywhere Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> * add prints Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> * change prints Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> * change test init Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> --------- Signed-off-by: Philippe Edwards <philippe.edwards@rte-france.com> Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com> Co-authored-by: Thomas Bouquet <thomas.bouquet@rte-france.com>
1 parent caaf7b5 commit 11b4ddc

2 files changed

Lines changed: 101 additions & 93 deletions

File tree

ra-optimisation/search-tree-rao/src/main/java/com/powsybl/openrao/searchtreerao/commons/costevaluatorresult/SumMaxPerTimestampCostEvaluatorResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public double getCost(Set<String> contingenciesToExclude, Set<String> cnecsToExc
5353
Map<OffsetDateTime, Set<State>> statesToEvaluatePerTimestamp = new HashMap<>();
5454
Set<State> statesToEvaluateWithoutTimestamp = new HashSet<>();
5555

56-
costPerState.keySet().stream().forEach(state -> {
56+
costPerState.keySet().forEach(state -> {
5757
if (statesContingencyMustBeKept(state, contingenciesToExclude)) {
5858
Optional<OffsetDateTime> timestamp = state.getTimestamp();
5959
if (timestamp.isPresent()) {
@@ -64,8 +64,8 @@ public double getCost(Set<String> contingenciesToExclude, Set<String> cnecsToExc
6464
}
6565
});
6666

67-
return statesToEvaluatePerTimestamp.values().stream().mapToDouble(states -> states.stream().mapToDouble(state -> costPerState.get(state)).max().orElse(0)).sum()
68-
+ statesToEvaluateWithoutTimestamp.stream().mapToDouble(state -> costPerState.get(state)).max().orElse(0);
67+
return statesToEvaluatePerTimestamp.values().stream().mapToDouble(states -> states.stream().mapToDouble(costPerState::get).max().orElse(0)).sum()
68+
+ statesToEvaluateWithoutTimestamp.stream().mapToDouble(costPerState::get).max().orElse(0);
6969

7070
}
7171

ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/commons/costevaluatorresult/SumMaxPerTimestampCostEvaluatorResultTest.java

Lines changed: 98 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.mockito.Mockito;
1717

1818
import java.time.OffsetDateTime;
19+
import java.util.HashSet;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Optional;
@@ -31,99 +32,93 @@ class SumMaxPerTimestampCostEvaluatorResultTest {
3132

3233
private FlowCnec flowCnecPreventiveT1;
3334
private FlowCnec flowCnecPreventiveT2;
34-
private FlowCnec flowCnecPreventiveT3;
35+
private FlowCnec flowCnecPreventiveNoTimestamp;
3536
private FlowCnec flowCnecCurative1T1;
36-
private FlowCnec flowCnecCurative12T1;
37-
private FlowCnec flowCnecCurativeT2;
38-
private FlowCnec flowCnecCurativeT3;
39-
40-
private State preventiveStateT1;
41-
private State preventiveStateT2;
42-
private State preventiveStateT3;
43-
private State curativeStateT1;
44-
private State curativeStateT2;
45-
private State curativeStateT3;
46-
private OffsetDateTime timestamp1 = OffsetDateTime.parse("2025-02-25T15:11Z");
47-
private OffsetDateTime timestamp2 = OffsetDateTime.parse("2025-02-25T16:11Z");
37+
private FlowCnec flowCnecCurative1T2;
38+
private FlowCnec flowCnecCurative1NoTimestamp;
39+
private FlowCnec flowCnecCurative2T1;
40+
private FlowCnec flowCnecCurative2T2;
41+
private FlowCnec flowCnecCurative2NoTimestamp;
42+
private FlowCnec flowCnecCurative3T1;
43+
private FlowCnec flowCnecCurative3T2;
44+
private FlowCnec flowCnecCurative3NoTimestamp;
45+
46+
private final OffsetDateTime timestamp1 = OffsetDateTime.parse("2025-02-25T15:11Z");
47+
private final OffsetDateTime timestamp2 = OffsetDateTime.parse("2025-02-25T16:11Z");
4848

4949
@BeforeEach
5050
void setUp() {
51-
preventiveStateT1 = Mockito.mock(State.class);
52-
Mockito.when(preventiveStateT1.getContingency()).thenReturn(Optional.empty());
53-
flowCnecPreventiveT1 = Mockito.mock(FlowCnec.class);
54-
Mockito.when(flowCnecPreventiveT1.getState()).thenReturn(preventiveStateT1);
55-
Mockito.when(flowCnecPreventiveT1.getId()).thenReturn("cnec-preventive-t1");
56-
Mockito.when(flowCnecPreventiveT1.isOptimized()).thenReturn(true);
57-
58-
preventiveStateT2 = Mockito.mock(State.class);
59-
Mockito.when(preventiveStateT2.getContingency()).thenReturn(Optional.empty());
60-
flowCnecPreventiveT2 = Mockito.mock(FlowCnec.class);
61-
Mockito.when(flowCnecPreventiveT2.getState()).thenReturn(preventiveStateT2);
62-
Mockito.when(flowCnecPreventiveT2.getId()).thenReturn("cnec-preventive-t2");
63-
Mockito.when(flowCnecPreventiveT2.isOptimized()).thenReturn(true);
64-
65-
preventiveStateT3 = Mockito.mock(State.class);
66-
Mockito.when(preventiveStateT3.getContingency()).thenReturn(Optional.empty());
67-
flowCnecPreventiveT3 = Mockito.mock(FlowCnec.class);
68-
Mockito.when(flowCnecPreventiveT3.getState()).thenReturn(preventiveStateT3);
69-
Mockito.when(flowCnecPreventiveT3.getId()).thenReturn("cnec-preventive-no-timestamp");
70-
Mockito.when(flowCnecPreventiveT3.isOptimized()).thenReturn(true);
71-
72-
Contingency contingency1 = Mockito.mock(Contingency.class);
73-
Mockito.when(contingency1.getId()).thenReturn("contingency-1");
74-
curativeStateT1 = Mockito.mock(State.class);
75-
Mockito.when(curativeStateT1.getContingency()).thenReturn(Optional.of(contingency1));
76-
flowCnecCurative1T1 = Mockito.mock(FlowCnec.class);
77-
Mockito.when(flowCnecCurative1T1.getState()).thenReturn(curativeStateT1);
78-
Mockito.when(flowCnecCurative1T1.getId()).thenReturn("cnec-curative1");
79-
Mockito.when(flowCnecCurative1T1.isOptimized()).thenReturn(true);
80-
flowCnecCurative12T1 = Mockito.mock(FlowCnec.class);
81-
Mockito.when(flowCnecCurative12T1.getState()).thenReturn(curativeStateT1);
82-
Mockito.when(flowCnecCurative12T1.getId()).thenReturn("cnec-curative12");
83-
Mockito.when(flowCnecCurative12T1.isOptimized()).thenReturn(true);
84-
85-
Contingency contingency2 = Mockito.mock(Contingency.class);
86-
Mockito.when(contingency2.getId()).thenReturn("contingency-2");
87-
curativeStateT2 = Mockito.mock(State.class);
88-
Mockito.when(curativeStateT2.getContingency()).thenReturn(Optional.of(contingency2));
89-
flowCnecCurativeT2 = Mockito.mock(FlowCnec.class);
90-
Mockito.when(flowCnecCurativeT2.getState()).thenReturn(curativeStateT2);
91-
Mockito.when(flowCnecCurativeT2.getId()).thenReturn("cnec-curative2");
92-
Mockito.when(flowCnecCurativeT2.isOptimized()).thenReturn(true);
93-
94-
Contingency contingency3 = Mockito.mock(Contingency.class);
95-
Mockito.when(contingency3.getId()).thenReturn("contingency-3");
96-
curativeStateT3 = Mockito.mock(State.class);
97-
Mockito.when(curativeStateT3.getContingency()).thenReturn(Optional.of(contingency3));
98-
flowCnecCurativeT3 = Mockito.mock(FlowCnec.class);
99-
Mockito.when(flowCnecCurativeT3.getState()).thenReturn(curativeStateT3);
100-
Mockito.when(flowCnecCurativeT3.getId()).thenReturn("cnec-curative3");
101-
Mockito.when(flowCnecCurativeT3.isOptimized()).thenReturn(true);
51+
Contingency contingency1 = mockContingency("contingency-1");
52+
Contingency contingency2 = mockContingency("contingency-2");
53+
Contingency contingency3 = mockContingency("contingency-3");
54+
55+
State preventiveStateT1 = mockState(null, timestamp1);
56+
State preventiveStateT2 = mockState(null, timestamp2);
57+
State preventiveStateNoTimestamp = mockState(null, null);
58+
59+
State curativeState1T1 = mockState(contingency1, timestamp1);
60+
State curativeState1T2 = mockState(contingency1, timestamp2);
61+
State curativeState1NoTimestamp = mockState(contingency1, null);
62+
63+
State curativeState2T1 = mockState(contingency2, timestamp1);
64+
State curativeState2T2 = mockState(contingency2, timestamp2);
65+
State curativeState2NoTimestamp = mockState(contingency2, null);
66+
67+
State curativeState3T1 = mockState(contingency3, timestamp1);
68+
State curativeState3T2 = mockState(contingency3, timestamp2);
69+
State curativeState3NoTimestamp = mockState(contingency3, null);
70+
71+
flowCnecPreventiveT1 = mockFlowCnec("cnec-preventive-t1", preventiveStateT1);
72+
flowCnecPreventiveT2 = mockFlowCnec("cnec-preventive-t2", preventiveStateT2);
73+
flowCnecPreventiveNoTimestamp = mockFlowCnec("cnec-preventive", preventiveStateNoTimestamp);
74+
75+
flowCnecCurative1T1 = mockFlowCnec("cnec-curative-1-t1", curativeState1T1);
76+
flowCnecCurative1T2 = mockFlowCnec("cnec-curative-1-t2", curativeState1T2);
77+
flowCnecCurative1NoTimestamp = mockFlowCnec("cnec-curative-1", curativeState1NoTimestamp);
78+
79+
flowCnecCurative2T1 = mockFlowCnec("cnec-curative-2-t1", curativeState2T1);
80+
flowCnecCurative2T2 = mockFlowCnec("cnec-curative-2-t2", curativeState2T2);
81+
flowCnecCurative2NoTimestamp = mockFlowCnec("cnec-curative-2", curativeState2NoTimestamp);
82+
83+
flowCnecCurative3T1 = mockFlowCnec("cnec-curative-3-t1", curativeState3T1);
84+
flowCnecCurative3T2 = mockFlowCnec("cnec-curative-3-t2", curativeState3T2);
85+
flowCnecCurative3NoTimestamp = mockFlowCnec("cnec-curative-3", curativeState3NoTimestamp);
10286
}
10387

104-
void addTimestamp() {
105-
Mockito.when(preventiveStateT1.getTimestamp()).thenReturn(Optional.of(timestamp1));
106-
Mockito.when(preventiveStateT2.getTimestamp()).thenReturn(Optional.of(timestamp2));
107-
Mockito.when(preventiveStateT3.getTimestamp()).thenReturn(Optional.empty());
108-
Mockito.when(curativeStateT1.getTimestamp()).thenReturn(Optional.of(timestamp1));
109-
Mockito.when(curativeStateT2.getTimestamp()).thenReturn(Optional.of(timestamp2));
110-
Mockito.when(curativeStateT3.getTimestamp()).thenReturn(Optional.empty());
88+
private static Contingency mockContingency(String contingencyId) {
89+
Contingency contingency = Mockito.mock(Contingency.class);
90+
Mockito.when(contingency.getId()).thenReturn(contingencyId);
91+
return contingency;
92+
}
11193

94+
private static State mockState(Contingency contingency, OffsetDateTime timestamp) {
95+
State state = Mockito.mock(State.class);
96+
// mock contingency
97+
if (contingency == null) {
98+
Mockito.when(state.getContingency()).thenReturn(Optional.empty());
99+
} else {
100+
Mockito.when(state.getContingency()).thenReturn(Optional.of(contingency));
101+
}
102+
// mock timestamp
103+
if (timestamp == null) {
104+
Mockito.when(state.getTimestamp()).thenReturn(Optional.empty());
105+
} else {
106+
Mockito.when(state.getTimestamp()).thenReturn(Optional.of(timestamp));
107+
}
108+
return state;
112109
}
113110

114-
void addNullTimestamp() {
115-
Mockito.when(preventiveStateT1.getTimestamp()).thenReturn(Optional.empty());
116-
Mockito.when(preventiveStateT2.getTimestamp()).thenReturn(Optional.empty());
117-
Mockito.when(preventiveStateT3.getTimestamp()).thenReturn(Optional.empty());
118-
Mockito.when(curativeStateT1.getTimestamp()).thenReturn(Optional.empty());
119-
Mockito.when(curativeStateT2.getTimestamp()).thenReturn(Optional.empty());
120-
Mockito.when(curativeStateT3.getTimestamp()).thenReturn(Optional.empty());
111+
private static FlowCnec mockFlowCnec(String flowCnecId, State state) {
112+
FlowCnec flowCnec = Mockito.mock(FlowCnec.class);
113+
Mockito.when(flowCnec.getId()).thenReturn(flowCnecId);
114+
Mockito.when(flowCnec.getState()).thenReturn(state);
115+
Mockito.when(flowCnec.isOptimized()).thenReturn(true);
116+
return flowCnec;
121117
}
122118

123119
@Test
124120
void testEvaluator() {
125-
addTimestamp();
126-
Map<FlowCnec, Double> marginPerCnec = Map.of(flowCnecPreventiveT1, -10.0, flowCnecCurative1T1, -50.0, flowCnecCurative12T1, -120.0, flowCnecPreventiveT2, -34.0, flowCnecCurativeT2, -546.0, flowCnecPreventiveT3, 43.0, flowCnecCurativeT3, -76.0);
121+
Map<FlowCnec, Double> marginPerCnec = Map.of(flowCnecPreventiveT1, -10.0, flowCnecCurative1T1, -50.0, flowCnecCurative2T1, -120.0, flowCnecPreventiveT2, -34.0, flowCnecCurative1T2, -546.0, flowCnecPreventiveNoTimestamp, 43.0, flowCnecCurative1NoTimestamp, -76.0);
127122
SumMaxPerTimestampCostEvaluatorResult evaluatorResult = new SumMaxPerTimestampCostEvaluatorResult(
128123
marginPerCnec,
129124
List.of(),
@@ -132,15 +127,24 @@ void testEvaluator() {
132127

133128
// timestamp 1: -120, -10, -50 -> minMargin = -120 -> cost = 120
134129
// timestamp 2: -34, -546 -> minMargin = -546 -> cost = 546
135-
// timestamp 3: 43, -76 -> minMargin = -76 -> cost = 76
130+
// no timestamp: 43, -76 -> minMargin = -76 -> cost = 76
136131
// the expected evaluation in the sum of maxes: 120 + 546 + 76 = 742
137132
assertEquals(742, evaluatorResult.getCost(Set.of(), Set.of()));
138133
}
139134

140135
@Test
141136
void testEvaluatorWithExclusion() {
142-
addTimestamp();
143-
Map<FlowCnec, Double> marginPerCnec = Map.of(flowCnecPreventiveT1, -10.0, flowCnecCurative1T1, -50.0, flowCnecCurative12T1, -120.0, flowCnecPreventiveT2, -34.0, flowCnecCurativeT2, -546.0, flowCnecPreventiveT3, 43.0, flowCnecCurativeT3, -76.0);
137+
Map<FlowCnec, Double> marginPerCnec = Map.of(
138+
flowCnecPreventiveT1, -10.0,
139+
flowCnecCurative1T1, -50.0,
140+
flowCnecCurative2T1, -120.0,
141+
flowCnecCurative3T1, -200.0,
142+
flowCnecPreventiveT2, -34.0,
143+
flowCnecCurative2T2, -546.0,
144+
flowCnecCurative3T2, -211.0,
145+
flowCnecPreventiveNoTimestamp, 43.0,
146+
flowCnecCurative2NoTimestamp, -76.0
147+
);
144148
SumMaxPerTimestampCostEvaluatorResult evaluatorResult = new SumMaxPerTimestampCostEvaluatorResult(
145149
marginPerCnec,
146150
List.of(),
@@ -151,20 +155,24 @@ void testEvaluatorWithExclusion() {
151155
// Also exclude cnec-curative12
152156
// timestamp 1: -10, -50 -> minMargin = -50 -> cost = 50
153157
// timestamp 2: 34 -> minMargin = -34 -> cost = 34
154-
// timestamp 3: -43 -> minMargin = 43 -> cost = -43
158+
// no timestamp: -43 -> minMargin = 43 -> cost = -43
155159
// the expected evaluation in the sum of maxes: 50 + 34 - 43 = 41
156160
assertEquals(41, evaluatorResult.getCost(Set.of("contingency-3", "contingency-2"), Set.of("cnec-curative12")));
157161
}
158162

159163
@Test
160164
void testWithoutAnyTimestampWithExclusion() {
161-
addNullTimestamp();
162-
Map<FlowCnec, Double> marginPerCnec = Map.of(flowCnecPreventiveT1, -10.0, flowCnecCurative1T1, -50.0, flowCnecCurative12T1, -40.0, flowCnecCurativeT2, 20.0, flowCnecCurativeT3, -17.0);
165+
Map<FlowCnec, Double> marginPerCnec = Map.of(
166+
flowCnecPreventiveNoTimestamp, -10.0,
167+
flowCnecCurative1NoTimestamp, -50.0,
168+
flowCnecCurative2NoTimestamp, -40.0,
169+
flowCnecCurative3NoTimestamp, -17.0
170+
);
163171
SumMaxPerTimestampCostEvaluatorResult evaluatorResult = new SumMaxPerTimestampCostEvaluatorResult(marginPerCnec, List.of(), Unit.MEGAWATT);
164-
assertEquals(50.0, evaluatorResult.getCost(Set.of(), Set.of()));
165-
assertEquals(17.0, evaluatorResult.getCost(Set.of("contingency-1", "contingency-2"), Set.of()));
166-
assertEquals(40.0, evaluatorResult.getCost(Set.of(), Set.of("cnec-curative1")));
167-
assertEquals(17.0, evaluatorResult.getCost(Set.of("contingency-1"), Set.of("cnec-curative1")));
172+
assertEquals(50.0, evaluatorResult.getCost(new HashSet<>(), new HashSet<>()));
173+
assertEquals(17.0, evaluatorResult.getCost(Set.of("contingency-1", "contingency-2"), new HashSet<>()));
174+
assertEquals(40.0, evaluatorResult.getCost(new HashSet<>(), Set.of("cnec-curative-1")));
175+
assertEquals(10.0, evaluatorResult.getCost(Set.of("contingency-3"), Set.of("cnec-curative-1", "cnec-curative-2")));
168176
}
169177

170178
@Test

0 commit comments

Comments
 (0)