require 'csv' require 'set' # Usage: ruby this_program.rb matrix_path percentage # Author: Henrique Varella Ehrenfried # Obseration: the number told in the positions of random_numbers array is increased by one, due the position in array, # so if it says the line 5, it will take line 6 in the csv # Read CSV matrix = CSV.read(ARGV[0]) # Get number of lines number_of_lines = matrix.size - 1 # Get number of mutants desired number_desired_of_mutants = ( (ARGV[1].to_i * number_of_lines) / (100) ).to_i # Generate one matrix with all random_numbers indexes def gen_matrix(matrix, random_numbers) # Get the header matrix_interested = [matrix.first] # For each number in random_numbers random_numbers.each do |r| # Get the line that is in matrix and append to matrix)interested matrix_interested << matrix[r] end return matrix_interested end def find_not_dead_mutants(matrix, number_of_lines, number_desired_of_mutants) # Initialize an Array possibilities = Array.new # Set the iterator to 1 i = 1 # While the iterator is lower than the number of lines while i < number_of_lines # If none of the columns are 1 a = matrix[i].first.split(';') a.pop a.pop if a.map{|x| x.to_i}.reduce(&:+) > 0 # Append this index to the possibilities array possibilities << i end # Increment the iterator i+=1 end return possibilities end def gen_random_mutants(possibilities,number_desired_of_mutants) # Shuffle this array randomly and take the first 'number_desired_of_mutants' elements random_index = possibilities.shuffle.take(number_desired_of_mutants) return random_index end def matrix_set(matrix, number_of_lines, number_desired_of_mutants) i = 0 all_matrix = [] possibilities = find_not_dead_mutants(matrix, number_of_lines, number_desired_of_mutants) if ARGV[1].to_i != 100 while i < 10 # Get array of indexes of the interested mutants random_numbers = gen_random_mutants(possibilities, number_desired_of_mutants) # Append result all_matrix << gen_matrix(matrix, random_numbers) i += 1 end else # Get array of indexes of the interested mutants random_numbers = gen_random_mutants(possibilities, number_desired_of_mutants) # Append result all_matrix << gen_matrix(matrix, random_numbers) end return all_matrix end def to_file(data, name) CSV.open("./#{@directory_name}/"+name+".csv", "wb") do |csv| data.each do |dt| csv << dt end end end def save_in_file(a,i,number_desired_of_mutants) # Write matrix to file to_file(a,"ST_"+ARGV[0].to_s.split("/").last(2).first.upcase) # Define array to save operator operators = Hash.new # For each line, get its operator a.each_with_index do |b,j| # Make a string turn into an array op = b.first.split(';') # If it is the first line (header) if j == 0 # Skip this time of the loop next else # Take just the name of the operator ops = op.first.split("_") # Verify if the operator is already in the hash, if it is if operators.has_key?(ops.first) # Add 1 to the count operators[ops.first]+=1 else # If it is not in, set this key/value operators[ops.first] = 1 end end end # Sort hash by the least chosen mutant num = 0 operators = operators.sort_by {|_key, value| value}.to_h # Create a file File.open("#{@directory_name}/OP_MRS_"+ARGV[1].to_s+"_"+i.to_s+".txt", "w+") do |f| # Print the operators saved in the file operators.each{|k,v| f.puts(k.to_s+': '+v.to_s); num+=v.to_i} f.puts("---\nTOTAL: #{num}") end end def run_optimization # system("ls #{@directory_name}/*.csv") Dir.glob("#{@directory_name}/*.csv") do |file| system("java -cp TestCaseSetSelection/compiled/TestCaseSetSelection.jar br.ufpr.inf.cbiogres.main.Execute #{file} ';' true 10 > RESULT_OP_#{file.split("/").last}.txt") end end @directory_name = "STRATEGY_"+ARGV[0].to_s.split("/").last(2).first.upcase Dir.mkdir(@directory_name) unless File.exists?(@directory_name) all = matrix_set(matrix, number_of_lines, number_desired_of_mutants) all.each_with_index do |a, i| save_in_file(a,i,number_desired_of_mutants) end run_optimization