Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using DelimitedFiles;
- lcat(x...) = cat(x...; dims=1)
- function tofloat(x)
- if x isa AbstractString
- x = replace(x, ","=>".");
- return parse(Float64, x)
- end
- return x
- end
- # noms_nuls = [ "BLANCS", "NULS", "ABSTENTION" ]
- noms_nuls = [];
- noms_x = lcat([ "MACRON", "LE PEN", "FILLON", "MÉLENCHON", "HAMON",
- "DUPONT-AIGNAN", "LASSALLE", "POUTOU", "ASSELINEAU", "ARTHAUD", "CHEMINADE" ],
- noms_nuls)
- tmp_y = readdlm("euro1.csv", ';', quotes=true)
- noms_y = lcat([ tmp_eur[2,13+7j] for j=1:34 ], noms_nuls)
- function read_results(f,c,noms=false)
- E0 = readdlm(f,c,quotes=true);
- m = div(size(E0,2)-18,7)
- E1 = fill(0.,size(E0,1)-1,m+2+length(noms_nuls));
- for i=1:size(E1,1)
- l = i+1
- k = E0[l,1]
- if k isa AbstractString
- if k[1] == '2'; k = 20; end # Corse
- if k[1] == 'Z'; k = 97; end # DOM-COM
- end
- E1[i,1] = 1000*k+E0[l,3];
- E1[i,2] = E0[l,16];
- if length(noms_nuls) >= 1 E1[i,m+3] = E0[l,10]; end
- if length(noms_nuls) >= 2 E1[i,m+4] = E0[l,13]; end
- if length(noms_nuls) >= 3 E1[i,m+5] = E0[l,6]; end
- for j=1:m
- y = tofloat(E0[l,16+7j]) # 18+7j = %, 16+7j = exprimés
- if noms != false
- for k = 1:size(noms, 1)
- if E0[l,14+7*j] == noms[k]
- # print("$i, $j => $k $(noms[k])\n")
- E1[i,k+2] = y
- end
- end
- else
- E1[i,j+2] = y
- end
- end
- end
- return E1
- end
- function read_x() read_results("prez.csv",',', noms_x) end
- function read_y() read_results("euro.csv",';') end
- function merge_mat(A,B)
- M = Pair{Int,Int}[]
- sA = size(A,1)
- sB = size(B,1)
- j = 1
- for i = 1:sA
- a = A[i,1]
- # print("testing A[$i] = $a\n")
- while B[j,1] < a && j < sB; j+=1; end
- # print(" B[$j,1]=$(B[j,1])\n")
- if(B[j,1] == a); M = lcat(M,[i=>j]); end
- end
- A1 = fill(0., size(M,1), size(A,2)-2)
- B1 = fill(0., size(M,1), size(B,2)-2)
- W = fill(0, size(M,1))
- for i = 1:size(M,1)
- # print("M[$i] = $(M[i])\n")
- w = A[M[i].first,2]; w = (w == 0) ? 1 : w; # prevents div-by-0
- W[i] = w; # A[M[i].first, 2]
- for j = 1:size(A1,2)
- A1[i,j] = A[M[i].first,j+2]
- end
- for j = 1:size(B1,2)
- B1[i,j] = B[M[i].second,j+2]
- end
- end
- A1,B1,W
- end
- A0 = read_x()
- B0 = read_y()
- X,Y,weight = merge_mat(A0,B0)
- N = size(X,1)
- sX = size(X,2)
- sY = size(Y,2)
- result_y = [Int(sum([ B0[i,j+2] for i=1:size(B0,1) ])) for j=1:sY]
- result_x = [Int(sum([X[i,j] for i=1:N])) for j=1:sX]
- perm_y = lcat(sortperm(result_y[1:sY-length(noms_nuls)],rev=true),
- sY-length(noms_nuls)+1:sY)
- Y = [Y[i,perm_y[j]] for i=1:N,j=1:sY]
- noms_y = [noms_y[perm_y[j]] for j=1:sY]
- using JuMP
- using Ipopt
- model = Model(with_optimizer(Ipopt.Optimizer))
- @variable(model, a[1:sY,1:sX] >= 0)
- # @constraint(model, stoch[i=1:sX], sum(a[j,i] for j=1:sY) == 1)
- L = [-2*sum([ X[i,k]*Y[i,j]/weight[i] for i=1:N ]) for j=1:sY, k=1:sX ];
- Q = [ sum([ X[i,k]*X[i,l]/weight[i] for i=1:N]) for k=1:sX, l=1:sX ];
- # @constraint(model, consist[j=1:sY],
- # sum(a[j,i]*result_x[i] for i=1:sX) == result_y[j]);
- @objective(model, Min, sum(L[j,k]*a[j,k] for j=1:sY, k=1:sX)
- + sum(Q[k,l]*a[j,k]*a[j,l] for j=1:sY, k=1:sX, l=1:sX));
- optimize!(model)
- Sol = value.(a)
- writedlm("solved.csv",
- hcat(lcat("",noms_x), permutedims(hcat(noms_y, Sol), (2,1))))
- open("transfer.txt", "w") do f
- for j=1:sX, i=1:sY
- c=Int(round(Sol[i,j]*result_x[j]/2.3e5))
- x=noms_x[j]
- y=noms_y[i]
- if c > 0
- write(f, "$x\t[$c]\t$y\n")
- end
- end end
- # using LinearAlgebra
- # tX = transpose(X) # * Diagonal(W)
- # K = tX*X
- # sK = Symmetric(K)
- # L = tX*Y
- # Z = sK\L
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement