Esempio n. 1
0
def testsp_file(filename, sp_type, verbose=False):
    print("%s," % (filename), end="")

    election_file = open(filename, 'r')
    try:
        cmap, rmaps, unused_rmapcounts, nvoters = \
            preflibio.read_election_file(election_file)
    except Exception:
        print("0,0,FileError")
        sys.exit(0)
    print("%d,%d," % (len(cmap), nvoters), end="")

    profile_mat = []
    for pref in rmaps:
        pref_mat = create_matrix(pref, cmap, sp_type)
        for row in pref_mat:
            profile_mat.append(row)

    result = consones.solve_sat(profile_mat)
    if result is not None:
        print("True", end="")
    else:
        print("False", end="")

    if verbose:
        print()
        # prints the axis
        if result is not None:
            print("Axis:", [cmap[i + 1] for i in result])
Esempio n. 2
0
def main():
    filename = input("Preference profile file: ")
    input_file = open(filename, 'r')
    cand_map, rank_maps, rank_map_counts, num_voters = io.read_election_file(
        input_file)
    sol, path = solve(rank_maps, rank_map_counts, len(cand_map))
    print("By choosing profiles" + str(path) +
          ", %.2f pc of the voters are included" % (sol / num_voters * 100))
Esempio n. 3
0
def main():
    input_file = open(sys.argv[1], 'r')
    cand_map, rank_maps, rank_map_counts, num_voters = io.read_election_file(
        input_file)
    num_cand = len(cand_map)
    num_votes = len(rank_map_counts)
    delVotes, delW = deleted(rank_maps, num_cand, rank_map_counts)
    solution = list(filter(lambda x: not (x in delVotes), range(num_votes)))
    print('By choosing votes ' + str(solution) +
          ',%.2f pc of voters are included' %
          ((num_voters - delW) / num_voters * 100))
  io.pp_profile_toscreen(candmap, rankmaps, rankmapcounts)

  social_axis = is_single_peaked(rankmaps, candmap)
  if social_axis != []:
    print("Single Peaked w.r.t " + str(social_axis))
  else:
    print("Not Single Peaked")


  # Test all the SOC's... for fun....
  files = glob.glob("./soc/*.soc")
  total = 0
  totalSP = 0
  for cfile in sorted(files):
    print("Testing: " + str(cfile))
    inf = open(cfile, "r")
    candmap, rankmaps, rankmapcounts, numvoters = io.read_election_file(inf)
    total += 1
    social_axis = is_single_peaked(rankmaps, candmap)
    if social_axis != []:
      print("Single Peaked w.r.t " + str(social_axis))
      totalSP += 1
    else:
      print("Not Single Peaked")
    inf.close()
  print("Parsed " + str(total) + " SOC files")
  print("Exactly " + str(totalSP) + " were single peaked")



  
    io.pp_profile_toscreen(candmap, rankmaps, rankmapcounts)

    social_axis = is_single_peaked(rankmaps, candmap)
    if social_axis != []:
        print("Single Peaked w.r.t " + str(social_axis))
    else:
        print("Not Single Peaked")

    # Test all the SOC's... for fun....
    files = glob.glob("./soc/*.soc")
    total = 0
    totalSP = 0
    for cfile in sorted(files):
        print("Testing: " + str(cfile))
        inf = open(cfile, "r")
        candmap, rankmaps, rankmapcounts, numvoters = io.read_election_file(
            inf)
        total += 1
        social_axis = is_single_peaked(rankmaps, candmap)
        if social_axis != []:
            print("Single Peaked w.r.t " + str(social_axis))
            totalSP += 1
        else:
            print("Not Single Peaked")
        inf.close()
    print("Parsed " + str(total) + " SOC files")
    print("Exactly " + str(totalSP) + " were single peaked")

    # Test 1000 random-culture profiles:
    total = 0
    totalSP = 0
    for i in range(1000):