-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.v
147 lines (130 loc) · 3.74 KB
/
main.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import rand
import math
import os
import time
fn main() {
rand.seed(time.now().uni)
// #### read input args ####
input_config_file_path := os.args[1]
output_pajek_file_path := os.args[2]
// #### create parameter variables ####
mut end_time := 0
mut end_x := 0
mut end_y := 0
mut number_of_entities := 0
mut neighbours_distance := 0
// #### read config file ####
input_config_file_content := os.read_file(input_config_file_path.trim_space()) or {
println('failed to open $input_config_file_path')
return
}
input_config_file_lines := input_config_file_content.split(';')
config_number_lines := input_config_file_lines.len - 1
for i := 0; i < config_number_lines; i++ {
mut input_config_file_line := input_config_file_lines[i]
mut config_values_array := input_config_file_line.split('=')
mut name := config_values_array[0].trim_space()
mut value := config_values_array[1].trim_space()
println('$name = $value')
if name == 'end_time' {
end_time = value.int()
}
if name == 'end_x' {
end_x = value.int()
}
if name == 'end_y' {
end_y = value.int()
}
if name == 'number_of_entities' {
number_of_entities = value.int()
}
if name == 'neighbours_distance' {
neighbours_distance = value.int()
}
}
// #### create sequences####
mut time_arr := [0; end_time]
for i := 0; i < end_time; i++ {
time_arr[i] = i
}
mut x_arr := [0; end_x]
for i := 0; i < end_x; i++ {
x_arr[i] = i
}
mut y_arr := [0; end_y]
for i := 0; i < end_y; i++ {
y_arr[i] = i
}
// #### calculate frequencies ####
mut time_freq := [0; end_time]
for i := 0; i < end_time; i++ {
time_freq[i] = temporal_distribution(i)
}
mut x_freq := [0; end_x]
for i := 0; i < end_x; i++ {
x_freq[i] = x_distribution(i)
}
mut y_freq := [0; end_y]
for i := 0; i < end_y; i++ {
y_freq[i] = y_distribution(i)
}
// #### create entities ####
mut entities := []Entity
for entity_counter := 0; entity_counter < number_of_entities; entity_counter++ {
// draw temporal coordinate
entity_time := random_integer_distribution(time_arr, time_freq, end_time)
// draw social coordinates
entity_x := random_integer_distribution(x_arr, x_freq, end_x)
entity_y := random_integer_distribution(y_arr, y_freq, end_y)
// construct entities
entities << Entity{
id: entity_counter,
time: entity_time,
x: entity_x,
y: entity_y
}
}
// endless social world
//entities_cube := cubify_entities(entities, end_time, end_x, end_y)
// social world with boundaries
entities_cube := entities
// #### determine relations ####
mut relations := []Relation
for entity_a in entities {
for entity_b in entities_cube {
if (entity_a.id != entity_b.id) &&
(entity_b.time <= entity_a.time + neighbours_distance) &&
(entity_b.time >= entity_a.time - neighbours_distance) &&
(entity_b.x <= entity_a.x + neighbours_distance) &&
(entity_b.x >= entity_a.x - neighbours_distance) &&
(entity_b.y <= entity_a.y + neighbours_distance) &&
(entity_b.y >= entity_a.y - neighbours_distance) {
distance := int(math.sqrt(
math.pow(f64(entity_a.time - entity_b.time), 2.0) +
math.pow(f64(entity_a.x - entity_b.x), 2.0) +
math.pow(f64(entity_a.y - entity_b.y), 2.0)
))
relations << Relation{
id_a: entity_a.id,
id_b: entity_b.id,
distance: distance
}
}
}
}
// #### save result ####
output_pajek_file := os.create(output_pajek_file_path) or {
println(error)
return
}
output_pajek_file.write('*Network popgenerator_network \n')
output_pajek_file.write('*Vertices $number_of_entities \n')
for entity in entities {
output_pajek_file.write(entity.print())
}
output_pajek_file.write('*Edges \n')
for relation in relations {
output_pajek_file.write(relation.print())
}
output_pajek_file.close()
}