from otree.api import * from array import * from csv import writer, reader list_data=[] class C(BaseConstants): NAME_IN_URL = 'travelling_salesman_problem' PLAYERS_PER_GROUP = None NUM_ROUNDS = 16 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): name = models.StringField(label='enter your name') noOfCities =models.StringField(label='') timeTaken =models.StringField(label='') score =models.StringField(label='') optimalScore =models.StringField(label='') path =models.StringField(label='') totalScore = models.IntegerField() totalScorePercent = models.FloatField() # DOptimal = models.FloatField() # FUNCTIONS # PAGES class play(Page): form_model = 'player' form_fields = ['noOfCities','timeTaken','score','optimalScore','path'] @staticmethod def vars_for_template(player): boardNumber = player.round_number if(not list_data or list_data == []): if( player.participant.vars and player.participant.vars['name']): list_data.append(player.participant.vars["name"]) else: opened_file = open('TravellingSalesmanProblem.csv') read_file = reader(opened_file) apps_data = list(read_file) rowcount = len(apps_data) list_data.append(str(rowcount)) round_message = '' if boardNumber == 1: round_message = 'This is a practice round.' else: round_message = f'This is round {boardNumber - 1} out of {C.NUM_ROUNDS - 1}' return dict( board= setBoard(boardNumber), maxScore = getOptimalScore(boardNumber), roundMessage = round_message ) class endPage(Page): form_model = 'player' @staticmethod def vars_for_template(player): boardNumber = player.round_number if(player.field_maybe_none('noOfCities') and player.field_maybe_none('noOfCities') != ''): list_data.append(player.noOfCities) if(player.field_maybe_none('timeTaken') and player.field_maybe_none('timeTaken') != ''): list_data.append(player.timeTaken) if(player.field_maybe_none('score') and player.field_maybe_none('score') != ''): list_data.append(player.score) if(player.field_maybe_none('optimalScore') and player.field_maybe_none('optimalScore') != ''): list_data.append(player.optimalScore) if(player.field_maybe_none('path') and player.field_maybe_none('path') != ''): list_data.append(player.path) # if(boardNumber ==2): # with open('TravellingSalesmanProblem.csv', 'a', newline='') as f_object: # writer_object = writer(f_object) # writer_object.writerow(list_data) # f_object.close() totalScore = 0 for i, p in enumerate(player.in_all_rounds()): if i != 0: score = p.field_maybe_none('score') optimalScore = p.field_maybe_none('optimalScore') # print(score, optimalScore) if (int(float(score)) == int(float(optimalScore))): totalScore += 1 # print(f'total score: {totalScore}') totalScorePercentage = (totalScore / (C.NUM_ROUNDS - 1)) * 100 player.totalScorePercent= totalScorePercentage player.totalScore= totalScore # DOptimal = (optimalScore-score) # player.DOptimal = DOptimal return dict( boardNumber= boardNumber, numRounds = C.NUM_ROUNDS, list = list_data, totalScorePercentage = totalScorePercentage ) class welcome(Page): form_model = 'player' @staticmethod def vars_for_template(player): boardNumber = player.round_number return dict( boardNumber= boardNumber, ) class board1(Page): form_model = 'player' @staticmethod def vars_for_template(player): boardNumber = player.round_number return dict( boardNumber = boardNumber, ) page_sequence = [welcome, board1, play, endPage] def setBoard(boardNumber): boards = [ #Practice Round - 3 Cities [ [[698.813506634905], [581.674739718437]], [[838.195873773656], [323.888576077297]], [[734.884095494635], [149.823071062565]], ], #Round 1 - 4 Cities [ [[398.8922], [180.64706]], [[376.7825], [99.17237]], [[195.6278], [267.42898]], [[170.7783],[48.26312]], ], #Round 2 - 5 Cities [ [[398.8922], [99.17237]], [[376.7825], [267.42898]], [[195.6278], [48.26312]], [[170.7783], [493.92347]], [[180.6471], [83.78474]], ], #Round 3 - 6 Cities [ [[398.89216], [267.42898]], [[376.78255], [48.26312]], [[195.62784], [493.92347]], [[170.77835], [83.78474]], [[180.64706], [398.99456]], [[99.17237], [296.89701]], ], #Round 4 - 7 Cities [ [[398.89216], [48.26312]], [[376.78255], [493.92347]], [[195.62784], [83.78474]], [[170.77835], [398.99456]], [[180.64706], [296.89701]], [[99.17237], [452.65500]], [[267.42898], [440.42432]], ], #Round 5 - 8 Cities [ [[107.18389], [135.966938]], [[464.75194], [454.988297]], [[393.18325], [398.529632]], [[194.23883], [9.348362]], [[165.83704], [432.505452]], [[15.43076], [8.448996]], [[283.76471], [186.873872]], [[224.64791], [128.223178]], ], #Round 6 - 9 Cities [ [[253.2789], [406.171169]], [[242.7643], [135.812728]], [[107.5443], [8.557189]], [[141.7779], [352.000090]], [[402.0389], [405.389330]], [[474.2971], [150.191634]], [[344.2671], [416.216295]], [[351.5205], [195.975479]], [[180.7502], [320.163464]], ], #Round 7 - 10 Cities [ [[768.005888699553], [528.793477872386]], [[728.208585409447], [406.276413751766]], [[402.130115777254], [593.186005763709]], [[357.401028485037], [578.509188955649]], [[375.164701370522], [646.301930723711]], [[228.510261047631], [587.573801074177]], [[531.37216605246], [577.186010824516]], [[136.87361725606], [168.563449615613]], [[939.062248775735], [250.996160088106]], [[200.812528352253], [516.323790233582]], ], #Round 8 - 11 Cities [ [[398.89216], [296.89701]], [[376.78255], [452.65500]], [[195.62784], [440.42432]], [[170.77835], [496.91828]], [[180.64706], [447.97817]], [[99.17237], [439.32168]], [[267.42898], [98.80287]], [[48.26312], [167.49680]], [[493.92347], [388.60316]], [[83.78474], [79.79678]], [[398.99456], [40.32263]], ], #Round 9 - 12 Cities [ [[698.813506634905], [491.41097147949]], [[838.195873773656], [50.6819519214332]], [[734.884095494635], [284.722001152113]], [[847.512109577655], [327.496792469174]], [[460.832864115945], [282.886388944462]], [[199.734606593847], [291.491085058078]], [[342.585848039016], [157.378150895238]], [[508.301902096719], [620.995252206921]], [[704.93472837843], [322.236843965947]], [[940.763244125992], [246.051445184275]], [[81.0818915255367], [629.249194031581]], [[187.136141140945], [474.489126307889]], ], #Round 10 - 13 Cities [ [[162.30126], [315.2479]], [[468.80958], [449.6763]], [[348.55361], [317.2469]], [[91.96021], [200.6081]], [[435.16361], [159.0997]], [[493.18869], [156.3395]], [[40.04182], [233.5449]], [[219.07815], [388.2502]], [[488.44334], [232.3351]], [[370.05161], [285.2178]], [[366.35917], [433.1161]], [[382.07941], [152.5890]], [[85.00962], [308.8394]], ], #Round 11 - 14 Cities [ [[281.13042], [409.22894]], [[363.24925], [247.56248]], [[457.62493], [89.47103]], [[14.84719], [439.57111]], [[383.75485], [218.67038]], [[240.02699], [363.13562]], [[44.18980], [432.38430]], [[431.47293], [149.55365]], [[248.30153], [469.20678]], [[426.75201], [213.13412]], [[312.94397], [176.31779]], [[120.72625], [210.17333]], [[18.84487], [154.52061]], [[258.29649], [35.49003]], ], #Round 12 - 15 Cities [ [[556.034754193387], [347.074970602989]], [[703.848653961904], [157.365232985467]], [[873.724866588599], [577.485336828976]], [[76.7249334137886], [312.404455104842]], [[740.758733381517], [485.762748774141]], [[482.048580655828], [568.861161032691]], [[129.541640076786], [229.464375041425]], [[826.651278836652], [613.048133160918]], [[496.942751319148], [305.760939791798]], [[818.153626192361], [261.581342061982]], [[613.299141963943], [302.207997301593]], [[267.307254602201], [235.424736468121]], [[83.9207702549174], [92.5880339927971]], [[514.933673734776], [226.491037523374]], [[786.612093937583], [531.689353752881]], ], #Round 13 - 16 Cities [ [[296.490642], [85.72184]], [[363.240561], [483.26715]], [[185.211002], [155.33321]], [[257.461915], [307.23320]], [[188.831608], [215.53039]], [[209.168663], [142.76355]], [[5.328923], [171.07567]], [[266.147621], [193.31381]], [[216.080308], [483.76374]], [[46.840760], [161.01336]], [[277.889957], [326.61473]], [[295.114244], [141.65175]], [[33.570572], [393.71396]], [[23.773927], [297.96032]], [[78.101258], [29.29823]], [[223.213882], [254.94993]], ], #Round 14 - 17 Cities [ [[228.8056], [449.68793]], [[229.6262], [508.40719]], [[810.8590], [57.96022]], [[103.2483], [270.29146]], [[448.3472], [476.72750]], [[365.2183], [81.75784]], [[188.8972], [351.01649]], [[230.4180], [606.58373]], [[603.3105], [343.39908]], [[706.5655], [541.39259]], [[774.0548], [363.41885]], [[943.2172], [543.48146]], [[736.9716], [193.09544]], [[292.5644], [144.48456]], [[794.4348], [298.84425]], [[826.7716], [540.30754]], [[461.7361], [426.80870]], ], #Round 15 - 18 Cities [ [[220.48941], [477.77391]], [[280.04489], [474.57749]], [[73.49754], [432.98992]], [[398.01442], [87.91873]], [[242.73487], [246.69557]], [[308.72668], [392.18002]], [[346.67392], [48.15417]], [[480.23931], [232.60701]], [[136.82621], [422.23824]], [[334.73692], [404.81408]], [[29.30441], [148.04773]], [[448.39392], [361.85891]], [[344.52732], [288.61911]], [[459.23547], [330.90342]], [[100.67554], [194.16036]], [[34.34151], [187.22292]], [[281.30020], [128.53267]], [[294.31551], [129.64613]], ], ] if(boardNumber<=C.NUM_ROUNDS): return boards[boardNumber-1] return boards[boardNumber-1] def getOptimalScore(boardNumber): scores = [ 928.83, 738.21, 1134.22, 1185.2, 1348.69, 1473.35, 1350.93, 2257.75, 1562.09, 2513.99, 1405.87, 1688.08, 2561.03, 1566.98, 2669.43, 1702.22 ] return scores[boardNumber-1]