Repository navigation
Expand file tree
/
Copy pathnative_optimizer_test.go
More file actions
219 lines (210 loc) · 7.06 KB
/
Copy pathnative_optimizer_test.go
File metadata and controls
219 lines (210 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package mpc
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/srcfl/ftw/go/internal/state"
)
// The optional native process is pinned and checked by make native-solver-test.
// Ordinary Go tests remain usable without a Rust toolchain.
func nativeWorker(t testing.TB, budget time.Duration) *ExternalOptimizer {
t.Helper()
binary := os.Getenv("FTW_NATIVE_SOLVER")
if binary == "" {
t.Skip("run make native-solver-test to include the Rust worker")
}
if !filepath.IsAbs(binary) {
t.Fatal("FTW_NATIVE_SOLVER must be an absolute executable path")
}
o, err := NewExternalOptimizer(ExternalOptimizerConfig{Command: []string{binary, "--time-limit=" + budget.String()}, Timeout: 3 * time.Second})
if err != nil {
t.Fatal(err)
}
return o
}
func nativeFixture() ([]Slot, Params) {
slots, p := externalTestFixture()
p.SoCLevels, p.ActionLevels = 41, 21
p.Loadpoint = &LoadpointSpec{ID: "garage", CapacityWh: 10000, Levels: 11, SoCMax: 1, InitialSoC: .2, PluggedIn: true, TargetSoC: .35, TargetSlotIdx: 1, MaxChargeW: 2000, AllowedStepsW: []float64{0, 1400, 2000}, ChargeEfficiency: .9, NoBatteryToEV: true}
return slots, p
}
func TestNativeProcessCoreContract(t *testing.T) {
o := nativeWorker(t, 500*time.Millisecond)
defer o.Close()
slots, p := nativeFixture()
floor := -5.0
p.ExportBonusOreKwh, p.ExportFeeOreKwh, p.ExportFloorOreKwh = 12, 3, &floor
p.PVChargeBonusOreKwh, p.MinArbitrageSpreadOreKwh = 30, 20
slots[0].PVW = -4500
for _, mode := range []Mode{ModeArbitrage, ModeSelfConsumption, ModePassiveArbitrage, ModeCheapCharge} {
p.Mode = mode
plan, err := o.Optimize(context.Background(), slots, p)
if err != nil {
t.Fatalf("%s: %v", mode, err)
}
if err := ValidatePlan(slots, p, &plan); err != nil {
t.Fatal(err)
}
if plan.Solver.Backend != "fleet_milp_rust" || plan.Actions[1].LoadpointSoC < p.Loadpoint.TargetSoC {
t.Fatalf("unexpected plan: %+v", plan)
}
if strings.Contains(string(plan.OptimizerInput), `"price_ore"`) || !strings.Contains(string(plan.OptimizerInput), `"price_per_kwh"`) {
t.Fatalf("planner wire still uses öre keys: %s", plan.OptimizerInput)
}
for _, a := range plan.Actions {
if a.PVLimitW != 0 {
t.Fatal("unexpected curtailment")
}
}
}
// Core applies the PV risk margin to the slots itself (see
// EnergyplanOptimizer.Optimize), so a request never carries a scenario
// model, even when the margin is set.
p.PVForecastSafetyK, p.PVUncertaintyW = 1, 300
if plan, err := o.Optimize(context.Background(), slots, p); err != nil || plan.Solver.ScenarioCount > 1 {
t.Fatalf("a risk margin built a scenario model: plan=%+v err=%v", plan.Solver, err)
}
p.PVForecastSafetyK = 0
if _, err := o.Optimize(context.Background(), slots, p); err != nil {
t.Fatalf("worker did not recover after rejection: %v", err)
}
}
func TestNativeProcessRejectsMaximumOnlyPVSettings(t *testing.T) {
o := nativeWorker(t, 500*time.Millisecond)
defer o.Close()
slots, p := externalTestFixture()
for _, tc := range []struct {
name string
maximum float64
nullMin bool
}{
{"zero", 0, false},
{"positive", 1500, false},
{"zero with null minimum", 0, true},
{"positive with null minimum", 1500, true},
} {
t.Run(tc.name, func(t *testing.T) {
request := o.buildRequest(slots, p)
encoded, err := json.Marshal(request)
if err != nil {
t.Fatal(err)
}
var payload map[string]any
if err := json.Unmarshal(encoded, &payload); err != nil {
t.Fatal(err)
}
// Inject the invalid setting at the host/worker boundary. Core's
// normal request builder emits both verified capability bounds.
settings := payload["settings"].(map[string]any)
settings["pv_curtailment_max_w"] = tc.maximum
if tc.nullMin {
settings["pv_curtailment_min_w"] = nil
}
encoded, err = json.Marshal(payload)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
raw, err := o.transport.RoundTrip(ctx, encoded)
if err != nil {
t.Fatal(err)
}
var response externalResponse
if err := json.Unmarshal(raw, &response); err != nil {
t.Fatal(err)
}
if response.RequestID != request.RequestID || response.OK || response.Error == nil || response.Error.Code != "invalid_request" || len(response.Plan.Actions) != 0 {
t.Errorf("maximum-only setting must return invalid_request without a plan: %s", raw)
}
// Reuse this transport and let Core validate the next valid plan.
if _, err := o.Optimize(ctx, slots, p); err != nil {
t.Fatalf("valid request after rejection failed: %v", err)
}
})
}
}
func TestNativeFullHorizonPhysicalBoundaries(t *testing.T) {
o := nativeWorker(t, 500*time.Millisecond)
defer o.Close()
for _, mode := range []Mode{ModeSelfConsumption, ModeArbitrage, ModePassiveArbitrage, ModeCheapCharge} {
for _, blocksBattery := range []bool{false, true} {
slots, p := nativeBenchmarkFixture(true)
p.Mode, p.Loadpoint.NoBatteryToEV = mode, blocksBattery
plan, err := o.Optimize(context.Background(), slots, p)
if err != nil {
t.Fatalf("%s, battery blocked=%t: %v", mode, blocksBattery, err)
}
if err := ValidatePlan(slots, p, &plan); err != nil {
t.Fatal(err)
}
for i, a := range plan.Actions {
if a.LoadpointW > 1e-5 && a.GridW < -1e-6 && a.BatteryW < -1e-5 {
t.Fatalf("slot %d: EV=%g W battery=%g W grid=%g W", i, a.LoadpointW, a.BatteryW, a.GridW)
}
}
}
}
}
func TestNativeProcessConcurrentAndImmutable(t *testing.T) {
o := nativeWorker(t, 500*time.Millisecond)
defer o.Close()
slots, p := nativeFixture()
before, _ := json.Marshal(struct {
Slots []Slot
Params Params
}{slots, p})
var wg sync.WaitGroup
for range 8 {
wg.Add(1)
go func() {
defer wg.Done()
if _, err := o.Optimize(context.Background(), slots, p); err != nil {
t.Error(err)
}
}()
}
wg.Wait()
after, _ := json.Marshal(struct {
Slots []Slot
Params Params
}{slots, p})
if string(before) != string(after) {
t.Fatal("shared input mutated")
}
}
func TestNativeServiceAndFallback(t *testing.T) {
for _, budget := range []time.Duration{time.Second, time.Nanosecond} {
o := nativeWorker(t, budget)
defer o.Close()
st, err := state.Open(filepath.Join(t.TempDir(), "site.db"))
if err != nil {
t.Fatal(err)
}
defer st.Close()
_, p := externalTestFixture()
p.SoCLevels, p.ActionLevels = 11, 5
now := time.Now().UTC().Truncate(time.Minute)
if err := st.SavePrices([]state.PricePoint{{Zone: "SE3", SlotTsMs: now.Add(-time.Minute).UnixMilli(), SlotLenMin: 15, SpotOreKwh: 50, TotalOreKwh: 100, Source: "test", FetchedAtMs: now.UnixMilli()}}); err != nil {
t.Fatal(err)
}
s := New(st, nil, "SE3", p)
s.BaseLoad = 500
s.Optimizer = o
plan := s.Replan(context.Background())
if plan == nil || plan.Solver == nil {
t.Fatal("service returned no plan")
}
if plan.Solver.Fallback != (budget == time.Nanosecond) {
t.Fatalf("fallback metadata: %+v", plan.Solver)
}
if budget == time.Second && plan.Solver.Backend != "value_curve_rust" {
t.Fatalf("Rust worker did not supply service plan: %+v", plan.Solver)
}
}
}