text:
# # [Multi-City COVID-19 Model](@id covid_example)
#
#md # [![](https://img.shields.io/badge/show-nbviewer-579ACA.svg)](@__NBVIEWER_ROOT_URL__/examples/covid/covid.ipynb)

using AlgebraicPetri
using AlgebraicPetri.Epidemiology

using OrdinaryDiffEq
using Plots

using Catlab
using Catlab.Theories
using Catlab.Programs
using Catlab.CategoricalAlgebra.FreeDiagrams
using Catlab.WiringDiagrams
using Catlab.Graphics

import Catlab.Theories: id

display_wd(ex) = to_graphviz(ex, orientation=LeftToRight, labels=true);
id(args...) = foldl((x,y)->id(x) ⊗ id(y), args);

# #### Step 1: Define a new primitive to extend the presentation of InfectiousDiseases

travel_petri = PetriCospan(
        [1,2,3],
        PetriNet(6, (1,4), (2,5), (3,6)),
        [4,5,6]);

# #### Step 2: Extend the Infectious Disease presentation,
# get the new generators, and update the functor

@present EpiWithTravel <: InfectiousDiseases begin
    travel::Hom(S⊗E⊗I,S⊗E⊗I)
end;

S,E,I,R,D,transmission,exposure,illness,recovery,death,travel = generators(EpiWithTravel);
new_functor = copy(FunctorGenerators)
new_functor[travel] = travel_petri

F(ex) = functor((PetriCospanOb, PetriCospan), ex, generators=new_functor);

# ### COVID-19 TRAVEL MODEL:

# SEIRD City Model with travel as S ⊗ E ⊗ I → S ⊗ E ⊗ I
#
# $seird_{city} = (((Δ(S) ⊗ id(E)) ⋅ (id(S) ⊗ σ(S,E))) ⊗ id(I)) ⋅ (id(S, E) ⊗ exposure) ⋅
#                 (id(S) ⊗ (∇(E) ⋅ Δ(E)) ⊗ id(I)) ⋅ (id(S, E) ⊗ ((illness ⊗ id(I)) ⋅
#                 (∇(I) ⋅ Δ(I)) ⋅ (id(I) ⊗ (Δ(I) ⋅(recovery ⊗ death))))) ⋅ (travel ⊗ ◊(R) ⊗ ◊(D))$
#
# This is a very complicated interaction, so we can use the program interface for easier model definition

seird_city = @program EpiWithTravel (s::S, e::E, i::I) begin
    e2 = exposure(s, i)
    i2 = illness(e2)
    d = death(i2)
    r = recovery(i2)
    return travel(s, [e, e2], i2)
end
seird_city = to_hom_expr(FreeBiproductCategory, seird_city)

display_wd(seird_city)
# -
Graph(decoration(F(seird_city)))

# create a multi-city SEIRD models

ncities(city,n::Int) = compose([city for i in 1:n]...)

seird_3 = ncities(seird_city, 3)
pc_seird_3 = F(seird_3)
p_seird_3 = decoration(pc_seird_3)
display_wd(seird_3)
# -
Graph(p_seird_3)

# Define time frame and initial parameters

tspan = (0.0,90.0)
u0 = zeros(Float64, length(base(pc_seird_3)))
u0[1]  = 10000
u0[6]  = 10000
u0[11] = 10000
u0[2]  = 1

seirdparams(n::Int, k::Number) = begin
    βseird = [10/sum(u0), 1/2, 1/5, 1/16]
    βtravel = [1/20, 1/200, 1/20]/100k
    β = vcat(βseird, βtravel)
    return foldl(vcat, [(1-(0/(2n)))*β for i in 1:n])
end
params = seirdparams(3, 5);

# Generate, solve, and visualize resulting ODE

prob = ODEProblem(vectorfield(p_seird_3),u0,tspan,params);
sol = solve(prob,Tsit5());

plot(sol)

# Define different dynamic transition function rates

asymptotic(a,b,k=1) = t->(b + (a-b)/(k*t+1))
triangleasm(a,b,k=1) = t->max(b, (1-t/k)*(a-b)+b)
trianglewave(a,b,k=1) = t->((a-b)/π)*asin(sin(t*2π/k))+2b
coswave(a,b,k=1) = t->(a-b)*(cos(t*2π/k)/2)+( a+b )/2
sincwave(a,b,k=1) = t->(a-b)*(sinc(t*2π/k)/2)+( a+b )/2
modsincwave(a,b,k) = t-> max(b, (a-b)*(sinc(t*2π/k))+(b))
dynseirdparams(f, a,b,period, n::Int, scale::Number) = begin
    βseird = [f(a,b,period), 1/2, 1/5, 1/16]
    βtravel = [1/20, 1/200, 1/20]/100scale
    β = vcat(βseird, βtravel)
    return foldl(vcat, [β for i in 1:n])
end

waveparams(f,a,b,p) = begin
    dynseirdparams(f,a,b,p,3,5)
end;

# Generate, solve, and visualize ODE of a dynamic flow rates

tspan = (0, 240.0)
a,b,p = 10, 1, 1/6

dynparams = waveparams(asymptotic, a/sum(u0), b/sum(u0),p)

prob = ODEProblem(vectorfield(p_seird_3),u0,tspan, dynparams)
sol = solve(prob,Tsit5(), saveat=1:1:tspan[2])

plot(sol)

max_stars_repo_name: AlgebraicJulia/AlgebraicPetri

max_issues_repo_name: AlgebraicJulia/AlgebraicPetri

max_forks_repo_name: AlgebraicJulia/AlgebraicPetri


text:
#Initialize neural network --> Restricted Boltzmann machine (RBM)

using Random

#define structure type containing the visible biases a,
#the hidden biases b and the weights W of the RBM.
struct RBM_par
    a::Array{Complex{Float64},1}
    b::Array{Complex{Float64},1}
    W::Array{Complex{Float64},2}
end

#initialize the network parameters with random numbers
function init_RBM_par(alpha::Int64, nspins::Int64)
    #generate random seed dependent on the network size alpha and  number of spins nspins
    seed_ = alpha*nspins*rand(0:20000);
    Random.seed!(seed_)

    #initiialize visible biases as vector of zeros
    a_ = zeros(Complex,nspins)

    #iniztialize hiddens biasees as vector of complex random numbers
    b_ = rand(-0.01:0.0001:0.01,nspins*alpha) .+ im*rand(-0.01:0.0001:0.01,nspins*alpha)

    #iniztialize weights as matrix of complex random numbers
    W_ = rand(-0.01:0.0001:0.01,nspins,nspins*alpha) .+ im*rand(-0.01:0.0001:0.01,nspins,nspins*alpha)

    return RBM_par(a_,b_,W_)
end

#Calculate the wavefunction projected onto the input state
function RBM_wf(par::RBM_par,state::Array{Int64,1},alpha::Int64,nspins::Int64)

    rbm = sum(par.a .* state)

    for h=1:alpha*nspins
        @views thetah = par.b[h] .+ sum(state[v] .* par.W[:,h]);
        rbm += log(2cosh(thetah));
    end;

    return exp(rbm);
end;

ext: jl

max_stars_repo_name: GiamFbn/MyULTRAFAST

max_issues_repo_name: GiamFbn/MyULTRAFAST

max_forks_repo_name: GiamFbn/MyULTRAFAST


text:
## CAOL signatures

SignalBank{N}  = AbstractVector{<:AbstractArray{<:Any,N}}    # List of arrays
SignalTuple{N} = Tuple{<:AbstractMatrix,NTuple{N,<:Integer}} # Columns with shape

# x::SignalBank, h0::SignalBank
function CAOL(x::SignalBank{N},λ::Real,h0::SignalBank{N};
        p=0,maxiters=2000,tol=1e-13,trace=false) where N
    @assert p < length(h0)

    H0,R = _filtermatrix(h0[p+1:end])
    if !trace
        H = _CAOL(x,λ,H0,R,maxiters,tol)
        return [h0[1:p]; Array.(_filterlist(H,R))]
    else
        H, Htrace, objtrace, Hdifftrace = _CAOLtrace(x,λ,H0,R,maxiters,tol)
        return [h0[1:p]; Array.(_filterlist(H,R))], _filterlist.(Htrace), objtrace, Hdifftrace
    end
end

# x::SignalBank, (H0,R)::SignalTuple
function CAOL(x::SignalBank{N},λ::Real,(H0,R)::SignalTuple{N};
        p=0,maxiters=2000,tol=1e-13,trace=false) where N
    @assert p < size(H0,2)

    if !trace
        H = _CAOL(x,λ,H0[:,p+1:end],R,maxiters,tol)
        return [H0[:,1:p] H]
    else
        H, Htrace, objtrace, Hdifftrace = _CAOLtrace(x,λ,H0[:,p+1:end],R,maxiters,tol)
        return [H0[:,1:p] H], Htrace, objtrace, Hdifftrace
    end
end

# X::AbstractArray
_extractsignals(X::AbstractArray) = collect(eachslice(X,dims=ndims(X)))
CAOL(X::AbstractArray,λ::Real,h0::SignalBank; p=0,maxiters=2000,tol=1e-13,trace=false) =
    CAOL(_extractsignals(X),λ,h0; p=p,maxiters=maxiters,tol=tol,trace=trace)
CAOL(X::AbstractArray,λ::Real,H0R::SignalTuple; p=0,maxiters=2000,tol=1e-13,trace=false) =
    CAOL(_extractsignals(X),λ,H0R; p=p,maxiters=maxiters,tol=tol,trace=trace)


text:
"""
    ConjugateGradients(d,operators,parameters;<keyword arguments>)
Conjugate Gradients following Algorithm 2 from Scales, 1987.
The user provides an array of linear operators. Verify that linear operator(s) pass the dot product.
See also: [`DotTest`](@ref)

# Arguments
- `Niter=10` : Number of iterations
- `mu=0`
- `tol=1.0e-15`

"""
function ConjugateGradients(d,operators,parameters;Niter=10,mu=0,tol=1.0e-15)

    cost = Float64[]
    r = copy(d)
    g = LinearOperator(r,operators,parameters,adj=true)
    m = zero(g)
    s = copy(g)
    gamma = InnerProduct(g,g)
    gamma00 = gamma
    cost0 = InnerProduct(r,r)
    push!(cost,1.0)
    for iter = 1 : Niter
	t = LinearOperator(s,operators,parameters,adj=false)
	delta = InnerProduct(t,t) + mu*InnerProduct(s,s)
	if delta <= tol
#	    println("delta reached tolerance, ending at iteration ",iter)
	    break;
	end
	alpha = gamma/delta
	m = m + alpha*s
	r = r - alpha*t
	g = LinearOperator(r,operators,parameters,adj=true)
	g = g - mu*m
	gamma0 = copy(gamma)
	gamma = InnerProduct(g,g)
        cost1 = InnerProduct(r,r) + mu*InnerProduct(m,m)
        push!(cost,cost1/cost0)
	beta = gamma/gamma0
	s = beta*s + g
	if (sqrt(gamma) <= sqrt(gamma00) * tol)
	    println("tolerance reached, ending at iteration ",iter)
	    break;
	end
    end

    return m, cost
end


text:
module FrostHelperTheAbyssJumpStar

using ..Ahorn, Maple

@mapdef Entity "FrostHelper/JumpStar" JumpStar(x::Integer, y::Integer, mode::String="Jump", directory::String="theAbyssJumpStar", strength::Integer=1)

const modes = String[
    "Jump",
    "Dash"
]

Ahorn.editingOptions(entity::JumpStar) = Dict{String, Any}(
	"mode" => modes
)

const placements = Ahorn.PlacementDict(
    "Jump Star (Frost Helper)" => Ahorn.EntityPlacement(
        JumpStar
    )
)

function Ahorn.selection(entity::JumpStar)
    x, y = Ahorn.position(entity)

    return Ahorn.getSpriteRectangle("theAbyssJumpStar/Jump/0star00", x, y)
end

function Ahorn.render(ctx::Ahorn.Cairo.CairoContext, entity::JumpStar, room::Maple.Room)
    directory = get(entity.data, "directory", "theAbyssJumpStar")
    mode = get(entity.data, "mode", "Jump")
    strength = string(get(entity.data, "strength", 0))

    Ahorn.drawSprite(ctx, "$directory/$mode/$(strength)star00", 0, 0)
end

end

max_stars_repo_name: JaThePlayer/FrostHelper

max_issues_repo_name: JaThePlayer/FrostHelper

max_forks_repo_name: JaThePlayer/FrostHelper


text:
module Presolve

include("types.jl")
include("postsolve.jl")
include("manipulation.jl")
include("subroutines.jl")
include("solver_info.jl")
include("PresolveInterface.jl")

end


text:
@block SimonDanisch [layout] begin

    @cell "Layouting" [scatter, lines, surface, heatmap, vbox] begin
        p1 = scatter(RNG.rand(10), markersize = 1)
        p2 = lines(RNG.rand(10), RNG.rand(10))
        p3 = surface(0..1, 0..1, RNG.rand(100, 100))
        p4 = heatmap(RNG.rand(100, 100))
        x = 0:0.1:10
        p5 = lines(0:0.1:10, sin.(x))
        pscene = vbox(
            hbox(p1, p2),
            p3,
            hbox(p4, p5, sizes = [0.7, 0.3]),
            sizes = [0.2, 0.6, 0.2]
        )
    end

    @cell "Comparing contours, image, surfaces and heatmaps" [image, contour, surface, heatmap, vbox] begin
        N = 20
        x = LinRange(-0.3, 1, N)
        y = LinRange(-1, 0.5, N)
        z = x .* y'
        hbox(
            vbox(
                contour(x, y, z, levels = 20, linewidth =3),
                contour(x, y, z, levels = 0, linewidth = 0, fillrange = true),
                heatmap(x, y, z),
            ),
            vbox(
                image(x, y, z, colormap = :viridis),
                surface(x, y, fill(0f0, N, N), color = z, shading = false),
                image(-0.3..1, -1..0.5, AbstractPlotting.logo())
            )
        )
    end
end


text:
# Useful integer math stuff

export trinum, triinv, triinv_strict, triinv_rem

"""
$(TYPEDSIGNATURES)

Get the `n`th triangular number.

# See also
[`triinv`](@ref)
"""
@inline trinum(n::Integer) = n * (n + 1) ÷ 2

"""
$(TYPEDSIGNATURES)

Get the largest `n` such that `t >= trinum(n)`.

# See also
[`triinv_strict`](@ref), [`trinum`](@ref)
"""
@inline triinv(t::Integer) = _triinv(t, Val{false}())

"""
$(TYPEDSIGNATURES)

Get `n` such that `t` is the `n`th triangular number.
If `t` is not a triangular number, throw a `DomainError`.

# See also
[`triinv`](@ref), [`trinum`](@ref)
"""
@inline triinv_strict(t::Integer) = _triinv(t, Val{true}())

function _triinv(t::Integer, ::Val{strict}) where strict
	t >= 0 || throw(DomainError(t, "Must be nonnegative"))
	s = 8 * t + 1
	rs = isqrt(s)
	strict && rs^2 != s && throw(DomainError(t, "Not a triangular number"))
	return (rs - 1) ÷ 2
end

"""
$(TYPEDSIGNATURES)

Get `n` and `r >= 0` such that `t == trinum(n) + r` and `t < trinum(n + 1)`.

# See also
[`triinv`](@ref), [`trinum`](@ref)
"""
function triinv_rem(t::Integer)
	n = triinv(t)
	return n, t - trinum(n)
end


text:
# This file was generated by the Julia Swagger Code Generator
# Do not modify this file directly. Modify the swagger specification instead.

@doc raw"""ListMeta describes metadata that synthetic resources must have, including lists and various status objects. A resource may have only one of {ObjectMeta, ListMeta}.

    IoK8sApimachineryPkgApisMetaV1ListMeta(;
        __continue__=nothing,
        remainingItemCount=nothing,
        resourceVersion=nothing,
        selfLink=nothing,
    )

    - __continue__::String : continue may be set if the user set a limit on the number of items returned, and indicates that the server has more data available. The value is opaque and may be used to issue another request to the endpoint that served this list to retrieve the next set of available objects. Continuing a consistent list may not be possible if the server configuration has changed or more than a few minutes have passed. The resourceVersion field returned when using this continue value will be identical to the value in the first response, unless you have received this token from an error message.
    - remainingItemCount::Int64 : remainingItemCount is the number of subsequent items in the list which are not included in this list response. If the list request contained label or field selectors, then the number of remaining items is unknown and the field will be left unset and omitted during serialization. If the list is complete (either because it is not chunking or because this is the last chunk), then there are no more remaining items and this field will be left unset and omitted during serialization. Servers older than v1.15 do not set this field. The intended use of the remainingItemCount is *estimating* the size of a collection. Clients should not rely on the remainingItemCount to be set or to be exact.
    - resourceVersion::String : String that identifies the server's internal version of this object that can be used by clients to determine when objects have changed. Value must be treated as opaque by clients and passed unmodified back to the server. Populated by the system. Read-only. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency
    - selfLink::String : selfLink is a URL representing this object. Populated by the system. Read-only.  DEPRECATED Kubernetes will stop propagating this field in 1.20 release and the field is planned to be removed in 1.21 release.
"""
mutable struct IoK8sApimachineryPkgApisMetaV1ListMeta <: SwaggerModel
    __continue__::Any # spec type: Union{ Nothing, String } # spec name: continue
    remainingItemCount::Any # spec type: Union{ Nothing, Int64 } # spec name: remainingItemCount
    resourceVersion::Any # spec type: Union{ Nothing, String } # spec name: resourceVersion
    selfLink::Any # spec type: Union{ Nothing, String } # spec name: selfLink

    function IoK8sApimachineryPkgApisMetaV1ListMeta(;__continue__=nothing, remainingItemCount=nothing, resourceVersion=nothing, selfLink=nothing)
        o = new()
        validate_property(IoK8sApimachineryPkgApisMetaV1ListMeta, Symbol("continue"), __continue__)
        setfield!(o, Symbol("__continue__"), __continue__)
        validate_property(IoK8sApimachineryPkgApisMetaV1ListMeta, Symbol("remainingItemCount"), remainingItemCount)
        setfield!(o, Symbol("remainingItemCount"), remainingItemCount)
        validate_property(IoK8sApimachineryPkgApisMetaV1ListMeta, Symbol("resourceVersion"), resourceVersion)
        setfield!(o, Symbol("resourceVersion"), resourceVersion)
        validate_property(IoK8sApimachineryPkgApisMetaV1ListMeta, Symbol("selfLink"), selfLink)
        setfield!(o, Symbol("selfLink"), selfLink)
        o
    end
end # type IoK8sApimachineryPkgApisMetaV1ListMeta

const _property_map_IoK8sApimachineryPkgApisMetaV1ListMeta = Dict{Symbol,Symbol}(Symbol("continue")=>Symbol("__continue__"), Symbol("remainingItemCount")=>Symbol("remainingItemCount"), Symbol("resourceVersion")=>Symbol("resourceVersion"), Symbol("selfLink")=>Symbol("selfLink"))
const _property_types_IoK8sApimachineryPkgApisMetaV1ListMeta = Dict{Symbol,String}(Symbol("continue")=>"String", Symbol("remainingItemCount")=>"Int64", Symbol("resourceVersion")=>"String", Symbol("selfLink")=>"String")
Base.propertynames(::Type{ IoK8sApimachineryPkgApisMetaV1ListMeta }) = collect(keys(_property_map_IoK8sApimachineryPkgApisMetaV1ListMeta))
Swagger.property_type(::Type{ IoK8sApimachineryPkgApisMetaV1ListMeta }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_IoK8sApimachineryPkgApisMetaV1ListMeta[name]))}
Swagger.field_name(::Type{ IoK8sApimachineryPkgApisMetaV1ListMeta }, property_name::Symbol) =  _property_map_IoK8sApimachineryPkgApisMetaV1ListMeta[property_name]

function check_required(o::IoK8sApimachineryPkgApisMetaV1ListMeta)
    true
end

function validate_property(::Type{ IoK8sApimachineryPkgApisMetaV1ListMeta }, name::Symbol, val)
end


text:
import LinearAlgebra: norm

""" Gradient descent for multidimensional functions Stops when the gradient is smaller than `TOL`,
    or when the maximum number of iterations `maxiter` has been reached"""
function gradient_descent(f, Df, x; α = 0.1, TOL = 1e-10, maxiter = 1000, verbose = false)
    N_iter = 0
    grad = Df(x)

    xn = x
    fn = [f(x)]
    while (N_iter < maxiter) & (norm(grad) > TOL)
        x = x - α*grad

        xn = [xn x]
        fn = [fn f(x)]

        grad = Df(x)
        N_iter += 1

        if (N_iter % 100 == 0) & (verbose == true)# print progress
            fx = fn[end]
            println("Iter. $N_iter,\tx = $x,\tf(x) = $fx")
        end
    end

    return (xn', reshape(fn,:)) # to have the components in the columns
end

max_stars_repo_name: ismedina/julia-scientific-computing

max_issues_repo_name: ismedina/julia-scientific-computing

max_forks_repo_name: ismedina/julia-scientific-computing


text:
module TestConstants

using Base.Test
using SoilTracers: Constants

@time @testset "Physical constants" begin
    @test typeof(Constants.T_0) <: AbstractFloat
    @test isapprox(Constants.T_0, 273.15)

    @test typeof(Constants.R) <: AbstractFloat
    @test isapprox(Constants.R, 8.314, rtol=1e-3)

    @test typeof(Constants.g) <: AbstractFloat
    @test isapprox(Constants.g, 9.81, rtol=1e-3)

    @test typeof(Constants.atm) <: AbstractFloat
    @test isapprox(Constants.atm, 101325., rtol=1e-3)

    @test typeof(Constants.M_w) <: AbstractFloat
    @test isapprox(Constants.M_w, 18.015e-3, rtol=1e-3)
end

end


text:
using Base.Filesystem:ispath,isfile,joinpath,mkdir
using Base:download
using Printf:@sprintf
using LinearAlgebra
using Combinatorics
using Random
using StatsBase:sample
function download_dataset(root_path,filename)
    url = "http://elib.zib.de/pub/mp-testdata/tsp/tsplib/tsp/"*filename
    if !ispath(root_path)
        mkdir(root_path)
    end
    save_file_path = joinpath(root_path,filename)
    if !isfile(filename)
        download(url,save_file_path)
        message=@sprintf("The data set: %s downloaded!",save_file_path)
        @info message
    else
        message = @sprintf("The data set: %s already has downloaded!",save_file_path)
        @info message
    end
end
function get_data(filename)
    data_list = []
    open(filename,"r") do stream
        flag = false
        for line in eachline(stream)
            if occursin("EOF",line)
                break
            elseif occursin("NODE_COORD_SECTION",line)
                flag = true
            elseif flag
                tp_list = []
                output = split(line,['\t',' '])
                vala = parse(Int64,output[1])
                valb = parse(Float32,output[2])
                valc = parse(Float32,output[3])
                append!(tp_list,vala)
                append!(tp_list,valb)
                append!(tp_list,valc)
                append!(data_list,tp_list)
            end
        end
    end
    arr_len = Int64(length(data_list)/3)
    data_list = reshape(data_list,3,arr_len)
    return data_list'
end
function random_array(citys_num)
    tp_mat = [i for i in range(1,stop=citys_num,step=1)]
    tp_mat = shuffle(tp_mat)
    return tp_mat
end
function randseed(ant_num,citys_num)
    if ant_num <citys_num
        tp_mat = random_array(citys_num)
        initial_route = tp_mat[:ant_num]
    else
        initial_route = zeros(Int64,ant_num)
        initial_route[1:citys_num] = random_array(citys_num)
        tmp_index = citys_num+1
        tmp_left = ant_num % citys_num
        while tmp_index + citys_num <= ant_num
            initial_route[tmp_index:citys_num + tmp_index-1] = random_array(citys_num)
            tmp_index = tmp_index + citys_num
        end
        if tmp_left != 0
            tp_mat = random_array(citys_num)
            initial_route[tmp_index:end] = tp_mat[1:tmp_left]
        end
    end
    return initial_route
end

function main()
    filename = "eil51.tsp"
    root_path = "data"
    download_dataset(root_path,filename)
    data_file_path = joinpath(root_path,filename)
    citys_mat = get_data(data_file_path)
    ant_num,alpha,beta,rho,Q,epoches = 500,1,5,0.2,10,20
    # 获取城市的个数
    citys_num = size(citys_mat)[1]
    # 获取邻接矩阵
    citys_x = reshape(citys_mat[:,2],citys_num,1).*ones(1,citys_num)
    citys_y = reshape(citys_mat[:,3],citys_num,1).*ones(1,citys_num)
    citys_index = citys_mat[:,1]
    citys_distance = sqrt.((citys_x-citys_x').^2+(citys_y-citys_y').^2)
    # 初始化启发函数

    tpMatrix = zeros(citys_num,citys_num)
    for k in range(1,stop=citys_num,step=1)
        tpMatrix[k,k] = 1.0*Inf32
    end
    Heu_f = 1.0./(citys_distance + tpMatrix)
    # 信息素矩阵
    Tau_table = ones(Float32,citys_num,citys_num)
    # 每一次迭代过程中每个蚂蚁的路径记录表
    Route_table = zeros(Int64,ant_num,citys_num,)
    #Route_table = parse(Int64,Route_table)
    # 每一次迭代过程中的最佳路径
    Route_best = zeros(Int64,epoches,citys_num)
    #Route_best = parse(Int64,Route_best)
    # 每一次迭代过程中最佳路径记录表
    Length_best = zeros(Float32,epoches)
    # 每次迭代过程中蚂蚁的平均路径长度
    Length_average = zeros(Float32,epoches)
    # 每次迭代过程中当前路径长度
    Length_current = zeros(Float32,ant_num)
    iter = 1
    while iter<=epoches
        # 产生城市集合表
        # 随机产生各个蚂蚁的起点城市
        #println(size(Route_table))
        Route_table[:,1]= randseed(ant_num,citys_num)

        # 更新信息素
        Delta_tau = zeros(Float32,citys_num, citys_num)
        for k in range(1,stop=ant_num,step=1)
            # 用于记录蚂蚁下一个访问的城市集合
            # 蚂蚁已经访问过的城市
            tabu = [Route_table[k,1]]

            city_index = Route_table[k,1]
            for i=2:citys_num
                allow_set = setdiff(Set([i for i in range(1,stop=citys_num,step=1)]),Set(tabu))
                allow_set = [item for item in allow_set]
                # 初始化城市之间的转移概率
                P_table = zeros(Float32,length(allow_set))
                # 计算城市之间的转移概率

                for item in enumerate(allow_set)
                    j = item[1]
                    val = item[2]
                    P_table[j] = Tau_table[city_index,val]^alpha*Heu_f[city_index,val]^beta
                end
                P_table = P_table./sum(P_table)
                # 轮盘赌算法来选择下一个访问的城市
                #out_prob = np.cumsum(P_table)
                while true
                    prob_r = rand()
                    index_need = findall(x->x >= prob_r,P_table)
                    if length(index_need)>0
                        city_index2 = allow_set[index_need[1]]
                        Route_table[k,i] = city_index2
                        append!(tabu,city_index2)
                        city_index = city_index2
                        break
                    end
                end
            end
            append!(tabu,tabu[1])
            # 计算蚂蚁路径的距离信息
            for j=1:citys_num
                Length_current[k] = Length_current[k] + citys_distance[tabu[j],tabu[j+1]]
            end
            for j in range(1,stop=citys_num,step=1)
                Delta_tau[tabu[j],tabu[j+1]] = Delta_tau[tabu[j],tabu[j+1]] + Q / Length_current[k]
            end
        end
        #println(typeof(Route_table))
        #println(size(Route_table))
        # 计算最短路径、最短路径长度以及平均路径长度
        minval = findmin(Length_current)
        Length_best[iter],index = minval[1],minval[2]
        Route_best[iter,:] = Route_table[index,:]

        Length_average[iter] = sum(Length_current)/length(Length_current)
        #更新信息素
        Tau_table = (1-rho).*Tau_table + Delta_tau
        #Route_table = np.zeros((self.ant_num,citys_num),dtype=np.int)
        Length_current = zeros(ant_num)
        message = @sprintf("epoches:%d,best value every epoches is %.4f",iter,Length_best[iter])
        @info message
        iter += 1
        Array
    end
end
main()

max_stars_repo_name: MobTgZhang/CIAlgorithms

max_issues_repo_name: MobTgZhang/CIAlgorithms

max_forks_repo_name: MobTgZhang/CIAlgorithms


text:
using Dates

portuguese_months = [
    "janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho",
    "agosto", "setembro", "outubro", "novembro", "dezembro"
]
portuguese_months_abbrev = [
    "jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out",
    "nov", "dez"
]
portuguese_days = ["domingo", "segunda", "terça", "quarta", "quinta", "sexta", "sábado"]
portuguese_days_abbrev = ["dom", "seg", "ter", "qua", "qui", "sex", "sab"]
Dates.LOCALES["portuguese"] = Dates.DateLocale(portuguese_months, portuguese_months_abbrev, portuguese_days, portuguese_days_abbrev)

"""
    {{ cpfiletosite filenamvec }}

Copy the given files to the site.

It is used in `index.md` to copy the webpage/README.md to the final website
repository, which is needed since README.md is ignored by Franklin.
"""
function hfun_cpfiletosite(filenamevec)
    for i in 1:length(filenamevec)
        cp(filenamevec[i], joinpath("__site", filenamevec[i]); force=true)
    end
    return ""
end

"""
    {{ blogposts lang }}

Add the list of blog posts contained in the `/blog/` folder.
Adapted from JuliaLang Franklin-generated website.
Language variable `lang` is used to set the language for displaying
the blog post dates.
"""
function hfun_blogposts(lang)
    curyear = year(Dates.today())
    io = IOBuffer()
    for year in curyear:-1:2021
        ys = "$year"
        year < curyear && write(io, "\n**$year**\n")
        for month in 12:-1:1
            ms = "0"^(month < 10) * "$month"
            base = joinpath("blog", ys, ms)
            isdir(base) || continue
            posts = filter!(p -> endswith(p, ".md"), readdir(base))
            days  = zeros(Int, length(posts))
            lines = Vector{String}(undef, length(posts))
            for (i, post) in enumerate(posts)
                ps  = splitext(post)[1]
                url = "/blog/$ys/$ms/$ps/"
                surl = strip(url, '/')
                title = pagevar(surl, :title)
                pubdate = pagevar(surl, :published)
                if isnothing(pubdate)
                    rawdate = Date(year, month, 1)
                    days[i] = 1
                else
                    rawdate = Date(pubdate, dateformat"d U Y")
                    days[i] = day(rawdate)
                end
                if lang[1] == "portuguese"
                    date = replace(Dates.format(rawdate, "d U YYYY", locale=lang[1]), " " => " de ")
                else
                    date = Dates.format(rawdate, "U d, YYYY")
                end
                lines[i] = "\n[$title]($url)\n$date\n"
            end
            # sort by day
            foreach(line -> write(io, line), lines[sortperm(days, rev=true)])
        end
    end
    # markdown conversion adds `<p>` beginning and end but
    # we want to  avoid this to avoid an empty separator
    r = "
\n" *
        Franklin.fd2html(String(take!(io)), internal=true) *
        "\n
\n"
    return r
end

"""
    {{ blogcomments }}

Add a comment javascript section, managed by the utterances app <https://utteranc.es>.
"""
function hfun_blogcomments()
    html_str = """

    """
    return html_str
end

function hfun_fullcodedownload()
    mdfile = locvar(:fd_rpath)
    mdpath = mdfile[1:end-3]
    codepath = joinpath("__site", "assets", mdpath, "code")
    fullcodepath = joinpath(codepath,"full_code.jl")
    regex = r"```julia:(.*)"
    header = """
# Title: $(locvar(:title))
# Publication date: $(locvar(:published))
# Last modified: $(locvar(:fd_mtime))
# Code from https://rmsrosa.github.io/$mdpath/"""
    open(f->write(f, header), fullcodepath, "w")
    for m in eachmatch(regex, read(mdfile, String))
        jlfile = joinpath(codepath,(m[1])*".jl")
        open(f->write(f, "\n\n# Code snippet: $(split(jlfile,'/')[end])\n"), fullcodepath, "a")
        run(pipeline(`cat $jlfile`, stdout=open(fullcodepath,"a")))
    end
    html_str = """⬇ Download the full julia code """
    return html_str
end

max_stars_repo_name: rmsrosa/WebsiteGen

max_issues_repo_name: rmsrosa/WebsiteGen

max_forks_repo_name: rmsrosa/WebsiteGen


text:
using XConv, LinearAlgebra, Flux, CUDA, PyPlot, BenchmarkTools
BLAS.set_num_threads(2)

nx = 128
ny = 128
batchsize=10
n_in = 2
n_out = 2
stride = 1
n_bench = 5
nw   = 3;

X = randn(Float32, nx, ny, n_in, batchsize) |> gpu
Y = randn(Float32, nx, ny, n_out, batchsize) |> gpu
g20 = grad_ev(X, Y, 5, nw, stride);
# Flux network
C = Conv((nw, nw), n_in=>n_out, identity;pad=1, stride=stride) |> gpu

batches = [2^k for k=0:8]

tf = zeros(length(batches))
t1 = similar(tf)
t10 = similar(tf)
t50 = similar(tf)
t100 = similar(tf)
angles = zeros(length(batches), 4)

close("all")

# Init plot
fig, axsl = subplots(3, 3, figsize=(10, 5))
fig.suptitle("Conv layer gradient chi-$(n_in), cho-$(n_out)")

for (i, b)=enumerate(batches)
    println("Gradient for batchsize=$b")

    local X = rand([-1f0, 1f0], nx, ny, n_in, b) |> gpu
    local Y = C(X) - gpu(randn(Float32, nx, ny, n_out, b))

    cdims = DenseConvDims(X, C.weight; stride=C.stride, padding=C.pad, dilation=C.dilation)

    # Run once to get gradient
    @time local g1 = ∇conv_filter(X, Y, cdims)
    @time local g20 = grad_ev(X, Y, 5, nw, stride);
    @time local g21 = grad_ev(X, Y, 10, nw, stride);
    @time local g22 = grad_ev(X, Y, 50, nw, stride);
    @time local g23 = grad_ev(X, Y, 100, nw, stride);

    # Compute similarities
    angles[i, 1] = dot(g20, g1)/(norm(g20)*norm(g1))
    angles[i, 2] = dot(g21, g1)/(norm(g21)*norm(g1))
    angles[i, 3] = dot(g22, g1)/(norm(g22)*norm(g1))
    angles[i, 4] = dot(g23, g1)/(norm(g23)*norm(g1))

    # Benchmark runtime
    tf[i] = @belapsed ∇conv_filter($X, $Y, $cdims) samples=n_bench
    t1[i] = @belapsed grad_ev($X, $Y, 5, $nw, $stride) samples=n_bench
    t10[i] = @belapsed grad_ev($X, $Y, 10, $nw, $stride) samples=n_bench
    t50[i] = @belapsed grad_ev($X, $Y, 50, $nw, $stride) samples=n_bench
    t100[i] = @belapsed grad_ev($X, $Y, 100, $nw, $stride) samples=n_bench

    # Plot result
    axsl[i].plot(vec(g20)/norm(g20, Inf), label="LR(s=5)", "-r")
    axsl[i].plot(vec(g21)/norm(g21, Inf), label="LR(s=10)", "-b")
    axsl[i].plot(vec(g22)/norm(g22, Inf), label="LR(s=50)", "-g")
    axsl[i].plot(vec(g23)/norm(g23, Inf), label="LR(s=100))", "-c")
    axsl[i].plot(vec(g1)/norm(g1, Inf), label="Flux", "-k")
    axsl[i].set_title("batchsize=$b")
end
lines, labels = fig.axes[end].get_legend_handles_labels()
fig.legend(lines, labels, loc = "upper left")
tight_layout()

figure()
title("10 Gradient computation speedup")
plot(batches, tf, label="Flux")
plot(batches,t1, label="LR(s=5)")
plot(batches,t10, label="LR(s=10)")
plot(batches,t50, label="LR(s=50)")
plot(batches,t100, label="LR(s=100)")
xlabel("Batch size")
ylabel("Runtime(s)")
legend()

figure()
title("Similarty x' x2 / (||x|| ||x2||)")
plot(batches,angles[:, 1], label="LR(s=5)")
plot(batches,angles[:, 2], label="LR(s=10)")
plot(batches,angles[:, 3], label="LR(s=50)")
plot(batches,angles[:, 4], label="LR(s=100)")
legend()

@show angles

max_stars_repo_name: sheevy/XConv

max_issues_repo_name: sheevy/XConv

max_forks_repo_name: sheevy/XConv


text:
module BuildTools

export build

using ..ShuHua
using ..Types
using ..Write

using Pkg.PlatformEngines
const REVEALJS = "https://github.com/hakimel/reveal.js/archive/"

const SKIP_EXTS = [".md", ".toml"]
const IGNORE_PATH = ["build"]

revealjs(version) = ShuHua.project("deps", "reveal.js-" * version)
revealjs_version(p::Presentation) = p.meta["revealjs"]["version"]
revealjs(p::Presentation) = revealjs(revealjs_version(p))

function build(p::Presentation, build_dir::String)
    download_revealjs(revealjs_version(p))

    if p.standalone
        build_file(p, build_dir)
    else
        build_project(p, build_dir)
    end
end

function build(p::Presentation)
    download_revealjs(revealjs_version(p))

    if p.standalone
        build_file(p)
    else
        build_project(p)
    end
end

function download_revealjs(version::String)
    if ispath(revealjs(version))
        return
    end

    url = REVEALJS * version * ".tar.gz"
    tarball = ShuHua.project("deps", version * ".tar.gz")
    ispath(ShuHua.project("deps")) || mkpath(ShuHua.project("deps"))
    download(url, tarball)
    PlatformEngines.probe_platform_engines!()
    unpack(tarball, ShuHua.project("deps"))
    return
end

function build_file(p::Presentation)
    name = splitext(basename(p.path))[1]
    return build_file(p, mktempdir(prefix=name * "_"))
end

function build_project(p::Presentation)
    name = basename(p.path)
    build_dir = joinpath(p.path, "build")
    ispath(build_dir) || mkpath(build_dir)

    return build_project(p, build_dir)
end

function build_project(p::Presentation, build_dir)
    build_file(p, build_dir)
    # copy materials
    for dir in readdir(p.path)
        if !startswith(dir, "_") && !startswith(dir, ".") && !skipfile(dir)
            cp(dir, joinpath(build_dir, dir); force=true)
        end
    end
    return build_dir
end

function build_file(p::Presentation, build_dir)
    path_revealjs = joinpath(build_dir, p.meta["revealjs"]["path"])
    ispath(path_revealjs) || cp(revealjs(p), path_revealjs)
    write(joinpath(build_dir, "index.html"), p)
    return build_dir
end

function skipfile(file)
    file in IGNORE_PATH && return true

    return any(SKIP_EXTS) do ext
        endswith(file, ext)
    end
end

end


text:
using BayesianLinearRegression

X = randn(100, 80);
β = randn(50);
y = X[:,1:50] * β + randn(100);

@time m = BayesianLinReg(X,y);

@time fit!(m);

logEvidence(m)

effectiveNumParameters(m)

priorScale(m)
noiseScale(m)

posteriorWeights(m)
posteriorVariance(m)

predict(m,X[:,m.active])

function getpath!(m)
    path = Pair{Int, Float64}[]
    while length(m.active) > 1
        bestj = 0
        bestlogEv = -Inf
        activebase = m.active
        for j in activebase
            m.active = setdiff(activebase, j)
            update!(m)
            fit!(m)

            thislogEv = logEvidence(m)
            if thislogEv > bestlogEv
                bestj = j
                bestlogEv = thislogEv
            end
        end

        push!(path, bestj => bestlogEv)
        println("removing ", bestj)
        m.active = setdiff(activebase, bestj)
    end
    return path
end

path = getpath!(m)

logEvs = [getproperty(p,:second) for p in path]

plot(logEvs)

effectiveNumParameters(m)

priorScale(m)
noiseScale(m)

posteriorWeights(m)
posteriorVariance(m)

predict(m,view(m.X, :, m.active))

function f(n)
    m = BayesianLinReg(X[:,1:n],y)
    fit!(m; callback=stopAtIteration(10))
    return m
end;

ms = f.(1:100);

argmax(logEvidence.(ms))
m = ms[49];

effectiveNumParameters(m)

priorScale(m)
noiseScale(m)

posteriorWeights(m)
posteriorVariance(m)

predict(m,m.X)

using Plots
using Statistics

logevs = logEvidence.(ms)

plot(3:100,logevs[3:100], legend=false)
xlabel!("Number of features used")
ylabel!("Log Evidence")

m = BayesianLinReg(X[:,1:50],y)
fit!(m; callback=stopAtIteration(10))


text:
using NPZ

#
# download data files into the data directory from here:
# https://github.com/dpcomp-org/dpcomp_core/tree/master/dpcomp_core/datafiles
#

getincome() = npzread("data/INCOME.n4096.npy")
getnet() = npzread("data/NETTRACE.n4096.npy")
getsearch() = npzread("data/SEARCHLOGS.n4096.npy")
getfrank() = npzread("data/ADULTFRANK.n4096.npy")

max_stars_repo_name: dkifer/dpcourse

max_issues_repo_name: dkifer/dpcourse

max_forks_repo_name: dkifer/dpcourse


text:
"""
# Usage

    solve(α, dx, dt, xStart, xEnd, n, κ, FiniteDiffEx())

!!! tip
    Here, if we set ``0<\\alpha\\leq 1``, the equation is the fractional diffusion equation or subdiffusion equation, whereas for ``1<\\alpha\\leq 2``, the equation is the fractional diffusion-wave equation.

Use explicit Caputo discretization method to solve the diffusion equations.

### References

```tex
@article{Murillo2011AnED,
  title={An Explicit Difference Method for Solving Fractional Diffusion and Diffusion-Wave Equations in the Caputo Form},
  author={Joaqu{\'i}n Quintana Murillo and Santos B. Yuste},
  journal={Journal of Computational and Nonlinear Dynamics},
  year={2011},
  volume={6},
  pages={021014}
}
```

Matlab version: https://github.com/awstown/Fractional-Derivative
"""
struct FiniteDiffEx <: FractionalDiffEqAlgorithm end

function solve(α, dx, dt, xStart, xEnd, n, κ, u0t, uendt, u0, ::FiniteDiffEx)
    x = collect(0:dx:xEnd)
    t = collect(0:dt:n)
    S = κ*((dt^α)/(dx^2))
    S_bar = gamma(3-α)*S

    # Preallocate
    U = zeros(Int64(n/dt + 1), round(Int, (xEnd - xStart)/dx + 1))

    # Boundry conditions
    U[:, 1] .= u0t
    U[:, end] .= uendt
    U[1, :] = u0.(x)

    k = collect(1:length(t)-2)
    bOfK = bbcoeff(k, α)
    test = Float64[]

    for m = 1:(length(t)- 1)
        # It is from 2 to end - 1 because of the boundry conditions
        U[m+1, 2:end-1], diff = nextStep(U, m, S_bar, test, bOfK)
    end
    return U
end

function bbcoeff(k, α)
    c = zeros(length(k))
    for i=1:length(k)
        if maximum(k) < 2
            @. c = (k+1)^(1-α) - k^(1-α)
        else
            @. c = (k+1)^(2-α) - k^(2-α)
        end
    end
    return c
end

# Compute values for each time step
function nextStep(U, m, S_bar, diff, bOfK)
    row_left   = U[m, 1:end-2]
    row_right  = U[m, 3:end]
    row_center = U[m, 2:end-1]

    if m < 2
        Uspatial = row_center + S_bar*(row_left - 2*row_center + row_right)
    else
        row_below  = U[m-1, 2:end-1]
        Uspatial = 2 * row_center - row_below + S_bar*(row_left - 2*row_center + row_right)
    end

    if m > 2
        if length(diff) == 0
            diff = U[m, 2:end-1] - 2*U[m-1, 2:end-1] + U[m-2, 2:end-1]
        else
            diff = [U[m, 2:end-1] .- 2 .*U[m-1, 2:end-1] .+ U[m-2, 2:end-1]; diff]
        end
        Utemporal = bOfK[1:length(diff[:, 1])]' * diff
        Unext = Uspatial .- Utemporal
    elseif m > 1
        if length(diff) == 0
            diff = U[m, 2:end-1] .- U[m-1, 2:end-1]
        else
            diff = [U[m, 2:end-1] .- U[m-1,2:end-1]; diff]
        end
        Utemporal = bOfK[1:length(diff[:, 1])]' * diff
        Unext = Uspatial .- Utemporal
    else
        Unext = Uspatial
    end
    return Unext, diff
end


text:
module aoc_04
    using Test
    export read_input, part1, part2
    export test_part1, test_part2, test
    export input, demo
    export required_keys

    const input_r = r"([a-z]*):(\S*)"

# byr (Birth Year)
# iyr (Issue Year)
# eyr (Expiration Year)
# hgt (Height)
# hcl (Hair Color)
# ecl (Eye Color)
# pid (Passport ID)
# cid (Country ID)

    const required_keys = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]

    function read_input(filename = "input.txt")
        lines = readlines(filename)
        # for line in eachline(filename)
        passports = Vector{Dict{String,String}}()
        data = Vector{Pair{String,String}}()
        for line in lines
            if isempty(line)
                push!(passports, Dict(data))
                empty!(data)
            else
                append!(data, [m.captures[1] => m.captures[2] for m in eachmatch(input_r, line)])
            end
        end
        push!(passports, Dict(data))
        # [parse_line(line) for line in eachline(filename)]
        passports
    end

    function parse_line(line)
    end

    const input = read_input("input.txt")
    const demo = read_input("demo.txt")

    function part1(input = input)
        N = length(required_keys)
        sum([sum(in.(required_keys,(keys(passport),))) == N for passport in input])
    end

    function test_part1()
        part1( demo )
    end

# byr (Birth Year) - four digits; at least 1920 and at most 2002.
# iyr (Issue Year) - four digits; at least 2010 and at most 2020.
# eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
# hgt (Height) - a number followed by either cm or in:
# If cm, the number must be at least 150 and at most 193.
# If in, the number must be at least 59 and at most 76.
# hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
# ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
# pid (Passport ID) - a nine-digit number, including leading zeroes.
# cid (Country ID) - ignored, missing or not.

    function validate(pair::Pair)
        key = pair.first
        value = pair.second
        try

        if key == "byr"
            1920 <= parse(Int,value) <= 2002
        elseif key == "iyr"
            2010 <= parse(Int,value) <= 2020
        elseif key == "eyr"
            2020 <= parse(Int,value) <= 2030
        elseif key == "hgt"
            m = match(r"(\d*)(in|cm)$",value)
            if isnothing(m)
                return false
            end
            height = parse(Int,m.captures[1])
            if m.captures[2] == "cm"
                150 <= height <= 193
            elseif m.captures[2] == "in"
                59 <= height <= 76
            else
                false
            end
        elseif key == "hcl"
            occursin(r"\#[0-9a-f]{6}", value)
        elseif key == "ecl"
            value ∈ ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
        elseif key == "pid"
            occursin(r"^[0-9]{9}$",value)
        elseif key == "cid"
            false
        else
            println(key)
            false
        end

        catch e
            println(e)
            println(key)
            println(value)
        end
    end

    function part2(input = input)
        N = length(required_keys)
        sum([sum(in.(required_keys,(keys(passport),))) == N && sum(validate.(collect(passport))) >= N for passport in input])
    end

    function test_part2()
        @test part2( demo ) == 2
        @test validate("byr" => "2002")
        @test !validate("byr" => "2003")
        @test validate("hgt" => "60in")
        @test validate("hgt" => "190cm")
        @test !validate("hgt" => "190in")
        @test !validate("hgt" => "190")
        @test validate("hcl" => "#123abc")
        @test !validate("hcl" => "#123abz")
        @test !validate("hcl" => "123abz")
        @test validate("ecl" => "brn")
        @test !validate("ecl" => "wat")
        @test validate("pid" => "000000001")
        @test !validate("pid" => "0123456789")
        @test part2( read_input("invalid.txt") ) == 0
        @test part2( read_input("valid.txt") ) == 4
        @test part2() == 101
    end

    function test()
        test_part1()
        test_part2()
    end
end


text:
# Discounted HEDEGEd KOM

# With p-loss
mutable struct dHedgePPM_1{T} <: BasePredictor{T}
    model::Trie{T,Int}
    context::Vector{T}
    cxt_length::Int
    β::Float64
    γ::Float64
    weights::Vector{Float64}
    last_prediction::Vector{Dict{T,Float64}}

    dHedgePPM_1{T}( _c::Int, _β::Float64 = 1.0, _γ::Float64 = 1.0 ) where {T} = new( Trie{T,Int}(), Vector{T}(),
                                                _c, _β, _γ,
                                                ones(Float64,_c+1), fill(Dict{T,Float64}(),_c+1) );
end

function add( p::dHedgePPM_1{T}, sym::T ) where {T}
    p.model.value += 1;

    # Update weights
    for expert_idx = 1:p.cxt_length+1
        expert_best_symbol  = get_best_symbol( p.last_prediction[expert_idx] );
        # expert_loss         = (expert_best_symbol==sym)?0:1;
        if expert_best_symbol != nothing
            expert_loss     = 1.0 - p.last_prediction[expert_idx][expert_best_symbol];
        else
            expert_loss     = 1.0;
        end
        p.weights[expert_idx] = (p.weights[expert_idx]^p.γ) * (p.β^expert_loss);
    end

    # Create node path
    push!( p.context, sym );
    buffer  = p.context[1:end];
    while !isempty(buffer)
        p.model[buffer] = (haskey(p.model,buffer)) ? p.model[buffer]+1 : 1;
        shift!( buffer )
    end

    if length(p.context) > p.cxt_length
        shift!( p.context )
    end
end

function predict( p::dHedgePPM_1{T} ) where {T}
    # Clear away last predictions
    p.last_prediction   = fill(Dict{T,Float64}(),p.cxt_length+1);

    # Calculate normalized weights
    norm_weights    = p.weights ./ sum(p.weights);
    # println( "    norm_weights: ", norm_weights );

    # Get prediction from 0-order expert
    symbols = Dict( k => (p.model[[k]]/p.model.value) for k ∈ keys(children(p.model,Vector{T}())) );

    # Save this for later
    p.last_prediction[1]    = deepcopy( symbols );

    # Apply 0-order expert weight
    for sym ∈ keys(symbols)
        symbols[sym] *= norm_weights[1];
    end
    # println( "After Order-0: ", symbols );

    # Do prediction from all other higher orders
    no_of_active_experts    = min( p.model.value, p.cxt_length );
    for expert_idx  = 1:no_of_active_experts
        # Predict
        this_expert_prediction = predict_from_subcontext( p, p.context[1:expert_idx] );
        # Normalize probability out from experts - jugaad
        denom   = 0;
        for (sym,prob) ∈ this_expert_prediction denom += prob; end
        for sym ∈ keys(this_expert_prediction) this_expert_prediction[sym] /= denom;    end

        # Save
        p.last_prediction[expert_idx+1] = deepcopy( this_expert_prediction );
        # Apply weight to expert and add to final prediction
        for sym ∈ keys( symbols )
            symbols[sym] += (this_expert_prediction[sym]*norm_weights[expert_idx+1]);
        end
        # println( "After order-$expert_idx: ", symbols );
    end

    return symbols;
end

function info_string( p::dHedgePPM_1{T} ) where {T}
    return @sprintf( "dHedgePPM1(%d,\$\\beta\$ = %3.2f,\$\\gamma\$ = %3.2f)",
                            p.cxt_length, p.β, p.γ );
end

function unique_string( p::dHedgePPM_1{T} ) where {T}
    return @sprintf( "dHedgePPM1_%02d_%03d_%03d", p.cxt_length,
                        trunc(Int,100*p.β), trunc(Int,100*p.γ)  );
end

function predict_from_subcontext( p::dHedgePPM_1{T}, sub_cxt::Vector{T} )  where {T}
    # Create a dictionary with symbols
    symbols = Dict( k => (p.model[[k]]/p.model.value) for k ∈ keys(children(p.model,Vector{T}())) );

    buffer  = Vector{T}();
    for i = length(sub_cxt):-1:1
        unshift!( buffer, sub_cxt[i] );
        # println( "    Buffer: ", buffer, " -> ", p.model[buffer] );
        # Apply escape probability
        esc_prob = 1/p.model[buffer];
        for symbol in keys(symbols)
            symbols[symbol] *= esc_prob;
        end
        # Get each child probability
        list_of_children    = children( p.model, buffer );
        for k ∈ keys(list_of_children)
            # println( "        ", k, " -> ", list_of_children[k].value )
            symbols[k] += list_of_children[k].value/p.model[buffer];
        end
    end

    return symbols;
end


text:
const openers = ('"', '\'', '(', '{', '[')
const closerdict = Dict('"'=>'"',
                        '\''=>'\'',
                        '('=>')',
                        '['=>']',
                        '{'=>'}')
const closers = (collect(values(closerdict))...,)
closerdict['/'] = '/'  # / is a weird delimiter since it also means division

function lex(io::IO)
    lexed = AbstractLex[]
    buf = IOBuffer()
    pos = 1
    lex!(io, buf, lexed, pos)
    return lexed
end

function lex!(io, buf, lexed, pos, cterminate = '\0')
    inrelation = false   # true if we're lexing "=e=", "=g=", or "=l="
    innumber = false     # true if we're lexing a number (starts with digit, then contains 0-9, ., e, E, and + or -)
    hadexponent = false  # true if we've seen e/E while lexing a number. Ensures that 3-5 isn't considered a single number.
    prevbool = false     # op that returns a boolean, only relevant for lexing "<=" and ">="
    while !eof(io)
        c = read(io, Char)
        if pos == 1 && (c == '*' || c == '$')
            # Comment line
            readline(io)
            continue
        end
        pos += 1
        if c != '='
            prevbool = false
        end
        if c == cterminate
            str = String(take!(buf))
            if !isempty(str)
                tag!(lexed, str)
            end
            return pos
        end
        isopener = c ∈ openers
        if isdigit(c)
            if position(buf) == 0
                innumber = true
            end
        elseif innumber && c == '.'
        elseif innumber && c ∈ ('e', 'E')
            hadexponent = true
        elseif innumber && c ∈ ('+', '-')
            if !hadexponent
                innumber = false
            end
        else
            innumber = hadexponent = false
        end
        if c == '/'
            # We have to distinguish division from "set" notation, so look backward for
            # a keyword
            i = lastindex(lexed)
            while i > 0
                lexi = lexed[i]
                if lexi isa StatementEnd
                    break
                end
                if lexi isa Keyword
                    isopener = lexi.text ∈ ("sets", "scalars", "parameters", "models") ? true : false
                    break
                end
                i -= 1
            end
        end
        if isopener
            pre = String(take!(buf))
            prelc = lowercase(pre)
            if prelc ∈ gamskws
                push!(lexed, Keyword(prelc))
                pre = prelc = ""
            end
            if (c == '(' || c == '[')
                args = AbstractLex[]
                pos = lex!(io, buf, args, pos, closerdict[c])
                if c == '(' && prelc ∈ funcnames
                    push!(lexed, GCall(prelc, args))
                elseif isempty(pre)
                    push!(lexed, Parens(args))
                else
                    push!(lexed, GArray(pre, (filter(x->x!=GText(","), args)...,)))
                end
            else
                write(buf, c)
                pos = lex_delim(buf, io, closerdict[c], pos)
                tag_delim!(lexed, pre * String(take!(buf)))
            end
        elseif isspace(c)
            if c == '\n'
                pos = 1
            end
            if position(buf) > 0
                tag!(lexed, String(take!(buf)))
                if lexed[end] == Keyword("table")
                    # Tables are space-sensitive, lex them seperately
                    pos = lex_table!(buf, io, lexed, pos)
                end
            end
        elseif c == ';'
            tag!(lexed, String(take!(buf)))
            push!(lexed, StatementEnd())
        elseif c == ','
            tag!(lexed, String(take!(buf)))
            push!(lexed, GText(","))
        elseif innumber && c ∈ ('+', '-', '.')
            write(buf, c)
        elseif c == '=' && prevbool
            write(buf, c)  # ensure <= doesn't get split into GText("<"), GText("=")
        elseif c ∈ ('+', '-', '*', '/', '^', '=', '.')
            # Test for =E= and similar relations
            if inrelation
                write(buf, c)
                if c == '='
                    tag!(lexed, lowercase(String(take!(buf))))
                end
                inrelation = false
                continue
            end
            if c == '=' && peek(io) ∈ ('e', 'E', 'g', 'G', 'l', 'L')
                tag!(lexed, String(take!(buf)))
                write(buf, c)
                inrelation = true
                continue
            end
            tag!(lexed, String(take!(buf)))
            if c ∈ ('*', '.') && !eof(io)
                # Translate "x**y" into "x^y"
                c2 = Char(peek(io))
                if c2 == c == '*'
                    read(io, Char)
                    push!(lexed, GText("^"))
                elseif c2 == c == '.'
                    read(io, Char)
                    push!(lexed, Dots(".."))
                else
                    push!(lexed, c == '.' ? Dots(".") : GText(string(c)))
                end
            else
                if c == '.'
                    push!(lexed, Dots("."))
                else
                    push!(lexed, GText(string(c)))
                end
            end
        else
            if c ∈ ('<', '>')
                prevbool = true
            end
            write(buf, c)
        end
    end
    if position(buf) > 0
        tag!(lexed, String(take!(buf)))
    end
    # NOTE THIS HACK: Fixing double Dots(".") and relations issues
    i = 1;
    eq = GText("=")
    while i < length(lexed)
        if lexed[i] == lexed[i+1] == Dots(".")
            splice!(lexed, i)
            lexed[i] = Dots("..")
        elseif i < length(lexed)-2 && lexed[i] == lexed[i+2] == eq
            lexed[i] = GText(string("=", lowercase(lexed[i+1].text), "="))
            splice!(lexed, [i+1, i+2])
        else i +=1;
        end
    end
    return pos
end

function lex_delim(buf, io, cmatch, pos)
    local c
    while !eof(io)
        c = read(io, Char)
        pos += 1
        write(buf, c)
        c == cmatch && break
        if cmatch ∉ ('"', '\'')  # text in quotes should not be interpreted
            if c ∈ openers       # use recursion to ensure balanced parens
                pos = lex_delim(buf, io, closerdict[c], pos)
                continue
            end
            c ∈ closers && break
        end
    end
    c == cmatch && return pos
    c ∈ closers && error("closing got $c, wanted $cmatch. Rest of line:\n", readline(io))
    error("never encountered $cmatch before end of file")
end

matcharray(str) = match(r"^([a-zA-Z][a-zA-Z0-9]*)\((.*)\)$", str)

function tag_delim!(lexed, str)
    m = matcharray(str)
    if m != nothing
        return push!(lexed, GArray(m.captures[1], (strip.(split(m.captures[2], ','))...,)))
    end
    f, l = str[1], str[end]
    if f == l == '"'
        return push!(lexed, GText(str[2:end-1]))
    elseif f == l == '\''
        substr = str[2:end-1]
        if isnumberstring(substr)
            val = GNumber(parse(isintegerstring(substr) ? Int : Float64, substr))
        else
            val = GText(substr)
        end
        return push!(lexed, val)
    elseif f == l == '/'
        return push!(lexed, Slashed(strip(str[2:end-1])))
    elseif l == ')' || l == ']' # don't check f because this could be a function call or arrayref
        return push!(lexed, GText(str))
    end
    error("delim ", str, " not handled")
end

function tag!(lexed, str)
    isempty(str) && return nothing
    if isnumberstring(str)
        return push!(lexed, isintegerstring(str) ? GNumber{Int}(parse(Int, str)) : GNumber{Float64}(parse(Float64, str)))
    end
    lc = lowercase(str)
    if lc ∈ gamskws
        return push!(lexed, Keyword(get(stdkws, lc, lc)))
    end
    push!(lexed, GText(str))
end

function lex_table!(buf, io, lexed, pos)
    local array
    c = read(io, Char)
    pos += 1
    while !eof(io) && (c == ' ' || c == '\t')
        c = read(io, Char)
        pos += 1
    end
    while true
        if isspace(c)
            str = String(take!(buf))
            m = matcharray(str)
            array = GArray(m.captures[1], (GText.(strip.(split(m.captures[2], ',')))...,))
            break
        end
        write(buf, c)
        if c ∈ openers
            pos = lex_delim(buf, io, closerdict[c], pos)
        else
            @assert(isnumeric(c) || isletter(c))
        end
        eof(io) && error("table should not have ended yet")
        c = read(io, Char)
        pos += 1
    end
    if c != '\n'
        str = readline(io)
        if !isempty(str)
            push!(lexed, GText(str))
        end
    end
    body, term = readupto(io, ';')
    push!(lexed, Table(array, body))
    return pos
end

function lex(file::AbstractString)
    """ Parses a .gms file into Array{AbstractLex, 1}. """
    return open(file) do io
        lex(io)
    end
end

function isnumberstring(str)
    m = match(r"^[+-]?[0-9]+\.?[0-9]*([eE][+-]?[0-9]*)?$", str)
    return m !== nothing
end

isintegerstring(str) = match(r"[\.eE]", str) === nothing


text:
"""
    simulate linear feedback policy
"""
function simulate(
        model,
        policy, K,
        z_nom, u_nom,
        Q, R,
        T_sim, Δt,
        z0,
        w;
        _norm = 2,
        xl = -Inf * ones(length(z_nom[1])),
        xu = Inf * ones(length(z_nom[1])),
        ul = -Inf * ones(length(u_nom[1])),
        uu = Inf * ones(length(u_nom[1])),
        u_idx = 1:model.m)

    T = length(z_nom)
    times = [(t - 1) * Δt for t = 1:T-1]
    tf = Δt * (T-1)
    t_sim = range(0, stop = tf, length = T_sim)
    t_ctrl = range(0, stop = tf, length = T)
    dt_sim = tf / (T_sim - 1)

    z_rollout = [z0]
    u_rollout = []

    J = 0.0
    Jx = 0.0
    Ju = 0.0

    A_state = Array(hcat(z_nom...))
    A_ctrl = Array(hcat(u_nom...))

    for tt = 1:T_sim-1
        t = t_sim[tt]
        k = searchsortedlast(times, t)
        z = z_rollout[end]

        z_cubic = Array(zero(z_nom[1]))
        for i = 1:length(z_nom[1])
            interp_cubic = CubicSplineInterpolation(t_ctrl, A_state[i,:])
            z_cubic[i] = interp_cubic(t)
        end

        u = eval_policy(policy, K[k], z, z_cubic, u_nom[k])

        # clip controls
        u = max.(u, ul[u_idx])
        u = min.(u, uu[u_idx])

        push!(z_rollout, fd(model, z, u, w[:, tt], dt_sim, tt))
        push!(u_rollout, u)

        if _norm == 2
            J += (state_output(model, z_rollout[end]) - z_cubic)' * Q[k + 1] * (state_output(model, z_rollout[end]) - z_cubic)
            J += (u_rollout[end] - u_nom[k][u_idx])' * R[k][u_idx, u_idx] * (u_rollout[end] - u_nom[k][u_idx])
            Jx += (state_output(model, z_rollout[end]) - z_cubic)' * Q[k+1] * (state_output(model, z_rollout[end]) - z_cubic)
            Ju += (u_rollout[end] - u_nom[k][u_idx])' * R[k][u_idx, u_idx] * (u_rollout[end] - u_nom[k][u_idx])
        else
            J += norm(sqrt(Q[k + 1]) * (state_output(model, z_rollout[end]) - z_cubic), _norm)
            J += norm(sqrt(R[k][u_idx, u_idx]) * (u - u_nom[k][u_idx]), _norm)
            Jx += norm(sqrt(Q[k + 1]) * (state_output(model, z_rollout[end]) - z_cubic), _norm)
            Ju += norm(sqrt(R[k][u_idx, u_idx]) * (u - u_nom[k][u_idx]), _norm)
        end
    end

    return z_rollout, u_rollout, J / (T_sim - 1), Jx / (T_sim - 1), Ju / (T_sim - 1)
end


text:
module matching_simulation

immutable Matching
    apply_prefs::Array
    accept_prefs::Array
end

function Base.call(f::Matching, typ::ASCIIString)
    apply_prefs = f.apply_prefs
    accept_prefs = f.accept_prefs

    if typ == "DA1"
        apply_matched, accept_matched = DA1(apply_prefs, accept_prefs)
    elseif typ == "DA2"
        emp()
    elseif typ == "DA2"
        emp()
    else
        print("Please input 'DA1' or 'DA2' to 'typ'")
    end
    return apply_matched, accept_matched
end

function DA1(apply_prefs::Array, accept_prefs::Array)
    da = zeros(Int64, length(accept_prefs[1,:]))
    rem = [i for i in 1:length(apply_prefs[1,:])]
    depth = ones(Int64, length(accept_prefs[:,1])-1)

    while length(rem) != 0
        apply = rem[end]
        d = depth[apply]
        if d == length(accept_prefs[:,1])
            pop!(rem)
        end
        applied = apply_prefs[d, apply]
        if applied == 0
            pop!(rem)
        else
            if findfirst(accept_prefs[:,applied], apply) < findfirst(accept_prefs[:,applied], da[applied])
                pop!(rem)
                if da[applied] != 0
                    push!(rem, da[applied])
                    depth[da[applied]] += 1
                end
                da[applied] = apply
            else
                depth[apply] += 1
            end
        end
    end

    accept_matched = da
    apply_matched = zeros(Int64, length(apply_prefs[1,:]))

    for i in 1:length(accept_matched)
        if accept_matched[i] != 0
            apply_matched[accept_matched[i]] = i
        end
    end
    return apply_matched, accept_matched
end

function g()
    return 1
end

end

max_stars_repo_name: NlGG/Economics

max_issues_repo_name: NlGG/Economics

max_forks_repo_name: NlGG/Economics


text:
function make_scores(cur_board::AbstractBoard, is_mobile::Bool)
  rendered_entries = []

  for (cur_index, cur_entry) in enumerate(cur_board.entries)
    rendered_entry = dom"tr"(
      dom"td"(
        "$(cur_index)",
      ),
      dom"td"(
        "$(rpad(cur_entry.name, 8, "\u00A0"))"[1:8]
      ),
      dom"td"(
        "-",
        attributes=Dict(:class => "hidden-xs")
      ),
      dom"td"(
        "$(lpad(cur_entry.score, 8, "0"))"[1:8]
      )
    )

    push!(rendered_entries, rendered_entry)
  end

  cur_board = dom"table"(
    rendered_entries...,
    attributes=Dict(:class => "cs-score-board")
  )

  if !is_mobile
    cur_board = dom"div"(
      cur_board
    )
  end

  cur_contents = [
    dom"div"(
      dom"h1"(
        "High Scores",
        attributes=Dict(:class => "hidden-xs")
      ),
      dom"h2"(
        "High Scores",
        attributes=Dict(:class => "visible-xs")
      ),
      attributes=Dict(:class => is_mobile ? "" : "col-md-4")
    ),
    dom"div"(
      attributes=Dict(:class => is_mobile ? "" : "col-md-1")
    ),
    dom"div"(
      cur_board,
      attributes=Dict(:class => is_mobile ? "" : "col-md-7")
    )
  ]

  if is_mobile
    cur_contents = [
      dom"div"(
        cur_contents...,
        attributes=Dict(:class => "cs-mobile-scores")
      )
    ]
  end

  cur_row = dom"div"(
    cur_contents...,
    attributes=Dict(:class => "row $(is_mobile ? "visible-sm visible-xs cs-mobile" : "hidden-sm hidden-xs")")
  )

  cur_row
end


text:
@testset "15 Puzzle" begin

import Base
using DataStructures

struct State
  table::Array{Int8,2}
end

Base.copy(s::State) = State(copy(s.table))
Base.:(==)(s1::State, s2::State) = s1.table == s2.table

function hash(s::State)
  # the entire board state can be compressed in a single 64 bit UInt
  h = UInt64(0)
  # by column
  for j in 1:4, i in 1:4
    h += s.table[i, j]
    h <<= 4
  end
  return h
end

GOALSTATE = State([
  1  2  3  4;
  5  6  7  8;
  9 10 11 12;
  13 14 15 0
])

UP = CartesianIndex(-1, 0)
DOWN = CartesianIndex(1, 0)
LEFT = CartesianIndex(0, -1)
RIGHT = CartesianIndex(0, 1)

DIRECTIONS = [UP, DOWN, LEFT, RIGHT]
FINALSTATEINDEXES = [findfirst(x -> x == i, GOALSTATE.table) for i in 1:15]

emptyposition(s::State) = findfirst(x -> x == 0, s.table)

isvalidindex(c) = (0 < c[1] < 5) && (0 < c[2] < 5)

availablemoves(e::CartesianIndex) = [direction for direction in DIRECTIONS if isvalidindex(e + direction)]
availablemoves(s::State) = s |> emptyposition |> availablemoves
nextstates(s::State) = map(x -> move(s, x), availablemoves(s))

function move(s::State, direction::CartesianIndex)
  next = copy(s.table)
  cur = emptyposition(s)
  emptydest = cur + direction
  next[cur], next[emptydest] = next[emptydest], next[cur]
  return State(next)
end

manhattan(c1::CartesianIndex, c2::CartesianIndex) = c1 - c2 |> x -> x.I .|> abs |> sum

heuristicmanhattan(s::State) = sum(manhattan(findfirst(x -> x == i, s.table), FINALSTATEINDEXES[i]) for i in 1:15)

function countswaps(s::State)
  acc = 0
  for j in 1:3, i in 1:3
    # horizontal swap
    if (s.table[i, j] == GOALSTATE.table[i + 1, j]) && (s.table[i + 1, j] == GOALSTATE.table[i, j])
      acc += 1
    end
    #vertical swap
    if (s.table[i, j] == GOALSTATE.table[i, j + 1]) && (s.table[i, j + 1] == GOALSTATE.table[i, j])
      acc += 1
    end
  end
  return acc
end

function heuristicmanhattanandswaps(s::State)
  hm = heuristicmanhattan(s)
  swaps = countswaps(s)
  return hm + 2swaps
end

# the goal is fixed in this problem
heuristic(s, goal) = heuristicmanhattanandswaps(s)

@testset "Test goal" begin
  res = astar(nextstates, GOALSTATE, GOALSTATE, heuristic=heuristic)
  @test res.status == :success
  @test map(x -> x.table, res.path) == [
    Int8[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 0]
  ]
  @test res.cost == 0
end

@testset "Test state 1" begin
  start = State([
    1   2   3   4;
    5   7  11   8;
    9   6   0  12;
   13  10  14  15
  ])

  res = astar(nextstates, start, GOALSTATE, heuristic=heuristic)
  @test res.status == :success
  @test map(x -> x.table, res.path) == [
    Int8[1 2 3 4; 5 7 11 8; 9 6 0 12; 13 10 14 15],
    Int8[1 2 3 4; 5 7 0 8; 9 6 11 12; 13 10 14 15],
    Int8[1 2 3 4; 5 0 7 8; 9 6 11 12; 13 10 14 15],
    Int8[1 2 3 4; 5 6 7 8; 9 0 11 12; 13 10 14 15],
    Int8[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 0 14 15],
    Int8[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 0 15],
    Int8[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 0]
  ]
  @test res.cost == 6
end

@testset "Test maxcost argument" begin
  start = State([
    1   2   3   4;
    5   7  11   8;
    9   6   0  12;
   13  10  14  15
  ])

  res = astar(nextstates, start, GOALSTATE, heuristic=heuristic, maxcost=5)
  @test res.status == :nopath
  @test map(x -> x.table, res.path) == [
    Int8[1 2 3 4; 5 7 11 8; 9 6 0 12; 13 10 14 15],
    Int8[1 2 3 4; 5 7 0 8; 9 6 11 12; 13 10 14 15],
    Int8[1 2 3 4; 5 0 7 8; 9 6 11 12; 13 10 14 15],
    Int8[1 2 3 4; 5 6 7 8; 9 0 11 12; 13 10 14 15],
    Int8[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 0 14 15],
    Int8[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 0 15]
  ]
  @test res.cost == 5
end

end


text:
using JuMP, EAGO

                     m = Model()

                     EAGO.register_eago_operators!(m)

                     @variable(m, -1 <= x[i=1:3] <= 1)

                     @variable(m, -37.14009556780016 <= q <= 48.365016418887166)

                     add_NL_constraint(m, :(softplus(0.24902435571631587 + -0.02312729681609138*softplus(-0.46870412880299783 + 0.1700038462852933*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.44738483682757924*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.5112184633122538*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.17714072857386487*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9529511507705823*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.5418072450548097*softplus(0.5019192867350393 + 0.2997113620022893*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.5721484689942011*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.4073624372378828*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + -0.07630883358086304*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.3103910161241421*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.9789829058919253*softplus(-0.652369829242545 + -0.998242239199818*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.041041478696029365*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.4348093907280841*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.7773011179238121*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7537599741964742*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.13446361488468206*softplus(-0.40141742757451926 + 0.7289092837988314*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + 0.16077592157977172*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.6378484913808045*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9326240829024273*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9638370498324353*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.3069132295129271*softplus(0.17680227033398044 + 0.8293506390949057*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.09356004658531791*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.16358082594109513*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9029174038261001*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7318887196209252*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3])))) + softplus(-0.017552134571214406 + -0.41004797399293524*softplus(-0.46870412880299783 + 0.1700038462852933*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.44738483682757924*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.5112184633122538*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.17714072857386487*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9529511507705823*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.47019448283071075*softplus(0.5019192867350393 + 0.2997113620022893*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.5721484689942011*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.4073624372378828*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + -0.07630883358086304*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.3103910161241421*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.35795867979675133*softplus(-0.652369829242545 + -0.998242239199818*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.041041478696029365*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.4348093907280841*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.7773011179238121*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7537599741964742*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.44715886126593407*softplus(-0.40141742757451926 + 0.7289092837988314*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + 0.16077592157977172*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.6378484913808045*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9326240829024273*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9638370498324353*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.22638221307132733*softplus(0.17680227033398044 + 0.8293506390949057*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.09356004658531791*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.16358082594109513*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9029174038261001*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7318887196209252*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3])))) + softplus(-0.4029592795849051 + -0.6197606146665708*softplus(-0.46870412880299783 + 0.1700038462852933*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.44738483682757924*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.5112184633122538*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.17714072857386487*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9529511507705823*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.8464388391219866*softplus(0.5019192867350393 + 0.2997113620022893*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.5721484689942011*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.4073624372378828*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + -0.07630883358086304*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.3103910161241421*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.6262663939568953*softplus(-0.652369829242545 + -0.998242239199818*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.041041478696029365*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.4348093907280841*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.7773011179238121*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7537599741964742*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.06090941414894013*softplus(-0.40141742757451926 + 0.7289092837988314*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + 0.16077592157977172*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.6378484913808045*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9326240829024273*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9638370498324353*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.6699649674430836*softplus(0.17680227033398044 + 0.8293506390949057*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.09356004658531791*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.16358082594109513*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9029174038261001*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7318887196209252*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3])))) + softplus(0.6579858782021573 + -0.21911632564812766*softplus(-0.46870412880299783 + 0.1700038462852933*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.44738483682757924*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.5112184633122538*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.17714072857386487*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9529511507705823*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.7292607357990395*softplus(0.5019192867350393 + 0.2997113620022893*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.5721484689942011*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.4073624372378828*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + -0.07630883358086304*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.3103910161241421*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.23882038501163416*softplus(-0.652369829242545 + -0.998242239199818*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.041041478696029365*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.4348093907280841*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.7773011179238121*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7537599741964742*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.6855544316424598*softplus(-0.40141742757451926 + 0.7289092837988314*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + 0.16077592157977172*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.6378484913808045*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9326240829024273*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9638370498324353*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.2868272308560216*softplus(0.17680227033398044 + 0.8293506390949057*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.09356004658531791*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.16358082594109513*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9029174038261001*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7318887196209252*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3])))) + softplus(-0.10147744083820065 + -0.7175110849623545*softplus(-0.46870412880299783 + 0.1700038462852933*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.44738483682757924*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.5112184633122538*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.17714072857386487*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9529511507705823*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.9583278917345623*softplus(0.5019192867350393 + 0.2997113620022893*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.5721484689942011*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.4073624372378828*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + -0.07630883358086304*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.3103910161241421*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.7313133539296679*softplus(-0.652369829242545 + -0.998242239199818*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.041041478696029365*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.4348093907280841*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.7773011179238121*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7537599741964742*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + 0.008349755673621395*softplus(-0.40141742757451926 + 0.7289092837988314*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + 0.16077592157977172*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + 0.6378484913808045*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9326240829024273*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.9638370498324353*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3]))) + -0.13663535205769417*softplus(0.17680227033398044 + 0.8293506390949057*softplus(-0.07115429328131828 + -0.06282459839957566*$(x[1]) + 0.4662931752239805*$(x[2]) + -0.47993632100881634*$(x[3])) + -0.09356004658531791*softplus(-0.42367185224307535 + 0.829552620135646*$(x[1]) + -0.7267035823407642*$(x[2]) + 0.6637528273154039*$(x[3])) + -0.16358082594109513*softplus(0.6408115001973855 + -0.9252249601617657*$(x[1]) + 0.6902522661180681*$(x[2]) + 0.6531628217744303*$(x[3])) + 0.9029174038261001*softplus(0.3778204937005931 + 0.21019890124389518*$(x[1]) + -0.5904566970281198*$(x[2]) + -0.06968695857557394*$(x[3])) + 0.7318887196209252*softplus(0.7716365722893217 + 0.12636272698834716*$(x[1]) + 0.15699019471654063*$(x[2]) + -0.5007466869555728*$(x[3])))) - $q <= 0.0))

                     @objective(m, Min, q)

                     return m

max_stars_repo_name: PSORLab/RSActivationFunctions

max_issues_repo_name: PSORLab/RSActivationFunctions

max_forks_repo_name: PSORLab/RSActivationFunctions


text:
using LowRankModels, FullRankModels, Compat

srand(1)
# parameters
m, n, k = 30, 30, 3
lambda = 1

# generate data
X0 = randn(m,k)
Y0 = randn(k,n)
A = X0*Y0 + .1*randn(m,n)
P = sparse(float(randn(m,n) .>= .5)) # observed entries

I,J = findn(P) # observed indices (vectors)
@compat obs = Tuple{Int,Int}[(I[a],J[a]) for a = 1:length(I)]
observed_features, observed_examples = sort_observations(obs,size(P)...)

losses = fill(QuadLoss(1), n)
reg = TraceNormReg(lambda)
gfrm = GFRM(A, losses, reg, k, observed_features, observed_examples,
	zeros(m,n), zeros(m+n,m+n))

fit!(gfrm, PrismaParams(PrismaStepsize(Inf), 200, 1))

# compare with SDP solver
using Convex

U = Variable(m,n)
W = Variable(m+n, m+n)

obj = sumsquares(P.*(U-A)) + lambda*nuclearnorm(U)
p = minimize(obj)
solve!(p)

@show Convex.evaluate(lambda*nuclearnorm(U))
println("mean square error is ", vecnorm(U.value - gfrm.U) / vecnorm(U.value))
U.value = gfrm.U
println("objective of Convex problem evaluated at prisma solution is $(Convex.evaluate(obj))
")


text:
using NeuralOperators
using Test
using Flux

@testset "NeuralOperators.jl" begin
    include("data.jl")
    include("fourier.jl")
    include("model.jl")
end


text:
using MyExample
using Test

@testset "MyExample.jl" begin
    @test sum_values(2, 1) == 3
    @test sum_values(2, 3) == 5
end


text:
using DrWatson
quickactivate(@__DIR__)
@show projectdir()
##
using CovidSurvey
using CSV
using DataFrames
using Dates
# using RCall
using Turing
using StatsPlots
using Plots.PlotMeasures
using LaTeXStrings
using Random
using BSON
## ===========================================================================
# load data and model
# ============================================================================
# fdir = projectdir("reports/2021-04-27/")
# fname = "cases=2020-06-01_chains=10_hospit=true_model=v2_observ=2020-12-16_seed=10000_sero=true_steps=2000_warmup=1000"
# fdir = projectdir("reports/2021-04-28/")
# fname = "cases=2020-06-01_chains=10_hospit=true_model=v2_observ=2021-02-07_seed=10000_sero=true_steps=2000_warmup=1000"
# fdir = projectdir("reports/constant_iar/")
# fname = "cases=2020-06-01_chains=10_hospit=true_model=const-iar_observ=2021-02-06_sero=true_steps=2000_warmup=1000"
# fdir = projectdir("reports/const_iar_cases_only/")
# fname = "cases=2020-06-01_chains=5_hospit=true_model=const-iar_observ=2021-02-06_sero=true_steps=5000_warmup=2000"
# fdir = projectdir("reports/test_single_observation/")
# fname = "cases=2020-06-01_chains=10_hospit=true_model=cases_observ=2021-03-25_sero=true_steps=5000_warmup=2000"
# fname = "cases=2020-06-01_chains=10_hospit=true_model=deaths_observ=2021-03-25_sero=true_steps=2000_warmup=1000"
# fdir = projectdir("reports/test_single_observation_BrauersParameters//")
# fname = "cases=2020-06-01_chains=10_hospit=true_model=cases_observ=2021-03-25_sero=true_steps=2000_warmup=1000"
# fname = "cases=2020-06-01_chains=10_hospit=true_model=deaths_observ=2021-03-25_sero=true_steps=2000_warmup=1000"
# fname = "cases=2020-06-01_chains=10_hospit=true_model=hospit_observ=2021-03-25_sero=true_steps=2000_warmup=1000"
# fdir = projectdir("reports/test_single_observation_BrauersParameters//")
# fname = "cases=2020-06-01_chains=10_hospit=true_model=cases_observ=2021-02-06_sero=true_steps=2000_warmup=1000"
fdir = projectdir("reports/2021-05-12/")
fname = "cases=2020-06-01_chains=10_hospit=true_model=v2_observ=2021-01-02_sero=true_steps=2000_warmup=1000"
PREFIX = "SHIFT-14d-INITIAL-SERO-"
## read chain
chain = read(fdir*PREFIX*"CHAIN_"*fname*".jls", Chains) #chains_
# chain = read(fdir*"chains_"*fname*".jls", Chains) #chains_
chain = chain[1:10:end,:,:]
diagnostics = gelmandiag(chain)
## read params
_, ps, _ = parse_savename(fname, parsetypes = (Int, Float64, Bool))
!("preds" in keys(ps)) && (ps["preds"] = nothing)
ps = NamedTuple{Tuple(Symbol.(keys(ps)))}(values(ps))
## read data
data = National.load_data(
    ps.observ,
    ps.preds |> CovidSurvey.parse_predictors,
    ps.cases,
    ps.hospit,
    ps.sero;
    update=false,
    fname_covariates = projectdir("data/Rt_SSI.csv"),#normpath( homedir(), "data/covidsurvey/smoothed_contacts.csv" ),
    iar_step = 2,
    shift_covariates = 2)
turing_data = data.turing_data;
## ===========================================================================
# plot results
# ============================================================================
# make predictions
# m_pred = National.model_v2(turing_data..., true; link=KLogistic(3.), invlink=KLogit(3.) )
m_pred = National.model_hospit(turing_data..., true; link=KLogistic(3.), invlink=KLogit(3.) )
gq = Turing.generated_quantities(m_pred, chain)
generated_posterior = vectup2tupvec( reshape(gq, length(gq)) );
observed_daily_cases, expected_daily_deaths, expected_daily_hospit, Rt, expected_seropositive, iar = generated_posterior;
##
gq = BSON.load(fdir*"SHIFT-14d-INITIAL-SERO-GENERATED-QUANTITIES_cases=2020-06-01_chains=10_hospit=true_model=v2_observ=2021-01-02_sero=true_steps=2000_warmup=1000.bson")
for key in keys(gq)
   @eval $key = gq[$(QuoteNode(key))]
end
#---------------------------------------------------------------------------
# plot posterior predictions
plotlyjs()
p = National.plot_results(data.dates, observed_daily_cases, expected_daily_deaths, expected_daily_hospit, Rt, expected_seropositive, iar);
savefig(p, "/home/and/tmp/figures/POSTERIOR_"*fname*".html")
# savefig(p, projectdir("figures/tmp", fname*".png") )
run(`firefox $("/home/and/tmp/figures/POSTERIOR_"*fname*".html")`, wait=false)
#---------------------------------------------------------------------------
# plot chain statistics
plotlyjs()
n = filter( x->!occursin(r"(?<!α)\[", x), String.(names(chain)))
p = plot(chain[n]);
savefig(p, "/home/and/tmp/figures/CHAINS_"*fname*".html" )
run(`firefox  $("/home/and/tmp/figures/CHAINS_"*fname*".html")`, wait=false)
#---------------------------------------------------------------------------
# plot more chain statistics (meanplot)
using MCMCChains
plotlyjs()
p = meanplot(chain[n]);
savefig(p, projectdir("/home/and/tmp/figures/MEANPLOT_", fname*".html") )
run(`firefox $(projectdir("/home/and/tmp/figures/MEANPLOT_", fname*".html"))`, wait=false)
## ===========================================================================
# evaluate effect size
# ============================================================================
# posterior predictions
pgfplotsx()
scalefontsizes(2.)
p = let
    ocases  = hcat(observed_daily_cases...)#hcat([observed_daily_cases[t] for t = 1:data.num_total]...)
    ehospit  = hcat(expected_daily_hospit...)

    tickdates = data.dates[1]:Month(1):data.dates[end]
    p3 = plot(; legend = :outertopright, ylims=(0,7000), bottom_margin=3mm)
    National.plot_cases_posterior!(p3, data, ocases)
    p4 = plot(; legend = :outertopright, ylims=(0,500), top_margin=3mm)
    National.plot_hospit_posterior!(p4, data, ehospit)
    plot(p3, p4, layout=(2,1), sharex=true, link=:x, size=(1000, 700)) #, size=(1500, 1000)
end
##
savefig(p, projectdir("figures/", "posterior_check.png") )
#---------------------------------------------------------------------------
# evaluate Rt
rt = let
    ib = findfirst(==(Date("2020-05-15")), data.dates)
    ie = findfirst(==(Date(ps.observ)), data.dates)
    Rt_array = hcat(Rt...)[ib:ie,:]

    qs = [quantile(v, [0.025, 0.25, 0.5, 0.75, 0.975]) for v in eachrow(Rt_array)]
    llq, lq, mq, uq, uuq = (eachrow(hcat(qs...))..., )

    date = data.dates[ib:ie]
    DataFrame((;date, llq, lq, mq, uq, uuq))
end
plot(rt.date, rt.mq, ribbon=(rt.mq - rt.llq, rt.uuq - rt.mq), linewidth=0, label="95% quantiles")
plot!(rt.date, rt.mq, ribbon=(rt.mq - rt.lq, rt.uq - rt.mq), linewidth=2, label="50% quantiles", hover=rt.mq)
##
save(projectdir(fdir, "Rt_"*fname*".csv"), rt)
##
# Rts = DataFrame(:date=>rt.date)
# Rt_SSI = National.readsurvey("/home/and/data/covidsurvey/contacts.csv")
# Rt_SSI = Rt_SSI[:,[:Rt,:date]]
# names(Rts)
# Rts = leftjoin(
#     Rts,
#     DataFrame(:date=>Rt_SSI.date, Symbol("Rt-SSI")=>Rt_SSI.Rt),
#     on=:date
# )
# plot(Rts.date, Rts[:,"Rt-observ-2021-02-07"])
# plot!(Rts.date, Rts[:,"Rt-observ-2020-12-16"])
# plot!(Rts.date, Rts[:,"Rt-observ-2021-02-06-constiar"])
# plot!(Rts.date, Rts[:,"Rt-SSI"])
# Rts[!,Symbol("Rt-observ-2021-02-07")] = rt.mq
# sort!(Rts, :date)
CSV.write(projectdir("data/", "Rt.csv"), df3)
df3 = leftjoin(
    df,df2,on=:date
)
df = CSV.File(projectdir("data/", "Rt.csv"))|>DataFrame
df2 = DataFrame("date"=>df.date + Day(9), "Rt-SSI-shifted"=>df[:,"Rt-SSI"])
df.Rt-SSI
let
    ns = names(df3)
    ns = filter(x->x!==("date"), ns)
    p = plot()
    for n in ns
        plot!(df.date, df3[:,n])
    end
    p
end

df3[:,"Rt-SSI-shifted"]
sort!(df3, :date)

max_stars_repo_name: andreaskoher/CovidSurvey

max_issues_repo_name: andreaskoher/CovidSurvey

max_forks_repo_name: andreaskoher/CovidSurvey


text:
module TestScikitLearn

import MLJModels, ScikitLearn
using MLJBase, Tables, Test, LinearAlgebra, Random, MLJModels.ScikitLearn_, CategoricalArrays, RDatasets
include("../testutils.jl")

include("svm.jl")
include("linear-regressors.jl")
include("linear-classifiers.jl")
include("gaussian-process.jl")
include("ensemble.jl")
include("discriminant-analysis.jl")
include("misc.jl")

end
true


text:
module MusicXML

using LightXML

@enum STEP C=1 D=3 E=5 F=6 G=8 A=10 B=12
@enum TYPE whole=1 half=2 quarter=3 eighth=4 sixteenth=5 thirtysecond=6

type Pitch
  step::STEP
  octave::Int
  alter::Int
end

type Note
  pitch::Pitch
  duration::Int
  voice::Int
  notetype::TYPE
  isrest::Bool
end

type Measure
  notes::Array{Note, 1}
end

type Part
  measures::Array{Measure, 1}
end

type MXMLTree
  parts::Array{Part, 1}
end

function parseMXMLFile(fname::String)
  xmlfile = parse_file(fname)
  return parseMXMLTree(root(xmlfile))
end

function parseMXMLTree(root::XMLElement)
  parts = Part[]
  for c1 in child_elements(root)
    if LightXML.name(c1) == "part"
      measures = Measure[]
      for c2 in child_elements(c1)
        if LightXML.name(c2) == "measure"
          notes = Note[]
          for c3 in child_elements(c2)
            if LightXML.name(c3) == "note"
              c4 = find_element(c3, "pitch")
              step = A
              octave = 0
              alter = 0
              isrest = true
              if typeof(c4) != Void
                step = eval(parse(content(find_element(c4, "step"))))
                octave = parse(content(find_element(c4, "octave")))
                alternode = find_element(c4, "alter")
                alter = typeof(alternode) == Void ? 0 : parse(content(alternode))
                isrest = false
              end
              p = Pitch(step, octave, alter)
              duration = parse(content(find_element(c3, "duration")))
              voice = parse(content(find_element(c3, "voice")))
              notetype = eval(parse(content(find_element(c3, "type"))))

              push!(notes, Note(p, duration, voice, notetype, isrest))
            end
          end
          push!(measures, Measure(notes))
        end
      end
      push!(parts, Part(measures))
    end
  end
  return MXMLTree(parts)
end

end


text:
module DataConvenience

import WeakRefStrings:StringVector
using DataFrames: AbstractDataFrame, DataFrame, rename, dropmissing
using CategoricalArrays
using Statistics
using Missings: nonmissingtype

import Statistics:cor
export cor, dfcor, @replicate, StringVector

include("cate-arrays.jl")
include("CCA.jl")
include("janitor.jl")
include("dfcor.jl")
include("onehot.jl")
include("create-missing.jl")
include("read-csv-in-chunks.jl")
include("fsort-dataframes.jl")
include("fast-missing-count.jl")
include("sample.jl")
include("nest.jl")
include("shortstringify.jl")

# head(df::AbstractDataFrame) = first(df, 10)
#
# tail(df::AbstractDataFrame) = last(df, 10)
"""
    @replicate n expr

Replicate the expression `n` times

## Example
```julia
using DataConvenience, Random
@replicate 10 randstring(8) # returns 10 random length 8 strings
```
"""
macro replicate(n, expr)
    :([$(esc(expr)) for i=1:$(esc(n))])
end

end # module

max_stars_repo_name: xiaodaigh/DataConvenience

max_issues_repo_name: xiaodaigh/DataConvenience

max_forks_repo_name: xiaodaigh/DataConvenience