First, is anyone able to recreate the exact output I get? Second, Can anyone tell me why the value for u changes in this while loop? To the best of my understanding it should be constant yet it continues to decrease... Or if no one else can recreate this issue I'll look into hardware errors. I'm guessing it's a novice user error in Julia and I tried both global and non-global u declarations... Thanks!
Windows 7 Pro, HP Z820, 64-Bit, running Julia 0.4.5
global u=[2.681726086033021];
eo1 = u;
tem5 = [9999.9];
ktr = [1];
tsize=[1];
axnl=[0.1641272464920273];
aynl=[-.08693550068203583];
sineo1 = sin(eo1); #initialize
coseo1 = cos(eo1);
while (maximum(abs(tem5)) >= 1.0e-12 )== 1 && maximum(ktr) <= 10
tem5orig = abs(tem5) .>= 1.0e-12; #we care about updating records *originally* meeting conditions
sineo1[tem5orig] = sin(eo1[tem5orig]);
coseo1[tem5orig] = cos(eo1[tem5orig]);
tem5temp1 = ones(tsize) - coseo1 .* axnl - sineo1 .* aynl;
tem5[tem5orig] = tem5temp1[tem5orig];
tem5[tem5orig] = (u[tem5orig] - aynl[tem5orig] .*coseo1[tem5orig] + axnl[tem5orig] .*sineo1[tem5orig] - eo1[tem5orig]) ./tem5[tem5orig];
tem5temp2 = tem5;
if (maximum(abs(tem5temp2)) > 0.95) == 1
if maximum((abs(tem5temp2) .> 0.95) + (tem5temp2 .> 0) .> 1) == 1
tem5temp2[((abs(tem5temp2) .> 0.95) + (tem5temp2 .> 0)) .> 1] = 0.95;
end
if (maximum((abs(tem5temp2) .> 0.95) + (tem5temp2 .<= 0)) .> 1) == 1
tem5temp2[((abs(tem5temp2) .> 0.95) + (tem5temp2 .<= 0)) .> 1] = -0.95;
end
end
tem5[tem5orig] = tem5temp2[tem5orig];
eo1[tem5orig] = eo1[tem5orig] + tem5[tem5orig];
ktrtemp = ktr + 1;
ktr[tem5orig] = ktrtemp[tem5orig];
println(ktr,",",tem5,",",eo1,",",sineo1,",",coseo1,",",u,",",tem5orig)
end
Output:
[2],[-0.0042672957728621345],[2.6774587902601588],[0.4438285405397952],-0.8961117266291717],[2.6774587902601588],Bool[true]
[3],[-0.003598989243971466],[2.6738598010161874],[0.4476484617059426],[-0.8942096257211187],[2.6738598010161874],Bool[true]
[4],[-0.003035319765326468],[2.670824481250861],[0.4508638064529911],[-0.8925927559815393],[2.670824481250861],Bool[true]
[5],[-0.0025599151808959153],[2.6682645660699653],[0.4535710297874299],[-0.8912201304602417],[2.6682645660699653],Bool[true]
[6],[-0.0021589606544931365],[2.6661056054154724],[0.4558509890748779],[-0.8900561082086093],[2.6661056054154724],Bool[true]
[7],[-0.0018208009676989512],[2.6642848044477736],[0.4577715213143546],[-0.8890698702990342],[2.6642848044477736],Bool[true]
[8],[-0.0015356039646266324],[2.662749200483147],[0.45938958087161175],[-0.8882348861571498],[2.662749200483147],Bool[true]
[9],[-0.001295076024952105],[2.661454124458195],[0.46075301570977506],[-0.8875283987086542],[2.661454124458195],Bool[true]
[10],[-0.0010922216912360376],[2.660361902766959],[0.4619020457466296],[-0.8869309444004525],[2.660361902766959],Bool[true]
[11],[-0.0009211406996768078],[2.659440762067282],[0.4628704952575407],[-0.8864259160358744],[2.659440762067282],Bool[true]
eo1 and u refer to the same array object. When you modify the contents of one, the other one also changes – because they are the same object. If you initialize eo1 = copy(u) instead, u won't change when you modify eo1.
Related
Here is my code in Julia and I would like to improve its speed since it is slow for large dataset. I provided the code with a small example so it can be executed and produce the results. I think that bottleneck is using find function in the loop which causes the code to be very slow but I don't know how I can replace it with sth faster.
A = [[1,2,3,4,5], [2,3,4,5,6,7,8], [4,7,8,9], [9,10], [2,3,4,5]]
mx = maximum(maximum(ar))
idx_new = zeros(Int, mx)
flag = ones(Int, mx);
Hscore = rand(1, length(A))
thresh = 0.2 * sum(Hscore)
acc_q = 0
pos = sortperm(vec(Hscore))
iter = 1
while acc_q < thresh
acc_q = acc_q + Hscore[pos[iter]]
nd = A[pos[iter]]
fd_flag = flag[nd]
cc = in.(fd_flag, 2)
node = nd[findall(x->x==0, cc)]
dd = nd[findall(x->x!=0, cc)]
TF = isempty(dd)
if TF == true
q_val = Hscore[pos[iter]]
acc_q = acc_q + q_val
idx_new[vec(node)] .= (val + 1)
flag[node] .= 2
val = val + 1;
iter = iter + 1
end # end of if TF
end ## end of while loop
While "please improve my code" is not a right question style for StackOverflow, generally when searching many times for element among many many options these are the first two that you might consider:
Sort the list of elements (with sort!) and use searchsorted to find the desired element
Use Set(mylist) to create a hash set and than search within the set.
I'm new to both the Julia Language and its optimization package JuMP. I'm trying to solve a really simple optimization problem, the objective is the minimization of the fixed costs for a facility location problem
o = Model(with_optimizer(GLPK.Optimizer))
#variable(o, x[i = 1:size(candidates, 1)], Bin) #creating variables for each possible location
#variable(o, ϑ[i = 1:size(demand, 2)] >= 0) #creating one variable for each demand scenario (in this case only one scenario is considered)
#objective(o, Min, dot(x,cost_facility) * (1 + M) + sum(capacities .* x .* TH .* lab_cost_plant) + 1/size(demand,2) * sum(ϑ))
#constraint(o, 30000x[1] + 20000x[2] + 30000x[3] + 20000x[4] >= 1500.0)
#constraint(o, ϑ[1] + 1.6317502375004835e10x[1] + 1.0878334916669891e10x[2] + 1.6318862646433956e10x[3] >= 2.9076517866671824e9)
JuMP.optimize!(o)
st = MOI.get(o, MOI.TerminationStatus())
#info "Status $st"
And I get the following result:
┌ Info: Status INFEASIBLE
└ # Main In[37]:3
I couldn't see why such a problem could be infesible, considering that those two constraints were the only ones present. So, I tried to modify them to understand what was wrong and it turned out that by substituting the second constraint with an equality constraint (and keeping everything else unchanged):
#constraint(o, ϑ[1] + 1.6317502375004835e10x[1] + 1.0878334916669891e10x[2] + 1.6318862646433956e10x[3] == 2.9076517866671824e9)
An optimal solution is found:
┌ Info: Status OPTIMAL
└ # Main In[43]:3
I couldn't find any explanation for that, shouldn't the first problem be feasible too? Is there any mistake in the code? Thank you in advance for your help
candidate_plant = ["Roma", "London", "Berlin", "Milano"]
candidate_whs = ["Munich", "Glasgow","Madrid", "Lione"]
capacity_plant = [30000, 20000, 30000, 20000]
capacity_whs = [400000, 300000, 400000, 300000]
cost_plant = [174739, 293932, 174739, 293932]
cost_whs = [124739, 213932, 124739, 213932]
demand = [10000; 10000; 10000]
TH = 20
M = 0.3
lab_cost_plant = 8.64
candidates = vcat(candidate_plant, candidate_whs)
capacities = vcat(capacity_plant, capacity_whs)
cost_facility = vcat(cost_plant, cost_whs)
This is the input data used
I am building a metaheuristic in Julia for study purpose.
The purpose is to find the best order of boxes.
1) I start with an initial order (random order) defined as. Order = InitOrder before my while loop.
2) For each iteration in the while loop I set CurrentOrder = Order
3) When the CurrentOrder is changed, Order changes too. Why does Order change value without being assigned? And how do I avoid it?
Version:
JuliaPro 1.0.2.1
Editor: Atom
while ( (time_ns()-timestart)/1.0e9 < RunLength && done == false ) #Stopping Criteria
done = true #Starting point
IterationCount = IterationCount + 1
BestCurrentValue = sum(H) #Worst case solutio
CurrentOrder = Order #(From,To)
for n1=1:N
for n2=1:N
if n1 != n2
(CurrentOrder,CopyTo) = SwapBox(CurrentOrder,n1,n2) #Swap boxes
(CurrentLayout,L) = DeltaCopy(CurrentLayout,CopyTo,CurrentOrder) #Delta Copy to minimise calculations
(TempLayout,L) = BLV(BinW,CurrentLayout,CopyTo,CurrentOrder,W,H,L) #Evalueate by BLV
if L < BestCurrentValue #check if TempLayout is better than Best Current
BestCurrentValue = L
BestCurrentOrder = CurrentOrder
BestCurrentLayout = CurrentLayout
end #if L<...
end #if n1 != n2
##############################################################################
CurrentOrder = Order
##############################################################################
end #n2 in N
end #n1 in N
if BestCurrentValue < BestValue
done = false #Look further
BestValue = BestCurrentValue
BestOrder = BestCurrentOrder
BestLayout = BestCurrentLayout
Order = BestOrder
end #if BestCurrentValue...
end #while
Your assignment NewOrder=Order does not copy any information in memory, it just says that the variable NewOrder should point to the same memory location as Order. Changing one of these variables will thus also change the other. If you want to copy a variable you could use NewOrder=deepcopy(Order)
I have written the following code to calculate temperature distribution along a fin. For some reason, it keeps calculating certain values as zero, even when they aren't!
for t = 0:300:3000
for i = 2:L2
if i ==2
A(i)= 0.75*rhocp*DX/DT + 3*k/dx;
B(i)= k/dx;
C(i)= 2*k/dx;
D(i)= 0.75*rhocp*(DX/DT)*T(i);
elseif i == L2
A(i)= 0.75*rhocp*DX/DT + 3*k/dx;
B(i)= 2*k/dx;
C(i)= k/dx;
D(i)= 0.75*rhocp*(DX/DT)*T(i);
else
A(i)= 0.75*rhocp*DX/DT + 2*k/dx;
B(i)= k/dx;
C(i)= k/dx;
D(i)= rhocp*(DX/DT)*T(i);
end
P(1) = 0;
Q(1) = T(1);
for i = 2:L2
DENO = A(i) - C(i)*P(i-1);
NUM = D(i)+ C(i)*Q(i-1);
P(i) = B(i)/DENO;
Q(i) = NUM/DENO;
end
for i =L2:-1:2
if i == L2
T(i) = Q(i);
else
T(i) = P(i)*T(i+1) + Q(i);
end
end
T(L1) = ((2*k*T(L2) - h*DX*Tinf)/(2*k - h*DX));
end
disp (T);
`
DENO and NUM is calculated as zero in the first iteration, even though on calculating their values is not zero! This leads to "Division by zero" error.
A(2)-C(2)*P(1)
ans =
3750.
Analytically it has got a value though.
Please give the parameter values so one can run your script...
DT was missing , i set it to 1 .
Finally I found the problem: you forgot an end to close the loop
for i = 2:L2
the end has to be put just before P(1)=0
Moreover this loop does not depend on the t value so it could be put before the loop for t = 0:300:3000
Is there a way to make certain functions such as isinteger() work with JuMPArrays?
I am using Julia/JuMP to solve an optimization problem, and after I get the solution, I want to check if the solution is integer. So here is what I wrote:
#defVar(m, 0<= x[1:3] <= 1)
...
xstar = getValue(x)
if isinteger(xstar)
...
end
And I get an error saying isinteger() has no method matching isinteger(::JuMPArray).
Thanks
So in general you can get an underlying array from a JuMPArray by using [:], e.g.
m = Model()
#variable(m, 0 <= x[1:3] <= 1)
#variable(m, 0 <= y[1:10, 1:10] <= 1)
solve(m)
xstar = getvalue(x)[:]
ystar = getvalue(y)[:,:]
Note that the reason for this is that JuMPArrays don't have to start with index 1, so the user needs to explicitly say they want a normal Julia array before doing things.
Regardless, you shouldn't use isinteger. Solvers don't always return very precise answers, e.g. they might say x[1] = 0.999996 but they really mean it is 1. You should do something like
for i in 1:3
if getvalue(x[i]) >= 0.999
println("x[$i] is 1!")
elseif getvalue(x[i]) <= 0.001
println("x[$i] is 0!")
end
end
to make sure you don't get any false negatives. If the variable is restricted to be integer or binary, use iround, e.g.
for i in 1:3
v = iround(getvalue(x[i]))
if v == 1
println("x[$i] is 1!")
elseif v == 0
println("x[$i] is 0!")
end
end
but it looks like in this case you are just seeing if the solution is naturally 0 or 1.