Skip to content

Commit

Permalink
fix: made excessPvEnergyUse fixed to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
enell committed Mar 7, 2023
1 parent 89877bc commit 9023e86
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
75 changes: 40 additions & 35 deletions src/strategy-battery-charging-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,35 @@ const repair = (phenotype, endTime) => {
return p
}

const mutationFunction = (endTime, mutationRate) => (phenotype) => {
const timeAdjustment = () => {
const percent = Math.random() * 0.4 + 0.01
const adjustment =
Math.max(Math.floor(endTime * percent), 5) *
(Math.random() < 0.5 ? -1 : 1)
return adjustment
}

for (let i = 0; i < phenotype.periods.length; i += 1) {
const g = phenotype.periods[i]
if (Math.random() < mutationRate) {
// Mutate action
g.activity *= -1
const mutationFunction =
(endTime, mutationRate, excessPvEnergyUse) => (phenotype) => {
const timeAdjustment = () => {
return random(0, 61) - 30
}
if (Math.random() < mutationRate) {
// Mutate start time
const timeChange = timeAdjustment()
g.start += timeChange
g.duration -= timeChange

for (let i = 0; i < phenotype.periods.length; i += 1) {
const g = phenotype.periods[i]
if (Math.random() < mutationRate) {
// Mutate action
g.activity *= -1
}
if (Math.random() < mutationRate) {
// Mutate start time
const timeChange = timeAdjustment()
g.start += timeChange
g.duration -= timeChange
}
if (Math.random() < mutationRate) {
// Mutate duration
const timeChange = timeAdjustment()
g.duration += timeChange
}
}
if (Math.random() < mutationRate) {
// Mutate duration
const timeChange = timeAdjustment()
g.duration += timeChange
return {
periods: repair(phenotype.periods, endTime),
excessPvEnergyUse: excessPvEnergyUse,
}
}
return {
periods: repair(phenotype.periods, endTime),
excessPvEnergyUse:
Math.random() < mutationRate
? phenotype.excessPvEnergyUse ^ 1
: phenotype.excessPvEnergyUse,
}
}

const crossoverFunction = (endTime) => (phenotypeA, phenotypeB) => {
const midpoint = random(0, phenotypeA.periods.length)
Expand All @@ -98,7 +92,12 @@ const crossoverFunction = (endTime) => (phenotypeA, phenotypeB) => {
]
}

const generatePopulation = (endTime, populationSize, numberOfPricePeriods) => {
const generatePopulation = (
endTime,
populationSize,
numberOfPricePeriods,
excessPvEnergyUse
) => {
const sortedIndex = (array, value) => {
let low = 0
let high = array.length
Expand Down Expand Up @@ -133,7 +132,7 @@ const generatePopulation = (endTime, populationSize, numberOfPricePeriods) => {

population.push({
periods: timePeriods,
excessPvEnergyUse: Math.round(Math.random()),
excessPvEnergyUse: excessPvEnergyUse,
})
}
return population
Expand Down Expand Up @@ -234,6 +233,7 @@ const calculateBatteryChargingStrategy = (config) => {
batteryMaxEnergy,
batteryMaxInputPower,
soc,
excessPvEnergyUse,
} = config

const input = mergeInput(config)
Expand All @@ -249,13 +249,18 @@ const calculateBatteryChargingStrategy = (config) => {
soc,
})
const geneticAlgorithm = geneticAlgorithmConstructor({
mutationFunction: mutationFunction(totalDuration, mutationRate),
mutationFunction: mutationFunction(
totalDuration,
mutationRate,
excessPvEnergyUse
),
crossoverFunction: crossoverFunction(totalDuration),
fitnessFunction: f,
population: generatePopulation(
totalDuration,
populationSize,
numberOfPricePeriods
numberOfPricePeriods,
excessPvEnergyUse
),
})

Expand Down
1 change: 1 addition & 0 deletions src/strategy-battery-charging.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const node = (RED) => {
averageConsumption,
consumptionForecast,
productionForecast,
excessPvEnergyUse: 0, // 0=Fed to grid, 1=Charge
soc: soc / 100,
})

Expand Down

0 comments on commit 9023e86

Please sign in to comment.