Skip to content
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

fix: refactor code for oasis service, more details please go to #223 #224

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions integration/oasis/chargingstrategy/charge_strategy_creator.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ import (
"github.com/golang/glog"
)

type fakeChargingStrategyCreator struct {
type fakeChargeStrategy struct {
maxEnergyLevel float64
}

// NewFakeChargingStrategyCreator creates fake charging strategy
func NewFakeChargingStrategyCreator(maxEnergyLevel float64) *fakeChargingStrategyCreator {
return &fakeChargingStrategyCreator{
// NewFakeChargingStrategy creates fake charge strategy
func NewFakeChargingStrategy(maxEnergyLevel float64) *fakeChargeStrategy {
return &fakeChargeStrategy{
maxEnergyLevel: maxEnergyLevel,
}
}

// @todo:
// - Influence of returning candidate with no charge time and additional energy
// CreateChargingStrategies returns different charging strategy
func (f *fakeChargingStrategyCreator) CreateChargingStrategies() []State {
// CreateChargingStates returns different charging strategy
func (f *fakeChargeStrategy) CreateChargingStates() []State {
return []State{
State{
ChargingEnergy: f.maxEnergyLevel * 0.6,
Energy: f.maxEnergyLevel * 0.6,
},
State{
ChargingEnergy: f.maxEnergyLevel * 0.8,
Energy: f.maxEnergyLevel * 0.8,
},
State{
ChargingEnergy: f.maxEnergyLevel,
Energy: f.maxEnergyLevel,
},
}
}
Expand All @@ -38,15 +38,15 @@ func (f *fakeChargingStrategyCreator) CreateChargingStrategies() []State {
// 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 State) ChargingCost {
func (f *fakeChargeStrategy) EvaluateCost(arrivalEnergy float64, targetState State) ChargingCost {
sixtyPercentOfMaxEnergy := f.maxEnergyLevel * 0.6
eightyPercentOfMaxEnergy := f.maxEnergyLevel * 0.8
noNeedCharge := ChargingCost{
Duration: 0.0,
}

if arrivalEnergy > targetState.ChargingEnergy ||
util.FloatEquals(targetState.ChargingEnergy, 0.0) {
if arrivalEnergy > targetState.Energy ||
util.FloatEquals(targetState.Energy, 0.0) {
return noNeedCharge
}

Expand All @@ -58,7 +58,7 @@ func (f *fakeChargingStrategyCreator) EvaluateCost(arrivalEnergy float64, target
currentEnergy = sixtyPercentOfMaxEnergy
}

if util.FloatEquals(targetState.ChargingEnergy, sixtyPercentOfMaxEnergy) {
if util.FloatEquals(targetState.Energy, sixtyPercentOfMaxEnergy) {
return ChargingCost{
Duration: totalTime,
}
Expand All @@ -69,7 +69,7 @@ func (f *fakeChargingStrategyCreator) EvaluateCost(arrivalEnergy float64, target
totalTime += energyNeeded4Stage2 / (eightyPercentOfMaxEnergy - sixtyPercentOfMaxEnergy) * 3600.0
currentEnergy = eightyPercentOfMaxEnergy
}
if util.FloatEquals(targetState.ChargingEnergy, eightyPercentOfMaxEnergy) {
if util.FloatEquals(targetState.Energy, eightyPercentOfMaxEnergy) {
return ChargingCost{
Duration: totalTime,
}
Expand All @@ -80,7 +80,7 @@ func (f *fakeChargingStrategyCreator) EvaluateCost(arrivalEnergy float64, target
totalTime += energyNeeded4Stage3 / (f.maxEnergyLevel - eightyPercentOfMaxEnergy) * 7200.0
}

if util.FloatEquals(targetState.ChargingEnergy, f.maxEnergyLevel) {
if util.FloatEquals(targetState.Energy, f.maxEnergyLevel) {
return ChargingCost{
Duration: totalTime,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func TestFakeChargingStrategyCreator(t *testing.T) {

for _, c := range cases {
var actualResult []ChargingCost
strategy := NewFakeChargingStrategyCreator(c.maxEnergyLevel)
for _, state := range strategy.CreateChargingStrategies() {
strategy := NewFakeChargingStrategy(c.maxEnergyLevel)
for _, state := range strategy.CreateChargingStates() {
actualResult = append(actualResult, strategy.EvaluateCost(c.arrivalEnergyLevel, state))
}
if !reflect.DeepEqual(actualResult, c.expectResult) {
Expand Down
2 changes: 1 addition & 1 deletion integration/oasis/chargingstrategy/null_charge_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func NewNullChargeStrategy() *nullChargeStrategy {
return &nullChargeStrategy{}
}

func (f *nullChargeStrategy) CreateChargingStrategies() []State {
func (f *nullChargeStrategy) CreateChargingStates() []State {
return []State{}
}

Expand Down
30 changes: 30 additions & 0 deletions integration/oasis/chargingstrategy/strategy_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package chargingstrategy

// State contains charging related information
type State struct {
Energy float64
}

// ChargingCost represents the cost needed to reach certain states
type ChargingCost struct {
Duration float64
// Later could add money usage, etc
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be done. Plan to move to 'oasis/type`


// Creator creates charge states based on different charge strategy
type Creator interface {
// CreateChargingStates creates charge States which could be used by other algorithm
CreateChargingStates() []State
}

// Evaluator calculate cost from given status(energy, etc) to target State
type Evaluator interface {
// EvaluateCost accepts current status and target status and returns cost needed
EvaluateCost(arrivalEnergy float64, targetState State) ChargingCost
}

// Strategy defines interface related with creation and evaluation
type Strategy interface {
Creator
Evaluator
}
14 changes: 3 additions & 11 deletions integration/oasis/has_enough_energy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/Telenav/osrm-backend/integration/pkg/api/osrm/route"
"github.com/Telenav/osrm-backend/integration/util"
)

func TestHasEnoughEnergyPositive1(t *testing.T) {
Expand All @@ -20,7 +21,7 @@ func TestHasEnoughEnergyPositive1(t *testing.T) {
}

expect := 10000.0
if !floatEquals(remainRange, expect) {
if !util.FloatEquals(remainRange, expect) {
t.Errorf("Incorrect remaining range calculated, expect %s while actual value is %s", strconv.FormatFloat(expect, 'f', -1, 64), strconv.FormatFloat(remainRange, 'f', -1, 64))
}

Expand All @@ -39,16 +40,7 @@ func TestHasEnoughEnergyPositive2(t *testing.T) {
}

expect := 0.0
if !floatEquals(remainRange, expect) {
if !util.FloatEquals(remainRange, expect) {
t.Errorf("Incorrect remaining range calculated, expect %s while actual value is %s", strconv.FormatFloat(expect, 'f', -1, 64), strconv.FormatFloat(remainRange, 'f', -1, 64))
}
}

var epsilon float64 = 0.00000001

func floatEquals(a, b float64) bool {
if (a-b) < epsilon && (b-a) < epsilon {
return true
}
return false
}
17 changes: 6 additions & 11 deletions integration/oasis/haversine/distance_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package haversine

import "testing"
import (
"testing"

"github.com/Telenav/osrm-backend/integration/util"
)

func TestGreatCircleDistance(t *testing.T) {
// expect value got from http://www.onlineconversion.com/map_greatcircle_distance.htm
expect := 111595.4865288326
actual := GreatCircleDistance(32.333, 122.323, 31.333, 122.423)
if !floatEquals(expect, actual) {
if !util.FloatEquals(expect, actual) {
t.Errorf("Expected GreatCircleDistance returns %v, got %v", expect, actual)
}
}

var epsilon float64 = 0.00000001

func floatEquals(a, b float64) bool {
if (a-b) < epsilon && (b-a) < epsilon {
return true
}
return false
}
2 changes: 1 addition & 1 deletion integration/oasis/reachable_by_multiple_charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func pickChargeStationsWithEarlistArrival(oasisReq *oasis.Request, routeResp *ro
// for _, locations := range chargeLocations {
// c := stationfinder.CalculateWeightBetweenNeighbors(locations, oc, sc)
// sol := stationgraph.NewStationGraph(c, oasisReq.CurrRange, oasisReq.MaxRange,
// chargingstrategy.NewFakeChargingStrategyCreator(oasisReq.MaxRange)).GenerateChargeSolutions()
// chargingstrategy.NewFakeChargingStrategy(oasisReq.MaxRange)).GenerateChargeSolutions()
// }
}

Expand Down
14 changes: 3 additions & 11 deletions integration/oasis/stationfinder/stations_iterator_alg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Telenav/osrm-backend/integration/oasis/searchconnector"
"github.com/Telenav/osrm-backend/integration/pkg/api/osrm/table"
"github.com/Telenav/osrm-backend/integration/pkg/api/search/nearbychargestation"
"github.com/Telenav/osrm-backend/integration/util"
)

var mockDict1 map[string]bool = map[string]bool{
Expand Down Expand Up @@ -239,10 +240,10 @@ func TestCalculateWeightBetweenNeighbors(t *testing.T) {

if r.URL.EscapedPath() == "/entity/v4/search/json" {
req, _ := nearbychargestation.ParseRequestURL(r.URL)
if floatEquals(req.Location.Lat, 2.2) && floatEquals(req.Location.Lon, 2.2) {
if util.FloatEquals(req.Location.Lat, 2.2) && util.FloatEquals(req.Location.Lon, 2.2) {
var searchResponseBytes4Location1, _ = json.Marshal(nearbychargestation.MockSearchResponse1)
w.Write(searchResponseBytes4Location1)
} else if floatEquals(req.Location.Lat, 3.3) && floatEquals(req.Location.Lon, 3.3) {
} else if util.FloatEquals(req.Location.Lat, 3.3) && util.FloatEquals(req.Location.Lon, 3.3) {
var searchResponseBytes4Location2, _ = json.Marshal(nearbychargestation.MockSearchResponse3)
w.Write(searchResponseBytes4Location2)
}
Expand Down Expand Up @@ -530,12 +531,3 @@ func TestCalculateWeightBetweenNeighbors(t *testing.T) {
}
}
}

var epsilon float64 = 0.00000001

func floatEquals(a, b float64) bool {
if (a-b) < epsilon && (b-a) < epsilon {
return true
}
return false
}
2 changes: 1 addition & 1 deletion integration/oasis/stationgraph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type graph struct {
nodes []*node
startNodeID nodeID
endNodeID nodeID
strategy chargingstrategy.ChargingStrategyCreator
strategy chargingstrategy.Strategy
}

func (g *graph) dijkstra() []nodeID {
Expand Down
Loading