Esempio n. 1
0
def test_comb(options,o_min, o_max, to_include=None ):
    """ the naive approach """
    config.checks = 0
    if o_max == None: 
        o_max = len(options)
    if o_min == None: 
        o_min = 0
    if to_include == None:
        to_include = []
    #options=g_ids optionset=a subset of g_ids, i.e, a potential soln
     # creates an indexed hash 
    B = combfuncs.createLookup(options)
    if o_max > len(options):
        o_max = len(options)
    option_length = range(o_min, o_max+1)
    #option_length = range(len(options))
    option_length.reverse() #start with the largest sets
    # generate powersets
    for k in option_length:
        s = ksubsetlex.all(B, k) # s is an iterator over i-th subsets of B with lexicographic ordering
        for optionset in s: # call generator
            for o_in in to_include:
                optionset.append(o_in) # a list of elements the user says must be in the solution
            ad = is_admissible(optionset)
            config.checks += 1
            if ad:
                #pass
                print optionset, " admissible"
                
    print 'made ', config.checks, 'checks'
Esempio n. 2
0
def naive_option(parser, options,   ): 
    """ Given a set of options, create a powerset and try them for admissibility
     Return the sets of options which are admissible or none"""
    valid = {}
    eval_id = 0
    if options == []:
        # run the basic admissibility test
        parser.set_node_ids() #renumber the graph
        parser.generate_seb()
        parser.print_files()
        parser.zero_counts()
        try:
            admissible = parser.run_seb()
        except SebException as se:
                #print "Inadmissible"
                admissible = False
        #print "Admissible: ", admissible
        if admissible:
            valid[eval_id] = 'All Admissible'
        else:
            valid[eval_id] = 'All Inadmissible'
        return valid
    #continue with options if they exist    
    if o_max == None: 
        o_max = len(options)
    if o_min == None: 
        o_min = 0
   #options=g_ids optionset=a subset of g_ids, i.e, a potential soln
    # creates an indexed hash 
    B = combfuncs.createLookup(options)
    if o_max > len(options):
        o_max = len(options)
    option_length = range(o_min, o_max+1)
    option_length.reverse() #start with the largest sets
    for k in option_length:
       # start = time.clock()
        s = ksubsetlex.all(B, k) # s is an iterator over i-th subsets of B with lexicographic ordering
        for optionset in s: # call generator
            if optionset == []:
                break
            print 'optionset is', optionset
            #for o_in in to_include:
                #optionset.append(o_in) # a list of elements the user says must be in the solution                
            eval_id = eval_id + 1
            eval_version = 'option-' + str(eval_id)
            is_subset = False
            for solution in valid.keys(): # don't check subsets of solutions.  #TODO At some point do this efficiently in the powerset generator
                solution = set(valid[solution])
                oset = set(optionset)
                if oset.issubset(solution):
                    is_subset = True
                    break
            if is_subset: 
                continue #with next optionset
            # run the evaluation
            for g_id in optionset: # the graffle ID, immutable
                parser.set_node_status(g_id, 'to_unknown') # change the model so this node is neither optional nor mandatory
            parser.set_node_ids() #renumber the graph
            parser.generate_seb()
            parser.print_files()
            parser.zero_counts()
            try:
                admissible = parser.run_seb()
            except SebException as se:
                for g_id in optionset:
                    parser.set_node_status(g_id, 'to_optional')
                    #print "Inadmissible"
                break
            #print "Admissible: ", admissible
            if admissible:
                valid[eval_version] = optionset
            else:
                valid[eval_version] = 'false'
            # reset the options to T for the next calculation
            for g_id in optionset:
                parser.set_node_status(g_id, 'to_optional')
        #print str(k)+'-subset time taken: ' + str(time.clock() - start)
    # print "valid are:"
    # for v in valid.keys():
    #     print v, ': ',
    #     for o in valid[v]:
    #         print o.name, ',',
    #     print ''
    return valid