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

More options #52

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ BaseOptions:
MeanRewardPerForward: false
AverageNumberOfHops: false
HopFractionOfTotalRewards: false
NegaticeIncome: false
NegativeIncome: false
IncomeGini: true
IncomeTheil: false
HopIncome: false
Expand Down
2 changes: 2 additions & 0 deletions config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ type experimentOptions struct {
CacheIsEnabled bool `yaml:"CacheIsEnabled"`
PreferredChunks bool `yaml:"PreferredChunks"`
AdjustableThreshold bool `yaml:"AdjustableThreshold"`
VariableRefreshrate bool `yaml:"VariableRefreshrate"`
PayIfOrigPays bool `yaml:"PayIfOrigPays"`
RouteOnlyNearest bool `yaml:"RouteOnlyNearest"`
}

type outputOptions struct {
Expand Down
2 changes: 2 additions & 0 deletions config/default_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func getDefaultConfig() Config {
CacheIsEnabled: false, // false
PreferredChunks: false, // false
AdjustableThreshold: false, // false
VariableRefreshrate: false, // false
RouteOnlyNearest: false, // false
},
}
}
14 changes: 14 additions & 0 deletions config/get_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func IsAdjustableThreshold() bool {
return theconfig.ExperimentOptions.AdjustableThreshold
}

func IsVariableRefreshrate() bool {
return theconfig.ExperimentOptions.VariableRefreshrate
}

func GetAdjustableThresholdExponent() int {
return theconfig.BaseOptions.AdjustableThresholdExponent
}
Expand Down Expand Up @@ -71,6 +75,10 @@ func IsPayIfOrigPays() bool {
return theconfig.ExperimentOptions.PayIfOrigPays
}

func IsRouteOnlyNearest() bool {
return theconfig.ExperimentOptions.RouteOnlyNearest
}

func IsPayOnlyForCurrentRequest() bool {
return theconfig.ExperimentOptions.PayOnlyForCurrentRequest
}
Expand Down Expand Up @@ -322,6 +330,12 @@ func GetExperimentString() (exp string) {
if IsAdjustableThreshold() {
exp += "FgAdj"
}
if !IsPayOnlyForCurrentRequest() {
exp += "FullDept"
}
if IsRouteOnlyNearest() {
exp += "Nearest"
}

exp += "-" + GetExpeimentId()
return exp
Expand Down
4 changes: 2 additions & 2 deletions config/init_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
// theconfig This is the current configuration.
var theconfig Config

func InitConfig() {
config, err := ReadYamlFile("config.yaml")
func InitConfig(configfile string) {
config, err := ReadYamlFile(configfile)
if err != nil {
log.Panicln("Unable to read config file: config.yaml")
}
Expand Down
7 changes: 7 additions & 0 deletions generate_network_data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
To generate a network with the second choice option, you need to use the `config` parameter.
E.g.
```bash
cd generate_network_data
go build
./generate_network_data -random=false -config=true -conffile=../config.yaml
```
3 changes: 2 additions & 1 deletion generate_network_data/generate_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ func main() {
count := flag.Int("count", -1, "generate count many networks with ids i0,i1,...")
random := flag.Bool("random", true, "spread nodes randomly")
useconfig := flag.Bool("config", false, "use config.yaml to initialize bits, binSize, NetworkSize and randomness")
conffile := flag.String("conffile", "config.yaml", "specify config file")

flag.Parse()

if *useconfig {
config.InitConfig()
config.InitConfig(*conffile)
*binSize = config.GetBinSize()
*bits = config.GetBits()
*networkSize = config.GetNetworkSize()
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

func main() {
configfile := flag.String("conf", "config.yaml", "path to config file to use")
graphId := flag.String("graphId", "", "an Id for the graph, e.g. even")
count := flag.Int("count", -1, "run for different networks with ids i0,i1,...")
maxPOs := flag.String("maxPOs", "", "min:max maxPO value")
Expand Down Expand Up @@ -46,18 +47,18 @@ func main() {

for maxPO := min; maxPO < max; maxPO++ {
if *count < 0 {
run(-1, *graphId, maxPO)
run(-1, *graphId, maxPO, *configfile)
}
for i := 0; i < *count; i++ {
run(i, *graphId, maxPO)
run(i, *graphId, maxPO, *configfile)
}
}

}

func run(iteration int, graphId string, maxPO int) {
func run(iteration int, graphId string, maxPO int, configfile string) {
start := time.Now()
config.InitConfig()
config.InitConfig(configfile)
if maxPO > -1 {
config.SetMaxPO(maxPO)
}
Expand Down
80 changes: 69 additions & 11 deletions model/parts/types/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package types

import (
"fmt"
"go-incentive-simulation/config"
"go-incentive-simulation/model/general"
"math"
"sync"
)

Expand All @@ -11,7 +13,7 @@ type Graph struct {
*Network
CurState State
Edges map[NodeId]map[NodeId]*Edge
rwMutex sync.RWMutex
rwMutex sync.RWMutex
}

// Edge that connects to NodesMap with attributes about the connection
Expand All @@ -27,9 +29,10 @@ type Edge struct {
// "lastEpoch" is the epoch where it was last forgiven.
// "threshold" is for the adjustable threshold limit.
type EdgeAttrs struct {
A2B int
LastEpoch int
Threshold int
A2B int
LastEpoch int
Threshold int
Refreshrate int
}

func (g *Graph) GetNodeAdj(nodeId NodeId) [][]NodeId {
Expand All @@ -43,7 +46,7 @@ func (g *Graph) GetNodeAdj(nodeId NodeId) [][]NodeId {
}

// AddEdge will add an edge from a node to a node
func (g *Graph) AddEdge(fromNodeId NodeId, toNodeId NodeId, attrs EdgeAttrs) error {
func (g *Graph) AddEdge(fromNodeId NodeId, toNodeId NodeId) error {
toNode := g.NodesMap[toNodeId]
fromNode := g.NodesMap[fromNodeId]
if toNode == nil || fromNode == nil {
Expand All @@ -55,12 +58,37 @@ func (g *Graph) AddEdge(fromNodeId NodeId, toNodeId NodeId, attrs EdgeAttrs) err
if g.unsafeEdgeExists(toNodeId, fromNodeId) {
mutex = g.Edges[toNodeId][fromNodeId].Mutex
}
newEdge := &Edge{FromNodeId: fromNodeId, ToNodeId: toNodeId, Attrs: attrs, Mutex: mutex}
newEdge := &Edge{FromNodeId: fromNodeId, ToNodeId: toNodeId, Attrs: EdgeAttrs{}, Mutex: mutex}
newEdge.InitThresholdAndRefreshrate()
g.Edges[fromNodeId][toNodeId] = newEdge
return nil
}
}

func (e *Edge) InitThresholdAndRefreshrate() {
threshold := config.GetThreshold()
refreshrate := config.GetRefreshRate()
if config.IsVariableRefreshrate() {
threshold = config.GetMaxProximityOrder()
}

if config.IsAdjustableThreshold() {
threshold = threshold - config.GetBits() + general.BitLength(e.FromNodeId.ToInt()^e.ToNodeId.ToInt())
if threshold < 0 {
threshold = 0
}
refreshrate = GetAdjustedRefreshrate(threshold, config.GetThreshold(), config.GetRefreshRate(), config.GetAdjustableThresholdExponent())
}

e.Attrs.Threshold = threshold
e.Attrs.Refreshrate = refreshrate
}

func GetAdjustedRefreshrate(adjustedThreshold, threshold, refreshRate, power int) int {
ratio := float64(adjustedThreshold) / float64(threshold)
return int(math.Ceil(float64(refreshRate) * math.Pow(ratio, float64(power))))
}

func (g *Graph) NewNode() (*Node, error) {
g.rwMutex.Lock()
node := g.Network.NewNode()
Expand All @@ -71,13 +99,11 @@ func (g *Graph) NewNode() (*Node, error) {
nodeAdj := node.AdjIds
for _, adjItems := range nodeAdj {
for _, otherNodeId := range adjItems {
threshold := general.BitLength(node.Id.ToInt() ^ otherNodeId.ToInt())
attrs := EdgeAttrs{A2B: 0, LastEpoch: 0, Threshold: threshold}
err := g.AddEdge(node.Id, otherNodeId, attrs)
err := g.AddEdge(node.Id, otherNodeId)
if err != nil {
return nil, err
}
err = g.AddEdge(otherNodeId, node.Id, attrs)
err = g.AddEdge(otherNodeId, node.Id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -116,7 +142,7 @@ func (g *Graph) GetEdge(fromNodeId NodeId, toNodeId NodeId) *Edge {
g.rwMutex.Lock()
defer g.rwMutex.Unlock()

err := g.AddEdge(fromNodeId, toNodeId, EdgeAttrs{})
err := g.AddEdge(fromNodeId, toNodeId)
if err != nil {
return nil
}
Expand Down Expand Up @@ -147,6 +173,38 @@ func (g *Graph) unsafeEdgeExists(fromNodeId NodeId, toNodeId NodeId) bool {
return false
}

func (g *Graph) SetEdgeA2B(fromNodeId NodeId, toNodeId NodeId, a2b int) bool {
if g.EdgeExists(fromNodeId, toNodeId) {
g.Edges[fromNodeId][toNodeId].Attrs.A2B = a2b
return true
}
return false
}

func (g *Graph) SetEdgeIncrementThreshold(fromNodeId NodeId, toNodeId NodeId) bool {
if g.EdgeExists(fromNodeId, toNodeId) {
threshold := g.Edges[fromNodeId][toNodeId].Attrs.Threshold
if threshold < config.GetThreshold() {
g.Edges[fromNodeId][toNodeId].Attrs.Threshold++
}
if g.Edges[fromNodeId][toNodeId].Attrs.Refreshrate < config.GetThreshold()/2 {
g.Edges[fromNodeId][toNodeId].Attrs.Refreshrate++
}
return true
}
return false
}

func (g *Graph) SetEdgeDecrementThreshold(fromNodeId NodeId, toNodeId NodeId) bool {
if g.EdgeExists(fromNodeId, toNodeId) {
if g.Edges[fromNodeId][toNodeId].Attrs.Refreshrate > config.GetRefreshRate() {
g.Edges[fromNodeId][toNodeId].Attrs.Refreshrate--
}
return true
}
return false
}

func (g *Graph) SetEdgeData(fromNodeId NodeId, toNodeId NodeId, edgeAttrs EdgeAttrs) bool {
if g.EdgeExists(fromNodeId, toNodeId) {
g.Edges[fromNodeId][toNodeId].Attrs = edgeAttrs
Expand Down
57 changes: 51 additions & 6 deletions model/parts/types/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
"encoding/json"
"fmt"
"go-incentive-simulation/config"
"go-incentive-simulation/model/general"
"math/rand"
"os"
"sync"
Expand Down Expand Up @@ -119,10 +121,10 @@ func (network *Network) node(nodeId NodeId) *Node {
panic("address out of range")
}
res := Node{
Network: network,
Id: nodeId,
AdjIds: make([][]NodeId, network.Bits),
Active: true,
Network: network,
Id: nodeId,
AdjIds: make([][]NodeId, network.Bits),
Active: true,
OriginatorStruct: OriginatorStruct{
RequestCount: 0,
},
Expand Down Expand Up @@ -162,8 +164,14 @@ func (network *Network) node(nodeId NodeId) *Node {
func (network *Network) Generate(count int, random bool) []*Node {
nodeIds := generateIds(count, (1<<network.Bits)-1)
if !random {
nodeIds = generateIdsEven(count, (1<<network.Bits)-1)
if config.GetBits() > 0 {
fmt.Println("second choice!")
nodeIds = generateIdsSecondChoice(count, (1<<network.Bits)-1)
} else {
nodeIds = generateIdsEven(count, (1<<network.Bits)-1)
}
}

nodes := make([]*Node, 0)
for _, i := range nodeIds {
node := network.node(NodeId(i))
Expand Down Expand Up @@ -241,6 +249,44 @@ func generateIds(totalNumbers int, maxValue int) []int {
return result
}

func isneighbor(ida, idb int) bool {
return config.GetBits()-general.BitLength(ida^idb) >= config.GetStorageDepth()
}

func generateIdsSecondChoice(totalNumbers int, maxValue int) []int {
generatedNumbers := make(map[int]bool)
for len(generatedNumbers) < totalNumbers {
num1 := rand.Intn(maxValue-1) + 1
for generatedNumbers[num1] == true {
num1 = rand.Intn(maxValue-1) + 1
}
num2 := rand.Intn(maxValue-1) + 1
for generatedNumbers[num2] == true {
num2 = rand.Intn(maxValue-1) + 1
}
cnt1, cnt2 := 0, 0
for num := range generatedNumbers {
if isneighbor(num1, num) {
cnt1++
}
if isneighbor(num2, num) {
cnt2++
}
}
if cnt1 <= cnt2 {
generatedNumbers[num1] = true
} else {
generatedNumbers[num2] = true
}
}

result := make([]int, 0, totalNumbers)
for num := range generatedNumbers {
result = append(result, num)
}
return result
}

func generateIdsEven(totalNumbers int, maxValue int) []int {
result := make([]int, 0, totalNumbers)
step := float64(maxValue) / float64(totalNumbers)
Expand All @@ -252,4 +298,3 @@ func generateIdsEven(totalNumbers int, maxValue int) []int {
}
return result[:totalNumbers]
}

Loading