project1/p1q2.rb
def select_actors_combi(k, r, n, actor_list)
  result = []
  result_q = 0

  # process all k-combinations of top-(n+1) actors until timeout
  actor_list[0..n].combination(k) do |actors|
    break if (Time.now - @start_time) > @timeout

    # accumulate the number of chosen-actors in each movie
    c_movies = Hash.new(0)
    for actor in actors do
      mm = actor[1]
      for movie in mm do
        c_movies[movie.object_id] += 1
      end
    end

    # determine the quality of this combination
    # i.e. how many movies with at least r chosen-actors
    c_movies_score = 0
    for v in c_movies.values do
      c_movies_score += 1 if v >= r
    end

    # replace if better than previous solution
    if c_movies_score > result_q
      result = actors
      result_q = c_movies_score
    end
  end

  # convert actor arrays to actor names
  return result.map { |a| a[0] }
end
# n is top n actors to consider (0 to n inclusive)
n is top n actors to consider (0 to n inclusive)
n is top n actors to consider ( 0 to n inclusive )
def select_actors_combi(k, r, n, actor_list)
  result = []
  result_q = 0
  actor_list[0..n].combination(k) do |actors|
    break if (Time.now - @start_time) > @timeout
    c_movies = Hash.new(0)
    for actor in actors do
      mm = actor[1]
      for movie in mm do
        c_movies[movie.object_id] += 1
      end
    end
    c_movies_score = 0
    for v in c_movies.values do
      c_movies_score += 1 if v >= r
    end
    if c_movies_score > result_q
      result = actors
      result_q = c_movies_score
    end
  end
  return result.map { |a| a[0] }
end
def select_actors_combi ( k , r , n , actor_list ) result = [ ] result_q = 0 actor_list [ 0 .. n ] . combination ( k ) do | actors | break if ( Time . now - @start_time ) > @timeout c_movies = Hash . new ( 0 ) for actor in actors do mm = actor [ 1 ] for movie in mm do c_movies [ movie . object_id ] += 1 end end c_movies_score = 0 for v in c_movies . values do c_movies_score += 1 if v >= r end if c_movies_score > result_q result = actors result_q = c_movies_score end end return result . map { | a | a [ 0 ] } end
n is top n actors to consider (0 to n inclusive)
n is top n actors to consider ( 0 to n inclusive )
# process all k-combinations of top-(n+1) actors until timeout # accumulate the number of chosen-actors in each movie # determine the quality of this combination # i.e. how many movies with at least r chosen-actors # replace if better than previous solution # convert actor arrays to actor names
k
r
n
actor_list