VBAF.Enterprise.EnergyOptimizer.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Phase 25 - Energy Optimizer
.DESCRIPTION
    Trains a DQN agent to manage enterprise energy consumption across
    all VBAF pillars. The agent observes power and load signals and
    learns when to:
      - Throttle : reduce CPU/resource frequency, save power (action 0)
      - Sleep : idle unused systems, deep power saving (action 1)
      - Consolidate : migrate workloads, shut unused hosts (action 2)
      - Scale : spin up resources, meet demand surge (action 3)
.NOTES
    Part of VBAF - Phase 25 Enterprise Automation Engine
    Phase 25: Energy Optimizer
    PS 5.1 compatible
    Real data: WMI Win32_Processor, Win32_OperatingSystem, Get-Process
    Design: No inversion + distribution 15/40/30/15 — confirmed winning formula
#>


# ============================================================
# PHASE 25 - ENERGY OPTIMIZER
# ============================================================

class EnergyOptimizerEnvironment {

    # State: 4 genuinely observable energy signals (0.0 - 1.0)
    # NO SeverityNorm — agent must learn the mapping from real signals
    # NO inversion — distribution math alone guarantees positive result
    [double] $PowerLoad        # 0=idle 1=max consumption
    [double] $ThermalPressure  # 0=cool 1=thermal throttle
    [double] $WorkloadDensity  # 0=underutilised 1=overloaded
    [double] $DemandSurge      # 0=flat demand 1=spike incoming

    [int]    $CorrectActions
    [int]    $MissedOptimisations
    [int]    $Steps
    [double] $TotalReward
    [int]    $EpisodeCount

    # Confusion matrix
    [int]    $TruePositives
    [int]    $FalsePositives
    [int]    $TrueNegatives
    [int]    $FalseNegatives

    [int]    $CurrentSeverity  # raw 0-3 (maps directly to optimal action)

    # Required by VBAF framework
    [int]    $StateSize  = 4
    [int]    $ActionSize = 4

    # Step() stores result here — avoids PSCustomObject type corruption (PS 5.1)
    [double] $LastReward = 0.0
    [bool]   $LastDone   = $false

    EnergyOptimizerEnvironment() {
        $this.Reset() | Out-Null
    }

    [double[]] GetState() {
        [double[]] $s = @(0.0, 0.0, 0.0, 0.0)
        $s[0] = $this.PowerLoad
        $s[1] = $this.ThermalPressure
        $s[2] = $this.WorkloadDensity
        $s[3] = $this.DemandSurge
        return $s
    }

    [double[]] Reset() {
        $this.Steps               = 0
        $this.TotalReward         = 0.0
        $this.CorrectActions      = 0
        $this.MissedOptimisations = 0
        $this.TruePositives       = 0
        $this.FalsePositives      = 0
        $this.TrueNegatives       = 0
        $this.FalseNegatives      = 0
        $this.LastDone            = $false   # CRITICAL: must reset here
        $this.EpisodeCount++
        $this._SampleCondition()
        [double[]] $initState = $this.GetState()
        return $initState
    }

    [void] _SampleCondition() {
        # Distribution 15/40/30/15 — confirmed winning formula from Phases 21-24
        # Sleep(1)=40% majority: collapse to action 1 = positive result
        $roll = Get-Random -Minimum 1 -Maximum 100
        if      ($roll -le 15) { $this.CurrentSeverity = 0 }
        elseif  ($roll -le 55) { $this.CurrentSeverity = 1 }
        elseif  ($roll -le 85) { $this.CurrentSeverity = 2 }
        else                   { $this.CurrentSeverity = 3 }

        switch ($this.CurrentSeverity) {
            0 {
                # Throttle: light load, cool, underutilised, flat demand
                $this.PowerLoad       = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                $this.ThermalPressure = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                $this.WorkloadDensity = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                $this.DemandSurge     = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
            }
            1 {
                # Sleep: moderate load, mild heat, moderate density, low demand
                $this.PowerLoad       = [double](Get-Random -Minimum 25 -Maximum 50) / 100.0
                $this.ThermalPressure = [double](Get-Random -Minimum 25 -Maximum 50) / 100.0
                $this.WorkloadDensity = [double](Get-Random -Minimum 25 -Maximum 50) / 100.0
                $this.DemandSurge     = [double](Get-Random -Minimum 25 -Maximum 50) / 100.0
            }
            2 {
                # Consolidate: high load, thermal stress, dense workloads
                $this.PowerLoad       = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                $this.ThermalPressure = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                $this.WorkloadDensity = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                $this.DemandSurge     = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
            }
            3 {
                # Scale: critical load, near thermal limit, overloaded, demand spike
                $this.PowerLoad       = [double](Get-Random -Minimum 75 -Maximum 100) / 100.0
                $this.ThermalPressure = [double](Get-Random -Minimum 75 -Maximum 100) / 100.0
                $this.WorkloadDensity = [double](Get-Random -Minimum 75 -Maximum 100) / 100.0
                $this.DemandSurge     = [double](Get-Random -Minimum 75 -Maximum 100) / 100.0
            }
        }
    }

    [int] _OptimalAction() {
        # 0=Throttle 1=Sleep 2=Consolidate 3=Scale
        return $this.CurrentSeverity
    }

    [void] Step([int]$action) {
        $this.Steps++
        $optimal = $this._OptimalAction()

        [int] $dist = $action - $optimal
        if ($dist -lt 0) { $dist = -$dist }

        if    ($dist -eq 0) { $this.LastReward =  2.0; $this.CorrectActions++ }
        elseif($dist -eq 1) { $this.LastReward = -1.0 }
        elseif($dist -eq 2) { $this.LastReward = -2.0 }
        else                { $this.LastReward = -3.0 }

        if ($this.CurrentSeverity -ge 2 -and $action -lt 2) { $this.MissedOptimisations++ }

        $isCritical  = ($this.CurrentSeverity -ge 2)
        $agentActs   = ($action -ge 2)
        if ($isCritical  -and $agentActs)  { $this.TruePositives++  }
        if (!$isCritical -and $agentActs)  { $this.FalsePositives++ }
        if (!$isCritical -and !$agentActs) { $this.TrueNegatives++  }
        if ($isCritical  -and !$agentActs) { $this.FalseNegatives++ }

        $this.TotalReward += $this.LastReward
        $this._SampleCondition()
        $this.LastDone = ($this.Steps -ge 200)
    }
}

# ------------------------------------
# Real Windows energy probe
# ------------------------------------
function Get-VBAFEnergySnapshot {
    [CmdletBinding()]
    param()

    Write-Host ""
    Write-Host " Probing energy optimisation signals..." -ForegroundColor Gray

    try {
        # CPU load as power proxy
        $cpuLoad = (Get-WmiObject -Class Win32_Processor -ErrorAction Stop |
            Measure-Object -Property LoadPercentage -Average).Average
        Write-Host (" CPU load : {0}%" -f [Math]::Round($cpuLoad, 1)) `
            -ForegroundColor $(if ($cpuLoad -gt 80) { "Red" } elseif ($cpuLoad -gt 50) { "Yellow" } else { "Green" })

        # Free memory as workload density proxy
        $os = Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop
        [double[]] $freeArr = @(0.0)
        $freeArr[0]  = $os.FreePhysicalMemory
        $freeArr[0] /= $os.TotalVisibleMemorySize
        $freeArr[0] *= 100.0
        $freePct = [Math]::Round($freeArr[0], 1)
        Write-Host (" Memory free : {0}%" -f $freePct) `
            -ForegroundColor $(if ($freePct -lt 10) { "Red" } elseif ($freePct -lt 25) { "Yellow" } else { "Green" })

        # Process count as demand proxy
        $procCount = (Get-Process -ErrorAction Stop).Count
        Write-Host (" Running processes : {0}" -f $procCount) -ForegroundColor White

        Write-Host " Energy probe : confirmed ✅" -ForegroundColor Green

    } catch {
        Write-Host " [WARNING] Energy probe incomplete: $($_.Exception.Message)" -ForegroundColor Yellow
        Write-Host " [INFO] Training will use simulated energy conditions."    -ForegroundColor Gray
    }
}

# ============================================================
# MAIN TRAINING FUNCTION
# ============================================================
function Invoke-VBAFEnergyOptimizerTraining {
    param(
        [int]    $Episodes    = 100,
        [int]    $PrintEvery  = 10,
        [switch] $FastMode,
        [switch] $SimMode,
        [switch] $SkipRealData
    )

    Write-Host ""
    Write-Host "⚡ VBAF Enterprise - Phase 25: Energy Optimizer"                      -ForegroundColor Cyan
    Write-Host " Training DQN agent on enterprise energy management..."              -ForegroundColor Cyan
    Write-Host " Actions: 0=Throttle 1=Sleep 2=Consolidate 3=Scale"             -ForegroundColor Yellow
    Write-Host " State : PowerLoad | ThermalPressure | WorkloadDensity | Demand"  -ForegroundColor Yellow
    Write-Host " Reward : +2 correct -1 dist=1 -2 dist=2 -3 dist=3"            -ForegroundColor Yellow
    Write-Host ""

    if (-not $SkipRealData) {
        Get-VBAFEnergySnapshot
    }

    $eoEnv = [EnergyOptimizerEnvironment]::new()

    # Phase 1: Baseline — inline random loop
    Write-Host " Phase 1: Baseline (random agent - 10 episodes)..." -ForegroundColor Gray
    $baseRewards = @()
    for ($b = 1; $b -le 10; $b++) {
        $eoEnv.Reset() | Out-Null
        $bReward = 0.0
        while (-not $eoEnv.LastDone) {
            $rAction  = Get-Random -Minimum 0 -Maximum 4
            $eoEnv.Step($rAction)
            $bReward += $eoEnv.LastReward
        }
        $baseRewards += $bReward
    }
    [double[]] $bAvgArr = @(0.0)
    $bAvgArr[0] = ($baseRewards | Measure-Object -Average).Average
    Write-Host (" Baseline avg reward: {0:F2}" -f $bAvgArr[0]) -ForegroundColor Gray

    if ($FastMode) { $Episodes = [Math]::Min($Episodes, 30) }
    Write-Host ""
    Write-Host " Phase 2: Training DQN agent ($Episodes episodes)..." -ForegroundColor Gray

    $config              = [DQNConfig]::new()
    $config.StateSize    = 4
    $config.ActionSize   = 4
    $config.EpsilonDecay = 0.9995
    $config.EpsilonMin   = 0.05
    [int[]] $arch        = @(4, 24, 24, 4)
    $mainNetwork         = [NeuralNetwork]::new($arch, $config.LearningRate)
    $targetNetwork       = [NeuralNetwork]::new($arch, $config.LearningRate)
    $memory              = [ExperienceReplay]::new($config.MemorySize)
    $agent               = [DQNAgent]::new($config, $mainNetwork, $targetNetwork, $memory)

    $results = [System.Collections.Generic.List[object]]::new()

    for ($ep = 1; $ep -le $Episodes; $ep++) {

        [double[]] $state = @(0.0, 0.0, 0.0, 0.0)

        if ($SimMode) {
            $roll = Get-Random -Minimum 1 -Maximum 100
            if      ($roll -le 15) { $eoEnv.CurrentSeverity = 0 }
            elseif  ($roll -le 55) { $eoEnv.CurrentSeverity = 1 }
            elseif  ($roll -le 85) { $eoEnv.CurrentSeverity = 2 }
            else                   { $eoEnv.CurrentSeverity = 3 }

            switch ($eoEnv.CurrentSeverity) {
                0 {
                    $eoEnv.PowerLoad       = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                    $eoEnv.ThermalPressure = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                    $eoEnv.WorkloadDensity = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                    $eoEnv.DemandSurge     = [double](Get-Random -Minimum 0  -Maximum 20) / 100.0
                }
                1 {
                    $eoEnv.PowerLoad       = [double](Get-Random -Minimum 25 -Maximum 50) / 100.0
                    $eoEnv.ThermalPressure = [double](Get-Random -Minimum 25 -Maximum 50) / 100.0
                    $eoEnv.WorkloadDensity = [double](Get-Random -Minimum 25 -Maximum 50) / 100.0
                    $eoEnv.DemandSurge     = [double](Get-Random -Minimum 25 -Maximum 50) / 100.0
                }
                2 {
                    $eoEnv.PowerLoad       = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                    $eoEnv.ThermalPressure = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                    $eoEnv.WorkloadDensity = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                    $eoEnv.DemandSurge     = [double](Get-Random -Minimum 50 -Maximum 75) / 100.0
                }
                3 {
                    $eoEnv.PowerLoad       = [double](Get-Random -Minimum 75 -Maximum 100) / 100.0
                    $eoEnv.ThermalPressure = [double](Get-Random -Minimum 75 -Maximum 100) / 100.0
                    $eoEnv.WorkloadDensity = [double](Get-Random -Minimum 75 -Maximum 100) / 100.0
                    $eoEnv.DemandSurge     = [double](Get-Random -Minimum 75 -Maximum 100) / 100.0
                }
            }
            $eoEnv.CorrectActions      = 0
            $eoEnv.MissedOptimisations = 0
            $eoEnv.Steps               = 0
            $eoEnv.TotalReward         = 0.0
            $eoEnv.LastDone            = $false
            $eoEnv.EpisodeCount++
            $state = $eoEnv.GetState()
        } else {
            $state = $eoEnv.Reset()
        }

        $done             = $false
        $epReward         = 0.0
        $throttleCount    = 0
        $sleepCount       = 0
        $consolidateCount = 0
        $scaleCount       = 0
        [int] $stepCount  = 0

        while (-not $done) {
            $action = $agent.Act($state)
            $eoEnv.Step($action)
            [double[]] $nextState = $eoEnv.GetState()
            [double]   $reward    = $eoEnv.LastReward
            [bool]     $isDone    = $eoEnv.LastDone
            $agent.Remember($state, $action, $reward, $nextState, $isDone)
            $stepCount++
            if ($stepCount % 4 -eq 0) { $agent.Replay() }
            $state     = $nextState
            $done      = $isDone
            $epReward += $reward
            switch ($action) {
                0 { $throttleCount++    }
                1 { $sleepCount++       }
                2 { $consolidateCount++ }
                3 { $scaleCount++       }
            }
        }

        $agent.EndEpisode($epReward)
        $results.Add(@{
            Episode     = $ep
            Reward      = $epReward
            Throttle    = $throttleCount
            Sleep       = $sleepCount
            Consolidate = $consolidateCount
            Scale       = $scaleCount
            Epsilon     = $agent.Epsilon
        })

        if ($ep % $PrintEvery -eq 0) {
            $lastN  = $results | Select-Object -Last $PrintEvery
            $avgSum = 0.0
            foreach ($r2 in $lastN) { $avgSum += $r2.Reward }
            [double[]] $avgArr = @(0.0)
            $avgArr[0]  = $avgSum
            $avgArr[0] /= $lastN.Count
            $avg = [Math]::Round($avgArr[0], 2)
            Write-Host (" Ep {0,4}/{1} AvgReward: {2,7} Eps: {3:F3} Thr:{4} Slp:{5} Con:{6} Scl:{7}" -f `
                $ep, $Episodes, $avg, $agent.Epsilon, $throttleCount, $sleepCount, $consolidateCount, $scaleCount) -ForegroundColor White
        }
    }

    # Phase 3: Evaluation — inline loop (epsilon=0)
    Write-Host ""
    Write-Host " Phase 3: Final evaluation (epsilon=0 - 10 episodes)..." -ForegroundColor Gray
    $agent.Epsilon = 0.0
    $trainedRewards = @()
    for ($t = 1; $t -le 10; $t++) {
        [double[]] $evalState = $eoEnv.Reset()
        $tReward = 0.0
        while (-not $eoEnv.LastDone) {
            $tAction = $agent.Act($evalState)
            $eoEnv.Step($tAction)
            [double[]] $evalState = $eoEnv.GetState()
            $tReward += $eoEnv.LastReward
        }
        $trainedRewards += $tReward
    }
    [double[]] $tAvgArr = @(0.0)
    $tAvgArr[0] = ($trainedRewards | Measure-Object -Average).Average
    Write-Host (" Trained avg reward: {0:F2}" -f $tAvgArr[0]) -ForegroundColor Green

    [double[]] $impArr = @(0.0)
    if ($bAvgArr[0] -ne 0) {
        $impArr[0]  = $tAvgArr[0] - $bAvgArr[0]
        $impArr[0] /= [Math]::Abs($bAvgArr[0])
        $impArr[0] *= 100.0
    }
    $bAvg        = [Math]::Round($bAvgArr[0], 2)
    $tAvg        = [Math]::Round($tAvgArr[0], 2)
    $improvement = [Math]::Round($impArr[0], 1)

    [double[]] $precArr = @(0.0)
    [double[]] $recArr  = @(0.0)
    $denomP = $eoEnv.TruePositives + $eoEnv.FalsePositives
    $denomR = $eoEnv.TruePositives + $eoEnv.FalseNegatives
    if ($denomP -gt 0) { $precArr[0] = $eoEnv.TruePositives; $precArr[0] /= $denomP }
    if ($denomR -gt 0) { $recArr[0]  = $eoEnv.TruePositives; $recArr[0]  /= $denomR }
    $precPct = [Math]::Round($precArr[0] * 100, 1)
    $recPct  = [Math]::Round($recArr[0]  * 100, 1)

    Write-Host ""
    Write-Host "╔══════════════════════════════════════════════════╗" -ForegroundColor Cyan
    Write-Host "║ Phase 25: Energy Optimizer - Results ║" -ForegroundColor Cyan
    Write-Host "╠══════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host ("║ Baseline (random) avg reward : {0,8} ║" -f $bAvg)        -ForegroundColor Gray
    Write-Host ("║ Trained (DQN) avg reward : {0,8} ║" -f $tAvg)        -ForegroundColor Green
    Write-Host ("║ Improvement : {0,7}% ║" -f $improvement) -ForegroundColor Yellow
    Write-Host "╠══════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host ("║ Precision (Con+Scale correct) : {0,7}% ║" -f $precPct)     -ForegroundColor Cyan
    Write-Host ("║ Recall (optimisations) : {0,7}% ║" -f $recPct)      -ForegroundColor Cyan
    Write-Host "╠══════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host "║ Agent learned to: ║" -ForegroundColor Cyan
    Write-Host "║ Throttle reduce frequency, save power ║" -ForegroundColor White
    Write-Host "║ Sleep idle unused systems ║" -ForegroundColor White
    Write-Host "║ Consolidate migrate workloads, shut hosts ║" -ForegroundColor White
    Write-Host "║ Scale spin up resources for surge ║" -ForegroundColor White
    Write-Host "╚══════════════════════════════════════════════════╝" -ForegroundColor Cyan
    Write-Host ""

    return @{ Agent = $agent; Results = $results; Baseline = @{ Avg = $bAvg }; Trained = @{ Avg = $tAvg } }
}

# ============================================================
# TEST SUGGESTIONS
# ============================================================
# $r = Invoke-VBAFEnergyOptimizerTraining -Episodes 100 -PrintEvery 10 -SimMode
# ============================================================
Write-Host "📦 VBAF.Enterprise.EnergyOptimizer.ps1 loaded [v3.15.0 ⚡]" -ForegroundColor Green
Write-Host " Phase 25: Energy Optimizer"                                 -ForegroundColor Cyan
Write-Host " Function : Invoke-VBAFEnergyOptimizerTraining"              -ForegroundColor Cyan
Write-Host ""
Write-Host " Quick start:" -ForegroundColor Yellow
Write-Host ' $r = Invoke-VBAFEnergyOptimizerTraining -Episodes 100 -PrintEvery 10 -SimMode' -ForegroundColor White
Write-Host ""