Related
While I am defining a linear programming variables, I have to consider
index_i = 1:3 index_j = J = [1:2, 1:5, 1:3]
I want to define a variable x indexed with both i and j such that i is {1,2,3} and j is in {1,2} if i is 1, {1,2,3,4,5} if i is 2 and {1,2,3} if i is 3.
I tried several syntaxes but non of them delivered it successfully. Any suggestion?
I wonder why this is not working
#variable(m, e[i for i in I, j for j in J[i]])
I m expecting a result like this
e[1,1]
e[1,2]
e[1,3]
e[2,1]
e[2,2]
e[2,3]
e[2,4]
e[2,5]
e[3,1]
e[3,2]
e[3,3]
Assuming I=1:3 and J=[1:2, 1:5, 1:3]
you can do:
julia> #variable(m, e[i in I, j in J[i]])
JuMP.Containers.SparseAxisArray{VariableRef, 2, Tuple{Int64, Int64}} with 10 entries:
[1, 1] = e[1,1]
[1, 2] = e[1,2]
[2, 1] = e[2,1]
[2, 2] = e[2,2]
[2, 3] = e[2,3]
[2, 4] = e[2,4]
[2, 5] = e[2,5]
[3, 1] = e[3,1]
[3, 2] = e[3,2]
[3, 3] = e[3,3]
I'm a first year student who's learning Julia as a first programming language. I have a project about bellman ford's algorithm but it seems every code is a bit more advanced than I can currently understand. Is there a basic code like Dfs or Bfs for this that a starter could understand, if u have, do share.
This is implemented in LightGraphs
using LightGraphs
g = erdos_renyi(20, 100, seed=1)
bf_state = bellman_ford_shortest_paths(g, 1)
And now we can display all paths found in the graph:
julia> enumerate_paths(bf_state)
20-element Vector{Vector{Int64}}:
[]
[1, 4, 2]
[1, 3]
[1, 4]
[1, 5]
[1, 11, 6]
[1, 7]
[1, 3, 8]
[1, 3, 9]
[1, 7, 10]
[1, 11]
[1, 12]
[1, 3, 13]
[1, 3, 14]
[1, 15]
[1, 4, 16]
[1, 17]
[1, 3, 18]
[1, 19]
[1, 5, 20]
I have this piece of code:
for i=1:10
v=[2i,i]
#show v
end
and I get this result:
v = [2, 1]
v = [4, 2]
v = [6, 3]
v = [8, 4]
v = [10, 5]
v = [12, 6]
v = [14, 7]
v = [16, 8]
v = [18, 9]
v = [20, 10]
Now what I want to do is to collect all these outputs of into one array of arrays, something like:
[[2,1],[4,2],[6,3]]
and I don't really know how to do it, I've tried several solutions that didn't work.
You can use array comprehensions for this:
julia> x = [[2i,i] for i in 1:10]
10-element Array{Array{Int64,1},1}:
[2, 1]
[4, 2]
[6, 3]
[8, 4]
[10, 5]
[12, 6]
[14, 7]
[16, 8]
[18, 9]
[20, 10]
or go with the manual route of constructing an empty initial array, and pushing the inner arrays into it one-by-one:
julia> y = []
0-element Array{Any,1}
julia> for i in 1:10
push!(y,[2i,i])
end
julia> y
10-element Array{Any,1}:
[2, 1]
[4, 2]
[6, 3]
[8, 4]
[10, 5]
[12, 6]
[14, 7]
[16, 8]
[18, 9]
[20, 10]
Getting right to the gist of the problem:
In how many ways can we add k positive integers to reach a sum of exactly n if each number is smaller or equal to given number m?
The problem is solvable with dynamic programming but I am stuck because I cannot find the optimal substructure or recursion for the solution.
Here's a simple function in Python 3 that should fit your description. I assume that 0 is not an acceptable value but it's a trivial change if it is.
def howMany(k, n, m):
def sub(pos, currentSum, path):
if currentSum == n and pos == k: # reached the sum, print result and increase counter by 1
print(path)
return 1
elif currentSum < n and pos < k: # still worth trying
count = 0
for i in range(1, m):
count += sub(pos + 1, currentSum + i, path+[i])
return count
else: # abort
return 0
return sub(0, 0, [])
print(howMany(3, 10, 6))
yields
[1, 4, 5]
[1, 5, 4]
[2, 3, 5]
[2, 4, 4]
[2, 5, 3]
[3, 2, 5]
[3, 3, 4]
[3, 4, 3]
[3, 5, 2]
[4, 1, 5]
[4, 2, 4]
[4, 3, 3]
[4, 4, 2]
[4, 5, 1]
[5, 1, 4]
[5, 2, 3]
[5, 3, 2]
[5, 4, 1]
18
It could be optimised but that would obfuscate the logic at this stage.
I have a function which I can not get to run correctly and I cant figure out why. I think it may have to do with the base case.
This is what I have:
recursion(Start, [Tail|Tails], [Alpha|Alphas], Beta, [X|Xs]):-
third(Start, Alpha, S),
second(S,Beta,Tail,X),
recursion(Start, Tails, Alphas, Beta,Xs).
recursion([],[],[],_,[]).
I know that the second function works correctly but when I run this:
recursion([1, 2], [1, 2], [[1, 1], [2, 2]], 0, X).
I get:
T Call: (6) recursion([1, 2], [1, 2], [[1, 1], [2, 2]], 0, _G3980)
T Fail: (6) recursion([1, 2], [1, 2], [[1, 1], [2, 2]], 0, _G3980)
false.
My other predicates can be found:
second(A,B,_,1) :- A > B.
second(A,B,C,C) :- A == B.
second(A,B,_,-1) :- A < B.
third([A|As], [B|Bs], X) :-
third(As, Bs, Z),
X is Z + A* B.
third([],[],0).
UPDATE:
Start and Tail will both be the exact same list entered into the predicate. Thats why in the example they are both [1,2]. When I change my base case to recursion(_,[],[],_,[]). I get:
98 ?- recursion([1, 2], [1, 2], [[1, 1], [2, 2]], 0, X).
T Call: (6) recursion([1, 2], [1, 2], [[1, 1], [2, 2]], 0, _G1632)
T Redo: (6) recursion([1, 2], [1, 2], [[1, 1], [2, 2]], 0, _G1632)
T Fail: (6) recursion([1, 2], [1, 2], [[1, 1], [2, 2]], 0, _G1632)
false.
Any ideas?
Your base case is not getting accessed because Start is passed to recursion unchanged. Therefore, it will never unify with the empty list, meaning that only the first clause of recursion will be used.
It does not look like Start is being used in your function, so you might either ignore it, like that
recursion(_, [],[],_,[]).
or remove it altogether, like this:
recursion([Tail|Tails], [Alpha|Alphas], Beta, [X|Xs]):-
second(5,Beta,Tail,X),
recursion(Tails, Alphas, Beta,Xs).
recursion([],[],_,[]).