Powered by AppSignal & Oban Pro

Optimizing Yappy Chat Rooms

notebook/overview.livemd

Optimizing Yappy Chat Rooms

Introduction

Hello This is something we would like to show you!

Initializing Data

alias VegaLite, as: Vl
alias Candidate
alias Heuristics.HillClimbing

default_group_size = 3

%{state: state, amity: edges} = InputReader.load_data_from_file("data/example2.txt")

fitness_func = fn s ->
  Candidate.score(s, default_group_size, edges)
end

IO.puts("Initial")
IO.inspect(state, label: "\tState")
IO.inspect(Candidate.group_scores(state, default_group_size, edges), label: "\tValues")
IO.puts("\tScore: #{Candidate.score(state, default_group_size, edges)}")

initial_data =
  Candidate.group_scores(state, default_group_size, edges)
  |> Stream.with_index()
  |> Enum.map(fn {x, i} ->
    %{"group_index" => "#{i}", "score" => x}
  end)

Vl.new(width: 400, height: 300)
|> Vl.data_from_values(initial_data)
|> Vl.mark(:bar)
|> Vl.encode_field(:x, "group_index", type: :nominal, axis: [label_angle: 0])
|> Vl.encode_field(:y, "score", type: :quantitative)

Hill Climbing

optimized = HillClimbing.start(state, fitness_func, %{iterations: 100_000})
optimized_group_scores = Candidate.group_scores(optimized, default_group_size, edges)
optimized_score = Candidate.score(optimized, default_group_size, edges)

IO.inspect(optimized, label: "\tState")
IO.inspect(optimized_group_scores, label: "\tValues")
IO.puts("\tScore: #{optimized_score}")

data =
  optimized_group_scores
  |> Stream.with_index()
  |> Enum.map(fn {x, i} ->
    %{"group_index" => "#{i}", "score" => x}
  end)

Vl.new(width: 400, height: 300)
|> Vl.data_from_values(data)
|> Vl.mark(:bar)
|> Vl.encode_field(:x, "group_index", type: :nominal, axis: [label_angle: 0])
|> Vl.encode_field(:y, "score", type: :quantitative)

Simulated Annealing

#TODO Describe SA Algorithm Show

optimized =
  state
  |> Heuristics.SimulatedAnnealing.start(fitness_func, %{iterations: 100_000})

optimized_group_scores = Candidate.group_scores(optimized, default_group_size, edges)
optimized_score = Candidate.score(optimized, default_group_size, edges)

data =
  optimized_group_scores
  |> Stream.with_index()
  |> Enum.map(fn {x, i} ->
    %{"algo" => "Hill Climbing", "group_index" => "#{i}", "score" => x}
  end)

Vl.new(
  width: 400,
  height: 300,
  title: "Optimized: #{:erlang.float_to_binary(optimized_score, decimals: 3)}"
)
|> Vl.data_from_values(data)
|> Vl.mark(:bar)
|> Vl.encode_field(:x, "group_index", type: :nominal, axis: [label_angle: 0])
|> Vl.encode_field(:y, "score", type: :quantitative)