Beispiel #1
0
 def test_combinatorics(self):
     """Ensure a proper enumeration"""
     p = mi.powerset([1, 2, 3])
     self.assertEqual(
         list(p),
         [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
     )
Beispiel #2
0
def permutator(set_1, set_2):
    """Write the definition of the permutation operator given by the two sets.

    Args:
        set_1 (list): The list of the left-hand-side qp labels.
        set_2 (list): The list of the right-hand-side qp labels.

    Returns:
        (str): The LaTeX expression for the permutation operator.

    >>> print(permutator([1, 2], [3])) # doctest: +NORMALIZE_WHITESPACE
    P(k_{1}k_{2}/k_{3}) &= 1 - P_{k_{1} k_{3}} - P_{k_{2} k_{3}} \\\\\n

    """
    perm_op = 'P(%s/%s) &= 1 ' % ("".join(
        'k_{%i}' % label for label in set_1), "".join('k_{%i}' % label
                                                      for label in set_2))
    perm_counter = 0
    # Combine all possible subsets of both sets
    for subset_1, subset_2 in product(powerset(set_1), powerset(set_2)):
        # Ensure each qp state has a permutation partner, exclude the empty set
        if (len(subset_1) == len(subset_2)) and (len(subset_1) > 0):
            # Run through all permutations of the first subset
            for permutation in permutations(subset_1):
                perm_op += '+ ' if len(subset_1) % 2 == 0 else '- '
                for elem in zip(permutation, subset_2):
                    perm_op += 'P_{k_{%i} k_{%i}} ' % elem
                    perm_counter += 1
                # Avoid permutators lines being too long
                if perm_counter >= 10:
                    perm_op += '\\\\\n &\\phantom{=} '
                    perm_counter = 0
    if perm_op.endswith('\\\\\n &\\phantom{=} '):
        perm_op = perm_op[:-len(' &\\phantom{=} ')]
    else:
        perm_op += "\\\\\n"
    return perm_op
Beispiel #3
0
def main(inp):
    inp = inp.strip()
    mem = {}
    for l in inp.split("\n"):
        tokens = l.split()
        if p := parse("mask = {}", l):
            mask = p[0]
        else:
            add, value = parse("mem[{:d}] = {:d}", l)
            add |= int(tr(mask, "X", "0"), 2)
            add &= ~int(tr(mask, "1X", "01"), 2)
            xs = [35 - i for i, x in enumerate(mask) if x == "X"]
            for subset in powerset(xs):
                val = add + sum(map(pow, repeat(2), subset))
                mem[val] = value
def floaters(address, mask):
    addresses = set()
    xs = {match.start() for match in re.finditer('X', mask)}
    x_combos = more_itertools.powerset(xs)
    address = f'{address:0>36b}'
    for combo in x_combos:
        new_address = list(address)
        for x in xs:
            if x in combo:
                new_address[x] = '1'
            else:
                new_address[x] = '0'
        new_address = int(''.join(new_address))
        addresses.add(new_address)
    return addresses
Beispiel #5
0
def generate_images(extracted_content_path=EXTRACTED_CONTENT_PATH, output_file_prefix="", output_path=OUTPUT_PATH,
                    all_combinations=False, separate=False, background_layer=False, output_type='jpg'):
    lsr_content = json_file_to_object( f"{extracted_content_path}/Contents.json")
    width = lsr_content['properties']['canvasSize']['width']
    height = lsr_content['properties']['canvasSize']['height']
    num_layers = len(lsr_content['layers'])

    combinations = [[i for i in range(1, num_layers + 1)]]
    if all_combinations:
        combinations = [list(subset) for subset in list(more_itertools.powerset(combinations[0])) if subset]
        if background_layer:
            combinations = [subset for subset in combinations if num_layers in subset]
    elif separate:
        combinations = [[i] for i in range(1, num_layers + 1)]
    
    for combination in combinations:
        img = Image.new('RGBA', (width, height))

        for layer_num in combination[::-1]:
            layer = lsr_content['layers'][layer_num - 1]
            layer_name = layer['filename']
            layer_content = json_file_to_object(f"{extracted_content_path}/{layer_name}/Contents.json")

            layer_width = layer_content['properties']['frame-size']['width']
            layer_height = layer_content['properties']['frame-size']['height']
            layer_center_x = layer_content['properties']['frame-center']['x']
            layer_center_y = layer_content['properties']['frame-center']['y']

            layer_image_set_content = json_file_to_object(
                f"{extracted_content_path}/{layer_name}/Content.imageset/Contents.json")

            for image in layer_image_set_content['images']:
                image_path = f"{extracted_content_path}/{layer_name}/Content.imageset/{image['filename']}"
                img.alpha_composite(Image.open(image_path).convert('RGBA'),
                    (int(layer_center_x - layer_width/2), int(layer_center_y - layer_height/2)))

        try:
            os.makedirs(output_path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        
        combination_str = ','.join([str(i) for i in combination])

        if output_type == 'jpg':
            img = img.convert('RGB')
        img.save(f"{output_path}/{output_file_prefix}({combination_str}).{output_type}")
Beispiel #6
0
    def make_fair_teams(self) -> Solution:
        minimum = 20 * self.valuator.valueof(Division.C)
        optimum_solution = Solution(Team([], 0), Team([], 0))

        viable_solutions = list((set_, self._complem(set_))
                                for set_ in powerset(self.players)
                                if 1 <= len(set_) <= 5)
        for players1, players2 in viable_solutions:
            val1, val2 = self._values_of_teams(players1, players2)
            diff = abs(val2 - val1)
            if diff < minimum:
                minimum = diff
                optimum_solution.team1.players = list(players1)
                optimum_solution.team2.players = list(players2)
                optimum_solution.team1.value = val1
                optimum_solution.team2.value = val2

        return optimum_solution
def get_plausible_sets(board, pieces):
    piece_power_set = more_itertools.powerset(pieces)
    plausibleSets = []
    for aSet in piece_power_set:
        sum_of_set = {}
        for item in aSet:
            this_pieces_spots = num_spots_in_piece(pieces[item])
            for piece_type in this_pieces_spots:
                if piece_type in sum_of_set:
                    sum_of_set[piece_type] += this_pieces_spots[piece_type]
                else:
                    sum_of_set[piece_type] = this_pieces_spots[piece_type]
        if has_necessary_num_pieces(board, sum_of_set):
            plausibleSet = {}
            myIter = 0
            for item in aSet:
                plausibleSet[myIter] = pieces[item]
                myIter += 1
            plausibleSets.append(plausibleSet)
    return plausibleSets
def find_matching_complex(G, edge_labels=None):
    """
    Finds the matching complex of a given graph G

    @params
                  G = networkx graph object storing the graph to find a matching complex of. To create this from an
                      edge list, simply do:
                            G = networkx.Graph()
                            G.add_edges_from(edge_list)
                            
        edge_labels = List of names to give each of the edges in the matching.

    @returns
        maximal_matchings = A list of tuples for each face in the matching complex. e.g. a 1-tuple specifies a vertex, a 2-tuple an edge,
                            a 3-tuple a triangle, a 4-tuple a tetrahedron, etc
    """
    if edge_labels is None:
        edge_labels = {e: e for e in enumerate(G.edges())}

    matchings = set([])

    # find all possible matchings: improve this code if necessary

    for edge_set in mitl.powerset(G.edges()):
        if nx.algorithms.matching.is_matching(G, edge_set):
            matchings.add(tuple([edge_labels[e] for e in edge_set]))

    maximal_matchings = set()
    for match in matchings:
        to_remove = set()
        for max_match in maximal_matchings:
            if set(match).issubset(set(max_match)): break
            if set(max_match).issubset(set(match)): to_remove.add(max_match)
        else:
            maximal_matchings.add(match)

        maximal_matchings = maximal_matchings - to_remove

    return maximal_matchings
Beispiel #9
0
def power_list(data):
    """
	:param data: an iterable
	:return: power set of the data as a list (excluding the empty list).
	:rtype: list

	>>> l = [1, 2, 3]
	>>> power_list(l)
	[(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]

	>>> for x in power_list([(0.0, 2.0), (4.0, 5.5), (6.0, 7.25)]):
	...     print(x)
	((0.0, 2.0),)
	((4.0, 5.5),)
	((6.0, 7.25),)
	((0.0, 2.0), (4.0, 5.5))
	((0.0, 2.0), (6.0, 7.25))
	((4.0, 5.5), (6.0, 7.25))
	((0.0, 2.0), (4.0, 5.5), (6.0, 7.25))
	"""
    assert type(data) == list
    power_list = powerset(data)
    return [x for x in power_list if len(x) != 0]
Beispiel #10
0
def FindIntegrableConditions(S, context):
    result = list(S)
    if len(result) == 1:
        return []
    vars = list(range(len(context._independent)))
    monomials = [(_, derivative_to_vec(_.Lder(), context)) for _ in result]

    ms = tuple([_[1] for _ in monomials])
    m0 = []

    def map_old_to_new(l):
        return context._independent[vars.index(l)]

    # multiplier-collection is our M
    multiplier_collection = []
    for dp, monom in monomials:
        # S1
        # damned! Variables are messed up!
        _multipliers, _nonmultipliers = vec_multipliers(monom, ms, vars)
        multiplier_collection.append(
            (dp, [map_old_to_new(_) for _ in _multipliers],
             [map_old_to_new(_) for _ in _nonmultipliers]))
    result = []
    for e1, e2 in product(multiplier_collection, repeat=2):
        if e1 == e2: continue
        for n in e1[2]:
            for m in islice(powerset(e2[1]), 1, None):
                if eq(adiff(e1[0].Lder(), context, n),
                      adiff(e2[0].Lder(), context, *m)):
                    # integrability condition
                    # don't need leading coefficients because in DPs
                    # it is always 1
                    c = adiff(e1[0].expression(), context, n) - \
                        adiff(e2[0].expression(), context, *m)
                    result.append(c)
    return result
Beispiel #11
0
 def test_combinatorics(self):
     """Ensure a proper enumeration"""
     p = mi.powerset([1, 2, 3])
     self.assertEqual(list(p), [(), (1, ), (2, ), (3, ), (1, 2), (1, 3),
                                (2, 3), (1, 2, 3)])
from more_itertools import powerset
from numpy import ediff1d

def ps_sorter(tup):
    l = len(tup)
    d = ediff1d(tup).tolist()
    return l, d

ps = powerset([1,2,3,4])

ps = sorted(ps, key=ps_sorter)

for x in ps:
    print(x)
Beispiel #13
0
 def get_states(self):
     """Return list of all states. A state has the format (x, (a, b)) where x is the index of the current position
     and (a, b) a sorted tuple of the pick position that have been visited"""
     return [(cor, tuple(sorted(ppc))) for cor in self.corridors
             for ppc in powerset(self.pick_pts)]
Beispiel #14
0
def main() -> None:
    lines = aoc.get_lines(14)
    memory: Dict[int, int] = {}

    mask1s = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000
    mask0s = 0b_1111_1111_1111_1111_1111_1111_1111_1111_1111

    line_regex = re.compile(r"^([a-z]+)(?:\[(.+?)\])? = (.+)$")

    for line in lines:
        command, argument, value = line_regex.match(line).groups()
        if command == "mem":
            memory[int(argument)] = (int(value) & mask0s) | mask1s
        elif command == "mask":
            mask0s = 0
            mask1s = 0
            for character in value:
                mask0s <<= 1
                mask1s <<= 1
                if character == "X":
                    mask0s |= 1
                    mask1s |= 0
                elif character == "1":
                    mask0s |= 1
                    mask1s |= 1
                elif character == "0":
                    mask0s |= 0
                    mask1s |= 0
                else:
                    assert False, f"Invalid character {character} in mask"
        else:
            assert False, f"Invalid command {command}"
    print(sum(memory.values()))

    ############################################################################

    mask1s = 0b_0000_0000_0000_0000_0000_0000_0000_0000_0000
    mask0s = 0b_1111_1111_1111_1111_1111_1111_1111_1111_1111
    floats: List[int] = []
    memory = {}

    for line in lines:
        command, argument, value = line_regex.match(line).groups()
        if command == "mem":
            write_value = int(value)
            address = int(argument)
            newline = "\n"
            for floating_masks in more_itertools.powerset(floats):
                mask = reduce(lambda a, b: a | b, floating_masks, 0)
                local_address = ((int(argument) & mask0s) | mask1s) | mask
                memory[local_address] = write_value
        elif command == "mask":
            mask0s = 0
            mask1s = 0
            floats = []
            float_index = 0b_1_0000_0000_0000_0000_0000_0000_0000_0000_0000
            for character in value:
                mask0s <<= 1
                mask1s <<= 1
                float_index >>= 1
                if character == "X":
                    mask0s |= 0
                    mask1s |= 0
                    floats.append(float_index)
                elif character == "1":
                    mask0s |= 1
                    mask1s |= 1
                elif character == "0":
                    mask0s |= 1
                    mask1s |= 0
                else:
                    assert False, f"Invalid character {character} in mask"
        else:
            assert False, f"Invalid command {command}"
    print(sum(memory.values()))
Beispiel #15
0
 def all_coalitions(self):
     return powerset(range(self.N))
Beispiel #16
0
        b |= ormask
        a = int(a[4:-1])
        memory[a] = b
print(sum(memory.values()))
memory = dict()
for line in program:
    a, b = line.split(' = ')
    if a == 'mask':
        ormask, andmask = 0, 0
        maskstr = b[::-1]
        for c in range(36):
            if maskstr[c] == '1':
                ormask |= 2**c
            elif maskstr[c] == 'X':
                andmask ^= 2**c
    else:
        a = int(a[4:-1])
        a |= ormask
        a &= ~andmask
        bitlist = []
        for c in range(36):
            if maskstr[c] == 'X':
                bitlist.append(c)
        masks = [
            sum(map(lambda x: 2**x, s))
            for s in more_itertools.powerset(bitlist)
        ]
        for addr in [a | mask for mask in masks]:
            memory[addr] = int(b)
print(sum(memory.values()))
Beispiel #17
0
 def test_empty(self):
     p = list(mi.powerset([]))
     self.assertEqual(p, [()])
Beispiel #18
0
def general_cluster_statmech_model(regObj,
                                   lowAffinityOcc=None,
                                   conc=16,
                                   consensusKd=16,
                                   concO=0.6):
    """
    This method takes as input a regulatory element (module) object of binding sites 
    with their different properties (spacing, scores, etc.), as well as a concentration parameter,
    and returns the mean occupancy of transcription factor to the binding sites. It 
    works by assuming that non-overlapping sites bind transcription factor independently,
    and it deals with dependent (overlapping) binding sites by first identifying 
    all of the possible bound states of the system, followed by determining the 
    weighted multiplicities (relative to unbound state) based on Philips PBOC 2nd edition. 
    Then the mean occ. is calculated as a weighted average of the probability of occupancy of 
    each state (ratio of the state's rel. weighted multiplicity to the partition function (also
    rel.), and by the number of transcription factors that are bound in that state. 
    
    Based on PBOC - 2nd, p.270, equation 6.112:
    delE (change in energy with binding, relative to solution) in units Boltzmann constant*T (1/beta) = ln(Kd/concO)
    When calculating occupancy, I first convert Kds into this delE value in terms of Kb*T
    
    lowAffinityOcc: float
        If specified, this means we only care about the mean occupancy coming from low-affinity sites,
        which have Kd values above the threshold ratio stored in this variable. In order to implement
        this, for independent binding sites, we skip the occupancy contribution if the site's Kd is 
        below this threshold. For overlapping clusters, we multiply each state's weighted rel. mult.
        by the number of low-affinity binding sites instead of the total number of binding sites, before
        dividing by the partition function.
    
    Returns
    -------
    Mean occupancy of the input regulatory module.

    """

    aggOcc = 0  # aggregate occupancy, which will accumulate independently from
    # the modules (single sites and overlapping clusters)

    # iterate over spacings list, separate into different cases if it's an
    # overlapping site (dependent) or non-overlapping site (independent)

    i = 0

    while i < len(regObj.startPos):

        # -------------- if it's a non-overlapping site, just assume independent binding and add its occupancy to aggOcc
        if i == len(
                regObj.spacing
        ):  # account for the last site (note: we'd only arrive here if it's not part of an ovlp cluster (but can't check spacing),
            # so it's fine not to check, and just assume non-overlapping)

            if lowAffinityOcc:  # see parameter definition

                if regObj.siteAffinities[i] >= lowAffinityOcc:

                    fracOcc = single_site_statmech_model(
                        regObj.siteAffinities[i], conc, consensusKd, concO)
                    aggOcc = aggOcc + fracOcc

            else:  # note: this gets skipped if lowAffinityOcc=True, but site affinity < lowAffinityOcc, and therefore higher affinity non-ovlp sites are not added

                fracOcc = single_site_statmech_model(regObj.siteAffinities[i],
                                                     conc, consensusKd, concO)
                aggOcc = aggOcc + fracOcc

        elif regObj.spacing[
                i] >= 0:  # this is only fine if never end here with index corresponding to the final member of an ovlp cluster (else it would trigger)

            if lowAffinityOcc:

                if regObj.siteAffinities[i] >= lowAffinityOcc:

                    fracOcc = single_site_statmech_model(
                        regObj.siteAffinities[i], conc, consensusKd, concO)
                    aggOcc = aggOcc + fracOcc

            else:
                fracOcc = single_site_statmech_model(regObj.siteAffinities[i],
                                                     conc, consensusKd, concO)
                aggOcc = aggOcc + fracOcc

        else:  # -------------- must be an overlapping site (negative spacing), note that an ovlp cluster can't start on the last site

            start = i

            allCombosList = [
            ]  # list of tuples of all combos of sites (powerset)
            eachSitesOverlaps = [
            ]  # list of lists (one corresponding to each site), identifying which sites overlap

            while regObj.spacing[
                    i] < 0:  #find the last site in the overlapping cluster
                i += 1

                if i == len(
                        regObj.spacing
                ):  #if we're on the last member of both the ovlp cluster and also the entire reg element
                    break

            end = i
            #identify all binding sites that overlap with each binding site (create a separate list for each site), and append these to eachSitesOverlaps
            for j in range(start,
                           end + 1):  #for a given site j in the ovlp cluster

                tempList = []

                for k in range(start,
                               end + 1):  #do the other sites k in the cluster

                    if k is not j:  #ignoring that site j itself

                        if (k < j and regObj.endPos[k] >= regObj.startPos[j]
                            ) or (k > j and regObj.startPos[k] <=
                                  regObj.endPos[j]):  #overlap with site j?

                            tempList.append(
                                k
                            )  #for each site j, append all sites k that do overlap, into tempList

                if tempList:
                    eachSitesOverlaps.append(
                        tempList
                    )  #then append this list of sites overlapping with j to eachSitesOverlaps, and move to next site j+1

            allCombosList = list(
                powerset(np.arange(start, end + 1))
            )  #generate all possible combinations of sites (including states impossible due to binding exclusivity)
            possibleStates = restrict_to_possible_states(
                allCombosList, eachSitesOverlaps
            )  #list of possible states (sites that con be concurrently occupied in the ovlp cluster)
            # iterate over possible states and generate a relative boltzmann-weighted multiplicity for each, by summing the delta binding energies (relative to Esol)
            aggWeightedMeanOcc = 0  # Accumulate the numerator in the: weighted average (Pocc) mean occupancy calculation  (note that unbound state is multiplied by 0 sites)
            aggPartitionFunction = 0  # Accumulate the denominator (note that the unbound state results in +1)

            for y in range(len(possibleStates)):

                aggDelE = 0
                if lowAffinityOcc:
                    numLowAffSites = 0
                numSites = len(possibleStates[y])

                for z in range(numSites):

                    siteIndex = possibleStates[y][
                        z]  # index (in regEle object) of current site in current binding state
                    aggDelE = aggDelE + math.log(
                        regObj.siteAffinities[siteIndex] * consensusKd / concO
                    )  # iterate and sum the deltaEnergy terms for that state

                    if lowAffinityOcc and (regObj.siteAffinities[siteIndex] >=
                                           lowAffinityOcc):
                        numLowAffSites += 1

                relMultOvlp = ((conc / concO)**numSites) * math.exp(
                    -aggDelE
                )  # calculate the weighted relative multiplicity term for that state (see PBOC or notes for derivation)

                aggPartitionFunction = aggPartitionFunction + relMultOvlp

                if lowAffinityOcc:
                    aggWeightedMeanOcc = aggWeightedMeanOcc + numLowAffSites * relMultOvlp
                else:
                    aggWeightedMeanOcc = aggWeightedMeanOcc + numSites * relMultOvlp

            meanOcc = aggWeightedMeanOcc / aggPartitionFunction  # do the mean occupancy calculation

            aggOcc = aggOcc + meanOcc  # add the occupancy from the ovlp cluster to the occupancy for the overall regulatory element

        # -------------- after we're done with the case (non-ovlp site or ovlp cluster), move on to the next site
        i += 1

    return aggOcc
 def get_states(self):
     """Return list of all states. A state has the format (x, (a, b)) where x is the index of the current position
     and (a, b) a sorted tuple of the pick positions that have been visited"""
     return [(*cc, tuple(sorted(ppc)))
             for cc in product(self.corridors, repeat=self.n_agents)
             for ppc in powerset(self.pick_pts)]
Beispiel #20
0
 None:
 lambda s, _: [{}],
 jax._src.lax.lax.copy_p:
 lambda s, _: [{}],
 ad.zeros_like_p:
 lambda s, _: [{}],
 lax.neg_p:
 lambda s, _: [{}],
 lax.transpose_p:
 lambda s, _: [{
     'permutation': p
 } for p in itertools.permutations(range(len(s)))],
 lax.reduce_sum_p:
 lambda s, _: [{
     'axes': p
 } for p in more_itertools.powerset(range(len(s)))],
 lax.reduce_window_sum_p:
 lambda s, _: [{
     'base_dilation': b_d,
     'padding': p,
     'window_dilation': w_dl,
     'window_dimensions': w_dd,
     'window_strides': w_s
 } for b_d in _hypercube(len(s))[:3]
               for p in map(tuple, ([[]] if len(s) == 0 else [
                   [(0, 0) for _ in range(len(s))],
                   [(i, i // 2 + 1) for i in range(len(s))],
                   [(i // 2 + 1, i) for i in range(len(s))],
               ])) for w_dl in _hypercube(len(s))[:3]
               for w_dd in _hypercube(len(s))[:3]
               for w_s in _hypercube(len(s))[:3]],
Beispiel #21
0
from more_itertools import powerset

list(powerset(['col1', 'col2', 'col3']))
Beispiel #22
0
east
take space law space brochure
west
south
east
south
east
take weather machine
west
south
take manifold
west
take mouse
north
north
'''

#Droid(memory).run(initial_commands)

items = {
    command[5:]
    for command in initial_commands.splitlines() if command.startswith('t')
}

commands = initial_commands.splitlines() + [f'drop {item}' for item in items]
for combo in powerset(items):
    additional_commands = [f'take {item}' for item in combo
                           ] + ['east'] + [f'drop {item}' for item in combo]
    commands.extend(additional_commands)
commands = '\n'.join(commands) + '\n'
Droid(memory).run(commands)
Beispiel #23
0
    "toi",
    "gp",
    "icf",
    "g_pp",
    "a1_pp",
    "a2_pp",
    "points_pp",
    "toi_pp",
    "gp_pp",
    "isf_pp",
    "iff_pp",
    "season_next",
    "pos_D",
    "pos_D/F",
    "pos_F",
    "toi_gp",
    "sh_percent",
    "sh_percent_pp",
    "avg_goals_season",
    "avg_sh_perc",
    "sh_perc_diff",
    "g_avg_past_2_seasons",
]

feature_combos = list(more_itertools.powerset(test_cols))

import pickle

with open("column_combos", "wb") as fp:  # Pickling
    pickle.dump(feature_combos, fp)
Beispiel #24
0
def powerset_max_length(candidates, length):
    return filter(lambda s: len(s) <= length, powerset(candidates))
Beispiel #25
0
def train_parallel(env,
                   n_proc,
                   update_interval,
                   cache_size,
                   n_episodes,
                   n_steps,
                   l_rate,
                   d_rate,
                   max_e_rate,
                   min_e_rate,
                   e_d_rate,
                   r_threshold=None):
    """Perform parallel q-learning on env."""
    # Split up environment grid according to number of processes
    corridors_split = np.array_split(env.corridors, n_proc)
    possible_actions_split = list(
        split_into(env.possible_actions, [len(c) for c in corridors_split]))
    states_split = list(
        split_into(env.states, [
            len(c) * len(list(powerset(env.pick_pts))) for c in corridors_split
        ]))

    ray.init()  # start up ray
    main = Main.remote(env=env, n_proc=n_proc,
                       r_threshold=r_threshold)  # define main process
    workers = [
        Worker.options(name="worker" + str(i)).remote(
            env=env,
            states=states_slice,
            possible_actions=actions_slice,
            corridors=corridors_slice,
            id=i,
            grid_size=env.grid_size,
            start_global=env.start,
            pick_pts_global=env.pick_pts,
            main=main,
            update_interval=update_interval,
            cache_size=cache_size,
            n_episodes=n_episodes,
            n_steps=n_steps,
            l_rate=l_rate,
            d_rate=d_rate,
            max_e_rate=max_e_rate,
            min_e_rate=min_e_rate,
            e_d_rate=e_d_rate)
        for i, (states_slice, actions_slice, corridors_slice) in enumerate(
            zip(states_split, possible_actions_split, corridors_split))
    ]  # define all workers
    s = time.time()
    rewards = ray.get([worker.train.remote() for worker in workers
                       ])  # start local training process for all workers
    # Since the workers do not step through the episodes synchronously, the reward value that they received from the
    # global q-table at given update intervals can differ. Therefore, take the maximum value of all workers, as this
    # presents the actual best optimal path at that moment.
    rewards_final = list(map(max, zip(*rewards)))
    exec_time = time.time() - s
    q_table_final = ray.get(
        main.send_q_table.remote())  # get final q-table from main process
    ray.shutdown()  # shutdown ray
    actions_final, _ = q_table_to_action_list(
        q_table_final, env)  # get final action list from q-table
    return actions_final, rewards_final, exec_time
Beispiel #26
0
 def values(self):
     return (set(v) for v in mitt.powerset('abc'))