SHARE
TWEET

Untitled

a guest Nov 17th, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function optimization(data, n, k, ρ)
  2.     data_mean = mean(data)
  3.     data_std = std(data)
  4.     norm_data = (data .- data_mean) ./ data_std
  5.    
  6.     m = Model(solver=GurobiSolver(OutputFlag=0))
  7.     @variable(m, d)
  8.     @variable(m, x1[1:n], Bin)
  9.     @variable(m, x2[1:n], Bin)
  10.     mu1 = 1 / k * sum(norm_data .* x1)
  11.     mu2 = 1 / k * sum(norm_data .* x2)
  12.     sigma1 = 1 / k * sum((norm_data .^ 2) .* x1)
  13.     sigma2 = 1 / k * sum((norm_data .^ 2) .* x2)
  14.     @constraint(m, d >= mu1 - mu2 + ρ*sigma1 - ρ*sigma2)
  15.     @constraint(m, d >= mu1 - mu2 + ρ*sigma2 - ρ*sigma1)
  16.     @constraint(m, d >= mu2 - mu1 + ρ*sigma1 - ρ*sigma2)
  17.     @constraint(m, d >= mu2 - mu1 + ρ*sigma2 - ρ*sigma1)
  18.     @constraint(m, sum(x1) == k)
  19.     @constraint(m, sum(x2) == k)
  20.     @constraint(m, x1 .+ x2 .== 1)
  21.     @constraint(m, x1[1] == 0)
  22.     @objective(m, Min, d)
  23.     @show status=solve(m)
  24.     x1 = getvalue(x1)
  25.     x2 = getvalue(x2)
  26.    
  27.     first_group, second_group = zeros(0), zeros(0)
  28.     for i = 1:n
  29.         if x1[i] == 1
  30.             append!(first_group, data[i])
  31.         else
  32.             append!(second_group, data[i])
  33.         end
  34.     end
  35.     return first_group, second_group
  36. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top