UndefVarError in Julia - julia

Very new to Julia and trying to work through some code. I keep getting this error: "ERROR: LoadError: LoadError: UndefVarError: #defVar not defined". The start of the code is below where I define the #defVar. Julia Version 1.1.1 (2019-05-16). Here is the code I am using:
using DataFrames
using GLPKMathProgInterface
using JuMP
num_lineups = 6
num_overlap = 4
path_data = "/users/matt/desktop/example_players.csv"
path_data2 = "/users/matt/desktop/example_players2.csv"
path_to_output= "/users/matt/desktop/output.csv"
m = Model(solver=GLPKSolverMIP())
#defVar(m, players_a_lineup[i=1:num_players_a], Bin)
#defVar(m, players_b_lineup[i=1:num_players_b], Bin)

You are using an old syntax (#defvar has been used up to version 0.12). For the latest Julia/JuMP version 19, your code could should read
using DataFrames
using GLPK
using JuMP
...
m = Model(with_optimizer(GLPK.Optimizer))
#variable(m, players_a_lineup[i=1:num_players_a], Bin)
#variable(m, players_b_lineup[i=1:num_players_b], Bin)

Related

How to solve the GLMakie Precompiling error in Julia?

I was plotting my heatmap using Makie. It was working fine but suddenly I get this error trying to run the GLMakie package. CairoMakie worked fine.
using GLMakie
[ Info: [e9467ef8-e4e7-5192-8a1a-b1aee30e663a] ERROR: LoadError: KeyError: key :colormap not found
Stacktrace:
[1] getindex(h::Dict{Symbol, Observables.Observable}, key::Symbol)
# Base ./dict.jl:481
[2] getindex
# ~/.julia/packages/MakieCore/8YGMv/src/attributes.jl:96 [inlined]
[3] getindex(x::MakieCore.Mesh{Tuple{GeometryBasics.Mesh{2, Float32, GeometryBasics.Ngon{2, Float32, 3, GeometryBasics.Point{2, Float32}}, GeometryBasics.SimpleFaceView{2, Float32, 3, GeometryBasics.OffsetInteger{-1, UInt32}, GeometryBasics.Point{2, Float32}, GeometryBasics.NgonFace{3, GeometryBasics.OffsetInteger{-1, UInt32}}}}}}, key::Symbol)
# MakieCore ~/.julia/packages/MakieCore/8YGMv/src/attributes.jl:192
*<skipping abundant error messages>*
ERROR: Failed to precompile GLMakie [e9467ef8-e4e7-5192-8a1a-b1aee30e663a] to /home/mikheev/.julia/compiled/v1.7/GLMakie/jl_OWtvbO.
while my heatmap code gave me this error:
let
ix = sortperm([a[1] for a in argmax(dat_e[28,:,:], dims=1)][1,:])
ix = ix[2000:6000]
f, ax, hm = CairoMakie.heatmap((dat_e[28,:,ix]), colormap=Reverse("RdBu"), colorrange = (-40, 40))
ax = current_axis()
ax.xlabel = "Time [sec]"
ax.ylabel = "Sorted trials"
CairoMakie.Colorbar(f[:, end+1], hm, label = "Voltage [µV]")
hidespines!(ax, :t, :r)
f
end
MethodError: no method matching to_color(::MakieCore.Automatic)
Closest candidates are:
to_color(!Matched::Number) at ~/.julia/packages/Makie/bwZTV/src/conversions.jl:763
to_color(!Matched::Colorant) at ~/.julia/packages/Makie/bwZTV/src/conversions.jl:764
to_color(!Matched::Makie.AbstractPattern) at ~/.julia/packages/Makie/bwZTV/src/conversions.jl:769
Any ideas how to solve it? Both problems occured simultaneously after I restarted my VSCode.
Pkg.update() solved the problem.
The problem was in the outdated version of the GLMakie.
If you get any error loading GLMakie, it likely means, you don't have an OpenGL capable Graphic Card, or you don't have an OpenGL 3.3 capable video driver installed. Note, that most GPUs, even 8 year old integrated ones, support OpenGL 3.3.
https://github.com/MakieOrg/Makie.jl/tree/master/GLMakie#troubleshooting-opengl

TypeError: in typeassert in Julia 1.6.3

I am new to Julia. I am working on a Julia package which the writer is not available.
This is the part of the code where I have a problem with:
function find_partition(model::Model, ms)
ps = Dict{Z3Expr,Vector{Int}}()
for (i, m) in enumerate(ms)
mval = Z3.eval(model, m, false)
println(ps)
println(mval)
println(typeof(mval))
if haskey(ps, mval)
push!(ps[mval], i)
else
push!(ps, mval=>Int[i])
end
end
values(ps)
end
Here is the output:
Dict{Z3.Expr, Vector{Int64}}()
1.0
Z3.ExprAllocated
ERROR: TypeError: in typeassert, expected UInt64, got a value of type UInt32
Stacktrace:
[1] hashindex(key::Z3.ExprAllocated, sz::Int64)
# Base ./dict.jl:169
[2] ht_keyindex(h::Dict{Z3.Expr, Vector{Int64}}, key::Z3.ExprAllocated)
# Base ./dict.jl:284
[3] haskey(h::Dict{Z3.Expr, Vector{Int64}}, key::Z3.ExprAllocated)
# Base ./dict.jl:550
[4] find_partition(model::Z3.ModelAllocated, ms::Vector{Z3.ExprAllocated})
# Absynth.NLSat ~/Desktop/faoc/Absynth/src/nlsat/cfinitesolver.jl:101
I tried following it and found out that somewhere inside dict.jl in Julia, there are such lines
sz = length(h.keys)
...
index = hashindex(key, sz)
And this is the hashindex written somewhere in dict.jl:
hashindex(key, sz) = (((hash(key)::UInt % Int) & (sz-1)) + 1)::Int
It seems that the version of Z3 used in this package is not compatible with the current version of Julia. I am not sure if this package was working in the beginning at all.
Is there any quick fix to this? Like rewriting this part of the code:
if haskey(ps, mval)
push!(ps[mval], i)
else
push!(ps, mval=>Int[i])
in a way that this won't happen? I tried writing like merge!(ps,Dict(mval=>Int[i])) but it's eventually reaching this hashindex function again.
If I install the older versions of Julia, would it be possible to solve this issue or the problem is somewhere else?

In Julia Flux, I keep getting the error LoadError: Mutating arrays is not supported, but I don't see where I am mutating an array

I am new to Julia and for some reason I can't get this very simple code to work. No matter what I try, I get the error LoadError: Mutating arrays is not supported. I understand that this error occurs when I mutate an array during the course of optimization so that the code is no longer differentiable. I clearly do not understand Julia enough to see where I am doing this.
If it helps the error seems to be occurring in the line for d in dataset.
using Statistics
using Flux: onehotbatch, onehot, onecold, crossentropy, throttle
using Flux
using Base.Iterators:repeated
using Plots:heatmap
using ImageView:imshow
images = Flux.Data.MNIST.images()[1:10]
labels = Flux.Data.MNIST.labels()[1:10]
heatmap(images[4], color=:grays, aspect_ratio=1)
X = float.(reshape.(images, :))
encode(x) = onehot(x, 0:9)
Y = encode.(labels)
m = Chain(Dense(28^2, 32, relu), Dense(32, 10), softmax)
loss(x, y) = crossentropy(m(x), y)
opt = ADAM()
accuracy(x, y) = mean(onecold(m(x)) .== onecold(y))
dataset = zip(X, Y)
print(size(X))
evalcb = () -> #show(loss(X, Y))
print("Training...")
# Flux.train!(loss, params(m), dataset, opt, cb=throttle(evalcb, 5));
for d in dataset
print(d[2])
gs = gradient(params(m)) do
l = loss(d...)
end
update!(opt, params(m), gs)
end
It looks like I did have an old version of Flux (but not that old). I had to uninstall and reinstall Julia to install the new version of Flux.

How to use #constraintref correctly? In Julia Language

guys,I wrote the code and got the following error: #constraint is not defined. What did I wrong. How to fix it? Thanks
#constraintref restrição[1:2]
for j=1:2
#constraint(m, restrição[j], sum(A[j,i]*x[i] for i=1:3) <= b[j])`
end
```
You are using an old syntax that was valid in JuMP 0.18 (you can see the link for more details)
As of today you can just use an assignment operator instead of #constraintref macro and your code could look like this:
using GLPK
m = Model(with_optimizer(GLPK.Optimizer))
#variable(m, x[1:5] >= 0)
myCons = Vector{ConstraintRef}(undef, 5)
for i = 1:5
myCons[i] = #constraint(m, x[i] >= i)
end

How to run Julia parallel code using distributed array computations (pre 1.x Julia versions)

I am running on Windows the example from the documentation:
julia> function compute_something(A::DArray)
B = darray(eltype(A), size(A), 3)
for i = 1:size(A,3)
#spawnat owner(B,i) B[:,:,i] = sqrt(A[:,:,i])
end
B
end
julia> A = drand(10, 15, 4)
julia> compute_something(A)
But I get an error:
in compute_something: B not defined
Why I'm getting this error?
In Web REPL this example works.

Resources