Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
balasan committed Nov 5, 2020
1 parent bb6cdc8 commit f516862
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 22 deletions.
14 changes: 13 additions & 1 deletion deterministic/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// TODO: edge case (only impacts display) - if a node has no inputs we should set its score to 0 to avoid
// a stale score if all of nodes inputs are cancelled out
// would need to keep track of node inputs...
package reputation_det
package repDet

import (
"strconv"
Expand All @@ -30,6 +30,7 @@ const (
Negative
)

// Default decimals used in computation
const Decimals = 18

// this is defines the cutoff for when a node will have it's outging links counted
Expand All @@ -38,12 +39,14 @@ const Decimals = 18
// otherwise, we counter the outgoing lings with one 'heavy' link proportional to the MaxNegOffset ratio
const MaxNegOffset = 10

// Node input struct for creating nodes and edges
type NodeInput struct {
Id string
PRank sdk.Uint
NRank sdk.Uint
}

// Internal node struct
type Node struct {
id string
rank sdk.Uint // pos page rank of the node
Expand All @@ -63,6 +66,10 @@ type Graph struct {
MaxNegOffset sdk.Uint
}

// Pagerank params
// α is the probably the person will not teleport
// ε is the min global error between iterations
// personalization is the personalization vector (can be nil for non-personalized pr)
type RankParams struct {
α, ε sdk.Uint
personalization []string
Expand Down Expand Up @@ -145,6 +152,7 @@ func (graph *Graph) Link(source, target NodeInput, weight sdk.Int) {
graph.cancelOpposites(*sourceNode, target.Id, nodeType)
}

// Finalize is the method that runs after all other inits and before pagerank
func (graph *Graph) Finalize() {
graph.processNegatives()
}
Expand Down Expand Up @@ -240,10 +248,12 @@ 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)
}

// 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{
Expand All @@ -267,13 +277,15 @@ func (graph *Graph) initNode(key string, inputNode NodeInput, nodeType NodeType)
return graph.nodes[key]
}

// removeEdge removes edge from graph
func (graph *Graph) removeEdge(source string, target string) {
delete(graph.edges[source], target)
if len(graph.edges[source]) == 0 {
delete(graph.edges, source)
}
}

// getKey returns node id for positive nodes and <id>_neg for negative nodes
func getKey(key string, nodeType NodeType) string {
if nodeType == Positive {
return key
Expand Down
2 changes: 1 addition & 1 deletion deterministic/pagerank.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reputation_det
package repDet

import (
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
2 changes: 1 addition & 1 deletion deterministic/pagerank_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reputation_det
package repDet

import (
"reflect"
Expand Down
8 changes: 1 addition & 7 deletions deterministic/processResults.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package reputation_det
package repDet

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

type Results struct {
rankPos float64
rankNeg float64
id string
}

func (graph Graph) processResults(callback func(id string, pRank sdk.Uint, nRank sdk.Uint)) {
graph.mergeNegatives()
for key, node := range graph.nodes {
Expand Down
2 changes: 1 addition & 1 deletion deterministic/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reputation_det
package repDet

import (
"math"
Expand Down
19 changes: 17 additions & 2 deletions non-deterministic/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
// TODO: edge case (only impacts display) - if a node has no inputs we should set its score to 0 to avoid
// a stale score if all of nodes inputs are cancelled out
// would need to keep track of node inputs...
package reputation
package rep

import (
"math"
"strconv"
)

// this is 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)

// NodeType is positive or negative
Expand All @@ -31,12 +35,14 @@ const (
Negative
)

// Node input struct for creating nodes and edges
type NodeInput struct {
Id string
PRank float64
NRank float64
}

// Internal node struct
type Node struct {
id string
rank float64 // pos page rank of the node
Expand All @@ -54,9 +60,13 @@ type Graph struct {
negConsumer NodeInput
}

// Pagerank params
// α is the probably the person will not teleport
// ε is the min global error between iterations
// personalization is the personalization vector (can be nil for non-personalized pr)
type RankParams struct {
α, ε float64
personalization []string
personalization []string // array of ids
}

// NewGraph initializes and returns a new graph.
Expand Down Expand Up @@ -120,6 +130,7 @@ func (graph *Graph) Link(source, target NodeInput, weight float64) {
graph.cancelOpposites(*sourceNode, target.Id, nodeType)
}

// Finalize is the method that runs after all other inits and before pagerank
func (graph *Graph) Finalize() {
graph.processNegatives()
}
Expand Down Expand Up @@ -207,10 +218,12 @@ 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)
}

// 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{
Expand All @@ -232,13 +245,15 @@ func (graph *Graph) initNode(key string, inputNode NodeInput, nodeType NodeType)
return graph.nodes[key]
}

// removeEdge removes edge from graph
func (graph *Graph) removeEdge(source string, target string) {
delete(graph.edges[source], target)
if len(graph.edges[source]) == 0 {
delete(graph.edges, source)
}
}

// getKey returns node id for positive nodes and <id>_neg for negative nodes
func getKey(key string, nodeType NodeType) string {
if nodeType == Positive {
return key
Expand Down
2 changes: 1 addition & 1 deletion non-deterministic/pagerank.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reputation
package rep

import (
"math"
Expand Down
2 changes: 1 addition & 1 deletion non-deterministic/pagerank_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reputation
package rep

import (
"reflect"
Expand Down
8 changes: 1 addition & 7 deletions non-deterministic/processResults.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
package reputation

type Results struct {
rankPos float64
rankNeg float64
id string
}
package rep

func (graph Graph) processResults(callback func(id string, pRank float64, nRank float64)) {
graph.mergeNegatives()
Expand Down

0 comments on commit f516862

Please sign in to comment.