<s> import os <EOL> from subprocess import Popen , PIPE <EOL> from collections import namedtuple <EOL> Set = namedtuple ( "<STR_LIT>" , [ '<STR_LIT:index>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def solve_it ( input_data ) : <EOL> lines = input_data . split ( '<STR_LIT:\n>' ) <EOL> parts = lines [ <NUM_LIT:0> ] . split ( ) <EOL> item_count = int ( parts [ <NUM_LIT:0> ] ) <EOL> set_count = int ( parts [ <NUM_LIT:1> ] ) <EOL> sets = [ ] <EOL> for i in range ( <NUM_LIT:1> , set_count + <NUM_LIT:1> ) : <EOL> parts = lines [ i ] . split ( ) <EOL> sets . append ( Set ( i - <NUM_LIT:1> , float ( parts [ <NUM_LIT:0> ] ) , map ( int , parts [ <NUM_LIT:1> : ] ) ) ) <EOL> data_file = "<STR_LIT>" <EOL> generateMinizincDataFile ( item_count , set_count , sets , data_file ) <EOL> nb_solutions = <NUM_LIT:10> <EOL> process = Popen ( [ '<STR_LIT>' , '<STR_LIT>' , str ( nb_solutions ) , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> stdout = PIPE , stderr = PIPE ) <EOL> ( stdout , stderr ) = process . communicate ( ) <EOL> print stderr <EOL> solution = extractSolution ( stdout , set_count ) <EOL> obj = sum ( [ s . cost * solution [ s . index ] for s in sets ] ) <EOL> output_data = str ( obj ) + '<STR_LIT:U+0020>' + str ( <NUM_LIT:1> ) + '<STR_LIT:\n>' <EOL> output_data += '<STR_LIT:U+0020>' . join ( map ( str , solution ) ) <EOL> return output_data <EOL> def generateMinizincDataFile ( item_count , set_count , sets , data_file ) : <EOL> tmpFile = open ( data_file , '<STR_LIT:w>' ) <EOL> out = "<STR_LIT>" <EOL> out += "<STR_LIT>" + str ( item_count ) + "<STR_LIT>" <EOL> out += "<STR_LIT>" + str ( set_count ) + "<STR_LIT>" <EOL> out += "<STR_LIT>" <EOL> cnt = <NUM_LIT:0> <EOL> for s in sets : <EOL> out += str ( int ( s . cost ) ) <EOL> if cnt == len ( sets ) - <NUM_LIT:1> : <EOL> out += "<STR_LIT>" <EOL> else : <EOL> out += "<STR_LIT:U+002CU+0020>" <EOL> cnt += <NUM_LIT:1> <EOL> out += "<STR_LIT>" <EOL> cnt = <NUM_LIT:0> <EOL> for s in sets : <EOL> out += "<STR_LIT>" <EOL> cnt2 = <NUM_LIT:0> <EOL> for e in s . items : <EOL> out += str ( int ( e ) ) <EOL> if cnt2 == len ( s . items ) - <NUM_LIT:1> : <EOL> out += "<STR_LIT>" <EOL> else : <EOL> out += "<STR_LIT:U+002CU+0020>" <EOL> cnt2 += <NUM_LIT:1> <EOL> if cnt == len ( sets ) - <NUM_LIT:1> : <EOL> out += "<STR_LIT>" <EOL> else : <EOL> out += "<STR_LIT:U+002CU+0020>" <EOL> cnt += <NUM_LIT:1> <EOL> tmpFile . write ( out ) <EOL> tmpFile . close ( ) <EOL> def extractSolution ( stdout , set_count ) : <EOL> solution = [ ] <EOL> for i in range ( <NUM_LIT:0> , set_count ) : <EOL> solution . append ( <NUM_LIT:0> ) <EOL> lines = stdout . split ( '<STR_LIT:\n>' ) <EOL> line = lines [ <NUM_LIT:0> ] <EOL> words = line . split ( ) <EOL> if len ( words ) != set_count : <EOL> print "<STR_LIT>" <EOL> else : <EOL> for i in range ( <NUM_LIT:0> , len ( words ) ) : <EOL> solution [ i ] = int ( words [ i ] ) <EOL> return solution <EOL> import sys <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> file_location = sys . argv [ <NUM_LIT:1> ] . strip ( ) <EOL> input_data_file = open ( file_location , '<STR_LIT:r>' ) <EOL> input_data = '<STR_LIT>' . join ( input_data_file . readlines ( ) ) <EOL> input_data_file . close ( ) <EOL> print '<STR_LIT>' , file_location <EOL> print solve_it ( input_data ) <EOL> else : <EOL> print '<STR_LIT>' </s>
