switching produce() with put!() - julia

i am trying to modify a code that was written in julia 0.4 and one of these modifications is replacing produce() with put!()but i don't really know how am i suppose to replace it knowing that put!()requires Channel
i have tried c=Channel(i); put!(c,v) but i don't know if it is the right way to replace it in this function below
function scan()
i = 1
init = 1
while i > 0
if i >= init
#objective(m, Max, x[i])
res = JuMP.solve(m, suppress_warnings=true)
if res==:Optimal || res==:Unbounded
ub[i] = round(Int, getvalue(x[i]))
setobjectivesense(m, :Min)
res = JuMP.solve(m, suppress_warnings=true)
#assert res==:Optimal || res==:Unbounded
lb[i] = round(Int, getvalue(x[i]))
v[i] = lb[i]
init += 1
else
#assert res==:Infeasible
i -= 1
continue
end
elseif v[i] < ub[i]
v[i] += 1
else
setupperbound(x[i], Inf)
setlowerbound(x[i], -Inf)
init -= 1
i -= 1
continue
end
if i >= level
produce(v) # the line to be replaced
continue
else
setupperbound(x[i], v[i])
setlowerbound(x[i], v[i])
i += 1
end
end
end

Related

Multiple conditions in a for loop?

I am trying to figure out what this pseudocode would be in Julia code:
Pseudocode:
for (i = 0; (i < 32) && (array[i] ≠ nil); i += 1) do
result := merge(array[i], result)
array[i] := nil
The multiple conditions seem to trip me up. I don't know how to format it in Julia. If anyone knows how I would appreciate it. I am new to the language.
There are multiple ways to do it in Julia. For example, you can use break statement
for i in 1:32 # in Julia we usually start numbering from 1
array[i] == nothing && break
result = merge(array[i], result)
array[i] = nothing
end
or while loop
i = 1
while i <= 32 && array[i] != nothing
result = merge(array[i], result)
array[i] = nothing
i += 1
end
You could do the looping and put the conditional inside:
nil = nothing #or any other sentinel value
for i in 1:32 #for i in eachindex(array)
if array[i] != nil #if cond1 & cond2 & cond3... &condn
result = merge(result, array[i])
array[i] = nil
end
end

Translate matrix algebra code from VB to R

For a project, I am trying to replicate/rewrite the following Visual Basic code from a case study so I can run it in R.
For j=1 to N
For I=1 to M
While I>j
If b(I,j) c=P then
a(I,j)=l else
if b(1, j)<= [P+ (l-P ) /21 then
a(I,j)=3 else
a(1, j)=O
end if
end if
wend
while Icj
if a(I,j)=l then
a(j,i)=l else
if a(I,j)=3 then
a(j,I)=O else
a( j ,I) =3
end if
end if
wend
next
next
The goal is to compare the values b(j,I) from matrix B(M,N) and fill matrix A(M,N) with either 0,1 or 3. It should represent the results of a football game.
This is what I got so far:
M <- 3
N <- 3
A <- matrix(0,M,N)
B <- matrix(sample(0:4,25, replace=TRUE),nrow=M, ncol=N)
for (i in 1:nrow(B)) {
for (j in 1:ncol(B)) {
while (i > j) {
if (B[i,j] > B[j,i]) {
A[i,j] = 3 & A[j,i] = 0
}
if (B[i,j] < B[j,i]) {
A[i,j] = 0 & A[j,i] = 3
}
else {
A[i,j] = 1 & A[j,i] = 1
}
}
}
}
I would really appreciate any help or tips!

Converting recursive formulate into pseudocode using memoization

i've been trying to turn the recurrence formula underneath into a pseudocode that uses memoization, however currently all i know is my below attempt is incorrect, is anyone able to point me in the right direction?
My recurrence formula:
N(C,i) =
1 if C = 0
0 if i=0 or C<0}
N(C-p_i, i-1) + N(C, i-1) otherwise
My current attempt:
MEM-N(C, i, r)
if r[i] >= 0 then
return r[i]
if i = 0 and r[i] >= 0 or C < 0 and r[i] >= 0 then
return 0
else if C = 0 and r[i] >= 0 then
return 1
else
q = -$\infty$
q = MEM-N(C - $p_i$, i-1) + MEM-N(C,i - x, r)
r[i] = q
return q
Following on from the comments:
MEM-N(C, i, r)
if C = 0 then
return 1
else if i = 0 or C < 0 then
return 0
else if r[i] >= 0 then
return r[i] # move here
else
q = MEM-N(C - p_i, i - 1, r) + MEM-N(C, i - 1, r) # fix
r[i] = q
return q

error bernstein vandermonde julia.

the following error occurs. I tried to change the n .... but not working
"LoadError: BoundsError: attempt to access 9-element Array{Float64,1}:"
function bernstein_vandermonde( n )
if n == 1
v = ones(1, 1);
return v
end
v = zeros( n, n );
x = linspace( 0, 1, n );
for i = 1:n
println("entra no loop")
v[i,1:n] = bernstein_poly_01(n - 1, x[i])
end
return v
end
function bernstein_poly_01( n, x )
bern = ones(n)
if n == 0
bern[1] = 1
elseif 0 < n
bern[1] = 1 -x
bern[2] = x
for i = 2:n
bern[i+1] = x*bern[i];
for j = i-1:-1: 1
bern[j+1] = x*bern[j] + (1 - x)*bern[j+1]
end
bern[1] = (1 - x)*bern[1]
end
end
return bern
end
I can not solve :(

MATLAB to R Conversion: Append values to an existing empty array through for loop

I have the below code with me. This code was written originally in MATLAB. I have two questions here:
1) What would be the corresponding command in R for the below command in MATLAB:
duet(i).p = [];
2) In the below code I am getting all the correct 6 values for duet$n, but I am not getting correct values for duet$p. My question is how to append the values to an empty existing array duet$p[i] in R through the for loop iterations.
This line is not working in the below code:
duet$p[i] <- c(duet$p[i],j)
I might also have declared duet$p[i] <- array() incorrectly.
The values for duet.n and duet.p from MATLAB are:
duet.n
2 0 2 0 1 3
duet.p
[] [3,6] [] [1,3,5,6] [1,6] []
In R, I am getting duet$n values correctly, but I am not able to get the array kind of results for duet$p.
Any help to get the duet$p values would be appreciated.
x <- matrix(c(-1,2,4,1,7,4.2,3,0,1.2,-1.2,5.1,4,2,3.1,1.1,1,1,9,0,1,2,2,8,1,2,2,2,2,2,2),nrow=6,ncol=5,byrow=T)
fro=1;N=6;M=2;V=3;
F <- list(f=c())
duet = list()
for (i = 1 : N){
duet$n[i] = 0
duet$p[i] = array() ## Create an empty array
for (j in 1 : N){
dl = 0
de = 0
dm = 0
for (k = 1 : M){
if (x[i,V + k] < x[j,V + k]){
dl = dl + 1
} else if (x[i,V + k] == x[j,V + k]){
de = de + 1
} else{
dm = dm + 1
}
}
if (dl == 0 & de != M){
duet$n[i] = duet$n[i] + 1
} else if (dm == 0 & de != M){
duet$p[i] = c(duet$p[i],j)
}
}
if (duet$n[i] == 0){
x[i,6] = 1
F$f = c(F$f,i)
}
}
This appears to get the output you want:
x <- matrix(c(-1,2,4,1,7,4.2,3,0,1.2,-1.2,5.1,4,2,3.1,1.1,1,1,9,0,1,2,2,8,1,2,2,2,2,2,2),nrow=6,ncol=5,byrow=T)
fro=1;N=6;M=2;V=3;
F <- list(f=c())
duet = list(n=rep(0,N), p=lapply(1:N, function(x)c()))
for (i in 1 : N){
duet$n[i] = 0
#duet$p[[i]] = c() ## Create an empty array
#if(i==2) browser()
for (j in 1 : N){
k=1:M
dl <- sum(x[i,V + k] < x[j,V + k])
de <- sum(x[i,V + k] == x[j,V + k])
dm <- sum(x[i,V + k] > x[j,V + k])
if (dl == 0 & de != M){
duet$n[i] = duet$n[i] + 1
} else if (dm == 0 & de != M){
duet$p[[i]] = c(duet$p[[i]],j)
}
}
if (duet$n[i] == 0){
#x[i,6] = 1
F$f = c(F$f,i)
}
}
What have I done?
commented out the line x[i,6] =1, because there isn't an x[i,6], and I'm not sure what you meant it to be. You will need to sort this out.
Initialised duet$n as a vector
Initialised duet$p as a list of n empty vectors
removed the k loop as conditional counting in R can be done as the sum of elements where the condition is TRUE.
corrected the syntax of for loops: = became in
I think you're trying to do duet[i]$p instead of what you're doing. Also you need to initialize each cell as a list

Resources