Skip to content

Commit

Permalink
full lint
Browse files Browse the repository at this point in the history
  • Loading branch information
balasan committed Nov 5, 2020
1 parent b3759d3 commit 5086f11
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
26 changes: 13 additions & 13 deletions deterministic/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ const (
Negative
)

// Default decimals used in computation
// Decimals is the default decimals used in computation
const Decimals = 18

// MAX_NEG_OFFSET defines the cutoff for when a node will have it's outging links counted
// MaxNegOffset defines the cutoff for when a node will have it's outging links counted
// if previously NegativeRank / PositiveRank > MaxNegOffset / (MaxNegOffset + 1) we will not consider
// any outgoing links
// otherwise, we counter the outgoing lings with one 'heavy' link proportional to the MaxNegOffset ratio
Expand All @@ -42,12 +42,12 @@ const MaxNegOffset = 10
// NodeInput is struct passing data to the graph
// TODO does it make sense to just use the Node type?
type NodeInput struct {
Id string
ID string
PRank sdk.Uint
NRank sdk.Uint
}

// Internal node struct
// Node is an internal node struct
type Node struct {
id string
rank sdk.Uint // pos page rank of the node
Expand Down Expand Up @@ -87,22 +87,22 @@ func NewGraph(α sdk.Uint, ε sdk.Uint, negConsumerRank sdk.Uint) *Graph {
ε: ε,
personalization: make([]string, 0),
},
negConsumer: NodeInput{Id: "negConsumer", PRank: negConsumerRank, NRank: sdk.ZeroUint()},
negConsumer: NodeInput{ID: "negConsumer", PRank: negConsumerRank, NRank: sdk.ZeroUint()},
Precision: sdk.NewUintFromBigInt(sdk.NewIntWithDecimal(1, Decimals).BigInt()),
MaxNegOffset: sdk.NewUintFromBigInt(sdk.NewIntWithDecimal(MaxNegOffset, Decimals).BigInt()),
}
}

// NewNodeInput is ahelper method to create a node input struct
func NewNodeInput(id string, pRank sdk.Uint, nRank sdk.Uint) NodeInput {
return NodeInput{Id: id, PRank: pRank, NRank: nRank}
return NodeInput{ID: id, PRank: pRank, NRank: nRank}
}

// AddPersonalizationNode adds a node to the pagerank personlization vector
// these nodes will have high rank by default and all other rank will stem from them
// this makes non-personalaziation nodes sybil resistant (they cannot increase their own rank)
func (graph *Graph) AddPersonalizationNode(pNode NodeInput) {
graph.params.personalization = append(graph.params.personalization, pNode.Id)
graph.params.personalization = append(graph.params.personalization, pNode.ID)
// this to ensures source nodes exist
graph.InitPosNode(pNode)
}
Expand All @@ -120,7 +120,7 @@ func (graph *Graph) Link(source, target NodeInput, weight sdk.Int) {
}
}

sourceKey := getKey(source.Id, Positive)
sourceKey := getKey(source.ID, Positive)
sourceNode := graph.initNode(sourceKey, source, Positive)

// if weight is negative we use negative receiving node
Expand All @@ -133,7 +133,7 @@ func (graph *Graph) Link(source, target NodeInput, weight sdk.Int) {
nodeType = Positive
weightUint = sdk.NewUintFromBigInt(weight.BigInt())
}
targetKey := getKey(target.Id, nodeType)
targetKey := getKey(target.ID, nodeType)

graph.initNode(targetKey, target, nodeType)

Expand All @@ -150,7 +150,7 @@ func (graph *Graph) Link(source, target NodeInput, weight sdk.Int) {
graph.edges[sourceKey][targetKey] = graph.edges[sourceKey][targetKey].Add(weightUint)

// note: use target.id here to make sure we reference the original id
graph.cancelOpposites(*sourceNode, target.Id, nodeType)
graph.cancelOpposites(*sourceNode, target.ID, nodeType)
}

// Finalize is the method that runs after all other inits and before pagerank
Expand Down Expand Up @@ -179,7 +179,7 @@ func (graph *Graph) processNegatives() {
if posNode.rank.IsZero() || negNode.rank.IsZero() {
return
}
negConsumer := graph.initNode(negConsumerInput.Id, negConsumerInput, Positive)
negConsumer := graph.initNode(negConsumerInput.ID, negConsumerInput, Positive)

one := graph.Precision

Expand Down Expand Up @@ -251,14 +251,14 @@ func (graph *Graph) cancelOpposites(sourceNode Node, target string, nodeType Nod

// InitPosNode initialized a positive node
func (graph *Graph) InitPosNode(inputNode NodeInput) *Node {
return graph.initNode(inputNode.Id, inputNode, Positive)
return graph.initNode(inputNode.ID, inputNode, Positive)
}

// initNode initialized a node
func (graph *Graph) initNode(key string, inputNode NodeInput, nodeType NodeType) *Node {
if _, ok := graph.nodes[key]; ok == false {
graph.nodes[key] = &Node{
id: inputNode.Id, // id is independent of pos/neg keys
id: inputNode.ID, // id is independent of pos/neg keys
degree: sdk.ZeroUint(),
rank: sdk.ZeroUint(),
rankNeg: sdk.ZeroUint(),
Expand Down
3 changes: 3 additions & 0 deletions deterministic/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ func FtoBD(n float64) sdk.Uint {
return sdk.NewUint(uint64(n * math.Pow(10, float64(Decimals))))
}

// NewGraphHelper is test helper that allows use of floats
func NewGraphHelper(α, ε float64, negConsumerRank sdk.Uint) *Graph {
return NewGraph(FtoBD(α), FtoBD(ε), negConsumerRank)
}

// NewNodeInputHelper is test helper that allows use of floats
func NewNodeInputHelper(id string, pRank float64, nRank float64) NodeInput {
return NewNodeInput(id, FtoBD(pRank), FtoBD(nRank))
}

// LinkHelper is test helper that allows use of floats
func (graph Graph) LinkHelper(source, target NodeInput, weight float64) {
weightInt := sdk.NewInt(int64(weight * math.Pow(10, float64(Decimals/2))))
weightInt = weightInt.Mul(sdk.NewInt(int64(math.Pow(10, float64(Decimals/2)))))
Expand Down
30 changes: 15 additions & 15 deletions non-deterministic/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"strconv"
)

// this is defines the cutoff for when a node will have it's outging links counted
// MaxNegOffset defines the cutoff for when a node will have it's outging links counted
// if previously NegativeRank / PositiveRank > MaxNegOffset / (MaxNegOffset + 1) we will not consider
// any outgoing links
// otherwise, we counter the outgoing lings with one 'heavy' link proportional to the MaxNegOffset ratio
const MAX_NEG_OFFSET = float64(10)
const MaxNegOffset = float64(10)

// NodeType is positive or negative
// each node in the graph can be represented by two nodes,
Expand All @@ -38,7 +38,7 @@ const (
// NodeInput is struct passing data to the graph
// TODO does it make sense to just use the Node type?
type NodeInput struct {
Id string
ID string
PRank float64
NRank float64
}
Expand Down Expand Up @@ -81,20 +81,20 @@ func NewGraph(α, ε, negConsumerRank float64) *Graph {
ε: ε,
personalization: make([]string, 0),
},
negConsumer: NodeInput{Id: "negConsumer", PRank: negConsumerRank, NRank: 0},
negConsumer: NodeInput{ID: "negConsumer", PRank: negConsumerRank, NRank: 0},
}
}

// NewNodeInput is ahelper method to create a node input struct
func NewNodeInput(id string, pRank float64, nRank float64) NodeInput {
return NodeInput{Id: id, PRank: pRank, NRank: nRank}
return NodeInput{ID: id, PRank: pRank, NRank: nRank}
}

// AddPersonalizationNode adds a node to the pagerank personlization vector
// these nodes will have high rank by default and all other rank will stem from them
// this makes non-personalaziation nodes sybil resistant (they cannot increase their own rank)
func (graph *Graph) AddPersonalizationNode(pNode NodeInput) {
graph.params.personalization = append(graph.params.personalization, pNode.Id)
graph.params.personalization = append(graph.params.personalization, pNode.ID)
// this to ensures source nodes exist
graph.InitPosNode(pNode)
}
Expand All @@ -104,19 +104,19 @@ func (graph *Graph) AddPersonalizationNode(pNode NodeInput) {
func (graph *Graph) Link(source, target NodeInput, weight float64) {

// if a node's neg/post rank ration is too high we don't process its links
if source.PRank > 0 && source.NRank/source.PRank > MAX_NEG_OFFSET/(MAX_NEG_OFFSET+1) {
if source.PRank > 0 && source.NRank/source.PRank > MaxNegOffset/(MaxNegOffset+1) {
return
}

sourceKey := getKey(source.Id, Positive)
sourceKey := getKey(source.ID, Positive)
sourceNode := graph.initNode(sourceKey, source, Positive)

// if weight is negative we use negative receiving node
var nodeType NodeType
if nodeType = Positive; weight < 0 {
nodeType = Negative
}
targetKey := getKey(target.Id, nodeType)
targetKey := getKey(target.ID, nodeType)

graph.initNode(targetKey, target, nodeType)

Expand All @@ -128,7 +128,7 @@ func (graph *Graph) Link(source, target NodeInput, weight float64) {
graph.edges[sourceKey][targetKey] += math.Abs(weight)

// note: use target.id here to make sure we reference the original id
graph.cancelOpposites(*sourceNode, target.Id, nodeType)
graph.cancelOpposites(*sourceNode, target.ID, nodeType)
}

// Finalize is the method that runs after all other inits and before pagerank
Expand Down Expand Up @@ -161,14 +161,14 @@ func (graph *Graph) processNegatives() {
panic("negative ranking nodes should not have any degree") // this should never happen
}

negConsumer := graph.initNode(negConsumerInput.Id, negConsumerInput, Positive)
negConsumer := graph.initNode(negConsumerInput.ID, negConsumerInput, Positive)

var negMultiple float64

// this first case should not happen because we ignore these links
if negNode.rank/posNode.rank > MAX_NEG_OFFSET/(MAX_NEG_OFFSET+1) {
if negNode.rank/posNode.rank > MaxNegOffset/(MaxNegOffset+1) {
// cap the degree multiple at MAX_NEG_OFFSET
negMultiple = MAX_NEG_OFFSET
negMultiple = MaxNegOffset
} else {
negMultiple = 1/(1-negNode.rank/posNode.rank) - 1
}
Expand Down Expand Up @@ -221,14 +221,14 @@ func (graph *Graph) cancelOpposites(sourceNode Node, target string, nodeType Nod

// InitPosNode is a helper method that initializes a positive node
func (graph *Graph) InitPosNode(inputNode NodeInput) *Node {
return graph.initNode(inputNode.Id, inputNode, Positive)
return graph.initNode(inputNode.ID, inputNode, Positive)
}

// initNode initializes a node
func (graph *Graph) initNode(key string, inputNode NodeInput, nodeType NodeType) *Node {
if _, ok := graph.nodes[key]; ok == false {
graph.nodes[key] = &Node{
id: inputNode.Id, // id is independent of pos/neg keys
id: inputNode.ID, // id is independent of pos/neg keys
degree: 0,
nodeType: nodeType,
}
Expand Down
8 changes: 4 additions & 4 deletions non-deterministic/pagerank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ func TestMaxNeg(t *testing.T) {

graph.AddPersonalizationNode(a)

graph.Link(a, b, MAX_NEG_OFFSET+1)
graph.Link(a, c, MAX_NEG_OFFSET+2)
graph.Link(a, b, MaxNegOffset+1)
graph.Link(a, c, MaxNegOffset+2)
graph.Link(c, d, 1.0)
graph.Link(b, d, -1.0)
graph.Link(d, e, 1.0)
Expand Down Expand Up @@ -357,8 +357,8 @@ func TestMaxNeg(t *testing.T) {

graph.AddPersonalizationNode(a)

graph.Link(a, b, MAX_NEG_OFFSET+1)
graph.Link(a, c, MAX_NEG_OFFSET+2)
graph.Link(a, b, MaxNegOffset+1)
graph.Link(a, c, MaxNegOffset+2)
graph.Link(c, d, 1.0)
graph.Link(b, d, -1.0)
graph.Link(d, e, 1.0)
Expand Down

0 comments on commit 5086f11

Please sign in to comment.