forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/Implement logic to construct charge graph #203
Closed
Closed
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
38920b4
feat: implement logic to retrieve charge stations from graph.
CodeBear801 f177d45
fix: add unit test for generate stations.
CodeBear801 a7a48be
fix: fix logic in charge strategy.
CodeBear801 5df787d
fix: modify unit test.
CodeBear801 dc4d8b1
fix: update code and unit test.
CodeBear801 2790f0f
fix: remove no use comments.
CodeBear801 ad434e9
fix: update name
CodeBear801 ddee568
fix typo.
CodeBear801 1af4fbf
upate code.
CodeBear801 bf58f07
fix: change name from ChargingStatus to State
CodeBear801 7771a6b
fix: rename from solutionformat -> solution
CodeBear801 7083c16
Revert "fix: rename from solutionformat -> solution"
CodeBear801 f9bca74
fix: rename solutionformat -> solution
CodeBear801 74913d2
fix: adjust unit tests.
CodeBear801 d4c7528
fix: Move floatequal to integration/util.
CodeBear801 6cf7d90
Merge branch 'master' into feature/oasis-integration2
CodeBear801 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,101 @@ | ||
package chargingstrategy | ||
|
||
import ( | ||
"github.com/golang/glog" | ||
) | ||
|
||
type fakeChargingStrategyCreator struct { | ||
maxEnergyLevel float64 | ||
arrivalEnergyLevel float64 | ||
maxEnergyLevel float64 | ||
} | ||
|
||
// NewFakeChargingStrategyCreator creates fake charging strategy | ||
func NewFakeChargingStrategyCreator(maxEnergyLevel float64) *fakeChargingStrategyCreator { | ||
func NewFakeChargingStrategyCreator(arrivalEnergyLevel, maxEnergyLevel float64) *fakeChargingStrategyCreator { | ||
return &fakeChargingStrategyCreator{ | ||
maxEnergyLevel: maxEnergyLevel, | ||
arrivalEnergyLevel: arrivalEnergyLevel, | ||
maxEnergyLevel: maxEnergyLevel, | ||
} | ||
} | ||
|
||
// @todo: | ||
// - Influence of returning candidate with no charge time and additional energy | ||
// CreateChargingStrategies returns different charging strategy | ||
// Initial implementation: 1 hour charge for 60% of max energy, | ||
// 2 hour charge for 80% | ||
// 4 hour charge for 100% | ||
func (f *fakeChargingStrategyCreator) CreateChargingStrategies() []ChargingStrategy { | ||
|
||
return []ChargingStrategy{ | ||
ChargingStrategy{ | ||
ChargingTime: 3600, | ||
ChargingEnergy: f.maxEnergyLevel * 0.6, | ||
}, | ||
ChargingStrategy{ | ||
ChargingTime: 7200, | ||
ChargingEnergy: f.maxEnergyLevel * 0.8, | ||
}, | ||
ChargingStrategy{ | ||
ChargingTime: 14400, | ||
ChargingEnergy: f.maxEnergyLevel, | ||
}, | ||
} | ||
} | ||
|
||
// Fake charge strategy | ||
// From empty energy: | ||
// 1 hour charge to 60% of max energy | ||
// 2 hour charge to 80%, means from 60% ~ 80% need 1 hour | ||
// 4 hour charge to 100%, means from 80% ~ 100% need 2 hours | ||
func (f *fakeChargingStrategyCreator) EvaluateCost(arrivalEnergy float64, targetState ChargingStrategy) ChargingCost { | ||
sixtyPercentOfMaxEnergy := f.maxEnergyLevel * 0.6 | ||
eightyPercentOfMaxEnergy := f.maxEnergyLevel * 0.8 | ||
noNeedCharge := ChargingCost{ | ||
Duration: 0.0, | ||
} | ||
|
||
if arrivalEnergy > targetState.ChargingEnergy || | ||
floatEquals(targetState.ChargingEnergy, 0.0) { | ||
return noNeedCharge | ||
} | ||
|
||
totalTime := 0.0 | ||
currentEnergy := arrivalEnergy | ||
if arrivalEnergy < sixtyPercentOfMaxEnergy { | ||
energyNeeded4Stage1 := sixtyPercentOfMaxEnergy - arrivalEnergy | ||
totalTime += energyNeeded4Stage1 / sixtyPercentOfMaxEnergy * 3600.0 | ||
currentEnergy = sixtyPercentOfMaxEnergy | ||
} | ||
|
||
if floatEquals(targetState.ChargingEnergy, sixtyPercentOfMaxEnergy) { | ||
return ChargingCost{ | ||
Duration: totalTime, | ||
} | ||
} | ||
|
||
if arrivalEnergy < eightyPercentOfMaxEnergy { | ||
energyNeeded4Stage2 := eightyPercentOfMaxEnergy - currentEnergy | ||
totalTime += energyNeeded4Stage2 / (eightyPercentOfMaxEnergy - sixtyPercentOfMaxEnergy) * 3600.0 | ||
currentEnergy = eightyPercentOfMaxEnergy | ||
} | ||
if floatEquals(targetState.ChargingEnergy, eightyPercentOfMaxEnergy) { | ||
return ChargingCost{ | ||
Duration: totalTime, | ||
} | ||
} | ||
|
||
if arrivalEnergy < f.maxEnergyLevel { | ||
energyNeeded4Stage3 := f.maxEnergyLevel - currentEnergy | ||
totalTime += energyNeeded4Stage3 / (f.maxEnergyLevel - eightyPercentOfMaxEnergy) * 7200.0 | ||
} | ||
|
||
if floatEquals(targetState.ChargingEnergy, f.maxEnergyLevel) { | ||
return ChargingCost{ | ||
Duration: totalTime, | ||
} | ||
} | ||
|
||
glog.Fatalf("Invalid charging state %#v\n", targetState) | ||
return noNeedCharge | ||
} | ||
|
||
var epsilon float64 = 0.00000001 | ||
|
||
func floatEquals(a, b float64) bool { | ||
if (a-b) < epsilon && (b-a) < epsilon { | ||
return true | ||
} | ||
return false | ||
} |
71 changes: 71 additions & 0 deletions
71
integration/oasis/chargingstrategy/chargingstrategy_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package chargingstrategy | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestFakeChargingStrategyCreator(t *testing.T) { | ||
cases := []struct { | ||
arrivalEnergyLevel float64 | ||
maxEnergyLevel float64 | ||
expectResult []ChargingCost | ||
}{ | ||
{ | ||
10000, | ||
50000, | ||
[]ChargingCost{ | ||
ChargingCost{ | ||
Duration: 2400.0, | ||
}, | ||
ChargingCost{ | ||
Duration: 6000.0, | ||
}, | ||
ChargingCost{ | ||
Duration: 13200.0, | ||
}, | ||
}, | ||
}, | ||
{ | ||
32000, | ||
50000, | ||
[]ChargingCost{ | ||
ChargingCost{ | ||
Duration: 0.0, | ||
}, | ||
ChargingCost{ | ||
Duration: 2880.0, | ||
}, | ||
ChargingCost{ | ||
Duration: 10080.0, | ||
}, | ||
}, | ||
}, | ||
{ | ||
41000, | ||
50000, | ||
[]ChargingCost{ | ||
ChargingCost{ | ||
Duration: 0.0, | ||
}, | ||
ChargingCost{ | ||
Duration: 0.0, | ||
}, | ||
ChargingCost{ | ||
Duration: 6480.0, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
var actualResult []ChargingCost | ||
strategy := NewFakeChargingStrategyCreator(c.arrivalEnergyLevel, c.maxEnergyLevel) | ||
for _, state := range strategy.CreateChargingStrategies() { | ||
actualResult = append(actualResult, strategy.EvaluateCost(c.arrivalEnergyLevel, state)) | ||
} | ||
if !reflect.DeepEqual(actualResult, c.expectResult) { | ||
t.Errorf("parse case %#v, expect\n %#v but got\n %#v", c, c.expectResult, actualResult) | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
integration/oasis/chargingstrategy/null_charge_strategy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package chargingstrategy | ||
|
||
type nullChargeStrategy struct { | ||
} | ||
|
||
// NewNullChargeStrategy creates nullChargeStrategy used to bypass unit tests | ||
func NewNullChargeStrategy() *nullChargeStrategy { | ||
return &nullChargeStrategy{} | ||
} | ||
|
||
func (f *nullChargeStrategy) CreateChargingStrategies() []ChargingStrategy { | ||
return []ChargingStrategy{} | ||
} | ||
|
||
func (f *nullChargeStrategy) EvaluateCost(arrivalEnergy float64, targetState ChargingStrategy) ChargingCost { | ||
return ChargingCost{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
package solutionformat defines internal foramt for recording single solution result. | ||
It isolates internal calculation logic and final restful response: | ||
- Internal algorithm will calculate result in this format. | ||
- In the end, oasis will use this format to generate restful response. | ||
*/ | ||
package solutionformat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package solutionformat | ||
|
||
// Solution contains summary and selected charge stations | ||
type Solution struct { | ||
Distance float64 | ||
Duration float64 | ||
RemainingRage float64 | ||
Weight float64 | ||
ChargeStations []*ChargeStation | ||
} | ||
|
||
// ChargeStation contains all information related with specific charge station | ||
type ChargeStation struct { | ||
Location Location | ||
StationID string | ||
ArrivalEnergy float64 | ||
WaitTime float64 | ||
ChargeTime float64 | ||
ChargeRange float64 | ||
} | ||
|
||
// Location defines the geo location of a station | ||
type Location struct { | ||
Lat float64 | ||
Lon float64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create charging candidates