Skip to content

Commit

Permalink
fix: penalty for empty charge (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
enell authored May 11, 2023
1 parent fc20124 commit 5c8a165
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
23 changes: 13 additions & 10 deletions src/fitness.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,25 @@ const calculatePeriodScore = (
return [cost, currentCharge - _currentCharge]
}

const cost = (periods) => {
return periods.reduce((acc, cur) => acc + cur.cost, 0)
}

const fitnessFunction = (props) => (phenotype) => {
let cost = 0
let averagePrice = props.input.reduce((acc, cur) => acc + cur.importPrice, 0) / props.input.length
const periods = allPeriods(props, phenotype)
let score = -cost(periods)

for (const period of allPeriodsGenerator(props, phenotype)) {
let periodScore = period.cost
if (period.activity != 0 && period.charge == 0) {
periodScore += averagePrice * period.duration / 60
}
cost -= period.cost
}
let averagePrice = props.input.reduce((acc, cur) => acc + cur.importPrice, 0) / props.input.length
score -= periods.reduce((acc, cur) => {
if (cur.activity != 0 && cur.charge == 0) return acc + cur.duration * averagePrice / 60
else return acc
}, 0)

return cost
return score
}

module.exports = {
cost,
fitnessFunction,
splitIntoHourIntervals,
allPeriodsGenerator,
Expand Down
7 changes: 3 additions & 4 deletions src/strategy-battery-charging-functions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const geneticalgorithm = require('geneticalgorithm')
const geneticAlgorithmConstructor = require('geneticalgorithm')
const { fitnessFunction, allPeriodsGenerator } = require('./fitness')
const { fitnessFunction, allPeriodsGenerator, allPeriods, cost } = require('./fitness')

const random = (min, max) => {
return Math.floor(Math.random() * (max - min)) + min
Expand Down Expand Up @@ -247,12 +246,12 @@ const calculateBatteryChargingStrategy = (config) => {
best: {
schedule: toSchedule(props, best),
excessPvEnergyUse: best.excessPvEnergyUse,
cost: options.fitnessFunction(best) * -1,
cost: cost(allPeriods(props, best))
},
noBattery: {
schedule: toSchedule(props, noBattery),
excessPvEnergyUse: noBattery.excessPvEnergyUse,
cost: options.fitnessFunction(noBattery) * -1,
cost: cost(allPeriods(props, noBattery))
},
}
}
Expand Down
27 changes: 24 additions & 3 deletions test/fitness.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,38 @@ describe('Fitness', () => {
expect(score).toEqual(-3.5)
})

test('should calculate fitness penalty for empty charge', () => {
props.totalDuration = 180
props.soc = 0
const score1 = fitnessFunction(props)({
periods: [
{ start: 0, duration: 180, activity: -1 },
],
excessPvEnergyUse: 0,
})
expect(score1).toEqual(-6)

props.soc = 1
const score2 = fitnessFunction(props)({
periods: [
{ start: 0, duration: 180, activity: 1 },
],
excessPvEnergyUse: 0,
})
expect(score2).toEqual(-6)
})

test('should calculate fitness with soc', () => {
props.totalDuration = 120
props.soc = 1
props.soc = 0
const score = fitnessFunction(props)({
periods: [
{ start: 30, duration: 60, activity: 1 },
{ start: 90, duration: 30, activity: -1 },
],
excessPvEnergyUse: 0,
})
expect(score).toEqual(-1.5)
expect(score).toEqual(-2.5)
})

test('should calculate 180 min charge period with full battery', () => {
Expand Down Expand Up @@ -457,6 +478,6 @@ describe('Fitness', () => {
periods: [{ start: 0, duration: 180, activity: 1 }],
excessPvEnergyUse: 0,
})
expect(score).toEqual(-1501.5)
expect(score).toEqual(-2502.5)
})
})

0 comments on commit 5c8a165

Please sign in to comment.