-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.jl
65 lines (56 loc) · 2.24 KB
/
utils.jl
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
module Utils
function get_algo_params()
file_info = open("/harddrive/home/pablo/Google Drive/UNED/Computacion_Evolutiva/Actividades/Actividad_1/genetic_algo_scratch/algo_inputs.txt") do file
read(file, String)
end
file_info = split(file_info, "\n")
num_generations, population_size, number_genes, variable_range = nothing, nothing, nothing, nothing
variable_range_low, variable_range_high = nothing, nothing
function_param = nothing
# Could be more efficient
for param in file_info
param = split(param, "=")
if param[1] == "num_generations"
num_generations = parse(Int, param[2])
elseif param[1] == "population_size"
population_size = parse(Int, param[2])
elseif param[1] == "number_genes"
number_genes = parse(Int, param[2])
elseif param[1] == "variable_range_high"
variable_range_high = parse(Int, param[2])
elseif param[1] == "variable_range_low"
variable_range_low = parse(Int, param[2])
elseif param[1] == "function_param"
function_param = param[2]
end
end
variable_range = (variable_range_low, variable_range_high)
if num_generations == nothing || population_size == nothing ||
number_genes == nothing || variable_range == nothing
throw(ArgumentError("Missing parameters in algot_inputs.txt."))
end
return num_generations, population_size, number_genes, variable_range, function_param
end
function generate_graycode(decimal, binary_string)
gray_code_number = xor(decimal, decimal >> 1)
gray_code = string(gray_code_number, base = 2, pad = length(binary_string))
return gray_code
end
end
"""
function gray_code_recursive(binary_string, n)
length_str = length(binary_string)
if n <= 0
return nothing
else
for i in length_str:-1:1
char = '1' * binary_string[i]
binary_string *= char
end
for i in length_str:-1:1
binary_string[i] = '0' * binary_string[i]
end
gray_code_recursive(binary_string, n-1)
end
end
"""