Ejemplo n.º 1
0
def computePossiblePartitions(matchingPartitions, profsPartitions,
                              maxCoordinates, weights, profsData, lecturesData,
                              absolMin, absolMax):

    c = Coordinates(maxCoordinates, weights)
    idx = 0
    totalMatches = 0
    while (c.maxWeight(idx) >= absolMin):
        while c.up(idx):
            weight = c.weight()
            if absolMin <= weight <= absolMax:
                partition = {"coords": clone(c.coords), "profs": []}
                for prof in profsData:
                    if profsData[prof]["min"] <= weight <= profsData[prof][
                            "max"]:
                        partition["profs"] += [prof]
                        profsPartitions[prof] += [clone(c.coords)]
                        matchingPartitions += [clone(partition)]
                        totalMatches += 1
        idx = c.downMax()
        print("Done with idx =", idx - 1)
        if not idx:
            break
    print("Found", totalMatches, "matches in total")
    for prof in profsData:
        print("   *", len(profsPartitions[prof]), "for", prof)
    print("")
Ejemplo n.º 2
0
    def test_no_mapping(self):
        example = Example()
        assert modify_example(clone(example), dict()) == example

        example = Example("test")
        assert modify_example(clone(example), dict()) == example

        example = Example("test with $ARG")
        assert modify_example(clone(example), dict()) == example
Ejemplo n.º 3
0
    def test_no_replacement(self):
        mapping = {"argument": "replaced"}

        example = Example()
        assert modify_example(clone(example), mapping) == example

        example = Example("test")
        assert modify_example(clone(example), mapping) == example

        example = Example("test with $ARG")
        assert modify_example(clone(example), mapping) == example
Ejemplo n.º 4
0
 def testRotate(o): #ADD: check actual point values after a rotation
   a = getBlock('Z').shift(7,7)
   b = clone(a).rotate(clockwise).rotate(clockwise).rotate(clockwise)
   c = clone(a).rotate(counterClockwise)
   d = clone(a).rotate(clockwise).rotate(clockwise) \
               .rotate(clockwise).rotate(clockwise)
   e = clone(a).rotate(counterClockwise).rotate(clockwise)
   o._checkRotate(b,c)
   o._checkRotate(a,d)
   o._checkRotate(a,e)
   o.failUnless(rotBlock(a,4).rotation == 0 and rotBlock(a,1).rotation == 1
            and rotBlock(a,2).rotation == 2 and rotBlock(a,3).rotation == 3)
Ejemplo n.º 5
0
    def collect_and_upload_data(self):
        # build the query/url
        query = "%s?begin=%s&end=%s&period=%d" % (
            localserver, self.start_datetime.strftime("%d%m%Y%H%M%S"),
            self.end_datetime.strftime("%d%m%Y%H%M%S"), update_interval * 60)
        # add all the variable names, we need to calculate our target variables
        for each in self.config.dependents.keys():
            query += "&var=%s.%s" % (self.config.source, each)
        # make the request
        response = requests.get(query)
        # log
        self.logger.log("data collected")

        # parse the response data to records
        # each record has different timestamp than others
        records = re.split("(?:</record>)*(?:<record>)", response.text)
        # remove the first one from the stack
        # the first one contains no record but xml info.
        records.pop(0)

        # process each record
        for record in records:
            if self.stop:
                break
            # make a copy of target and dependents
            targets = clone(self.config.targets)
            dependents = clone(self.config.dependents)
            # extract datetime from record
            rid = re.search(r"<dateTime>(\d+)", record).group(1)
            # format datetime to match server/database format
            rid = datetime.datetime.strptime(
                rid, "%d%m%Y%H%M%S").strftime("%Y-%m-%d %H:%M:%S")
            # find all the values we requested
            matches = re.findall(r"(\w+)</id><value>([^<]+)", record)
            # convert the values to number and index/save them
            for match in matches:
                dependents[match[0]] = round(float(match[1]), 6)

            # calculate target variables using values from dependents variables
            for target in targets:
                for i in range(len(targets[target])):
                    targets[target][i] = dependents[targets[target][i]]
                targets[target] = sum(targets[target])
            # upload data
            # if server didnt get the data, abort
            # aborting is imprtant. this way we wont
            # have missing data in the middle
            # rest of the data will be uploaded in
            # the beginning of next transmission
            ack = self.upload_data(rid, targets)
            if not ack:
                break
Ejemplo n.º 6
0
def updateData(profsDataRaw, lecturesDataRaw):
    """ Update data from teachers' wishes """
    profsData = clone(profsDataRaw)
    lecturesData = clone(lecturesDataRaw)
    for prof in profsDataRaw:
        if profsDataRaw[prof]["wish"]:
            for section in profsDataRaw[prof]["wish"]:
                durationOfThis = findAndPop(lecturesData, section)
                profsData[prof]["min"] = reround(profsData[prof]["min"] -
                                                 durationOfThis)
                profsData[prof]["max"] = reround(profsData[prof]["max"] -
                                                 durationOfThis)
    return [profsData, lecturesData]
Ejemplo n.º 7
0
 def new_random_state(self):  # will be used in random restart and genetic
     new_state = clone(self)
     # must clone last graph,otherwise our source graph will change,because it contains pointers to nodes,and we are
     # -changing that same nodes colour
     for node in new_state.graphStructure:
         node.colour = randint(0, 3)
     return new_state
Ejemplo n.º 8
0
 def __call__(self, value):
     """When invoked, this will pass the given value through the configured set of transformations"""
     result = clone(value)
     for fn in reversed(self.__chain):
         result = fn(result)
     result["__raw"] = dumps(value)
     return result
Ejemplo n.º 9
0
 def test_str(self):
     graph = Graph(clone(MINIMAL))
     s = str(graph)
     self.assertEqual(
         s,
         '\n[section1]\nproperty1=10\nproperty2=http://redhat.com\nproperty3=howdy'
     )
Ejemplo n.º 10
0
    def pattern(self):
        def jn(trps, acc, ret):
            if not trps:
                ret.append(acc)
                return ret
            trp = trps.pop(0)
            if not acc:
                return jn(trps, [trp._from, trp._edge, ">", trp._to], ret)
            else:
                if trp._from == acc[0]:
                    acc[0:0] = [trp._to, "<", trp._edge]
                elif trp._from == acc[-1]:
                    acc.extend([trp._edge, ">", trp._to])
                elif trp._to == acc[0]:
                    acc[0:0] = [trp._from, trp._edge, ">"]
                elif trp._to == acc[-1]:
                    acc.extend(["<", trp._edge, trp._from])
                else:
                    ret.append(acc)
                    acc = [trp._from, trp._edge, ">", trp._to]
                return jn(trps, acc, ret)
            pass

        if not self._pattern:
            trps = clone(self.triples)
            grps = jn(trps, [], [])
            pats = []
            for grp in grps:
                pats.append("".join([
                    x.pattern() if not isinstance(x, str) else x for x in grp
                ]))
            self._pattern = ", ".join(pats)
        return self._pattern
Ejemplo n.º 11
0
    def run(self):
        if self.mct == None:
            # if new tree then initialize that it came from the opposite player
            self.mct = Tree(switch_turns(self.turn))
        else:
            # if the tree exists look for the node if it contains that
            children = self.mct.get_root().get_children()
            if len(children) != 0:
                contained = False
                node = children[0]
                for n in children:
                    if n.get_move() == self._cloned_board.get_previous_move():
                        contained = True
                        node = n
                if contained:
                    self.mct.set_root(node)
                else:
                    # otherwise initialize new tree
                    self.mct = Tree(switch_turns(self.turn))
            else:
                # otherwise initialize new tree
                self.mct = Tree(switch_turns(self.turn))

        start_time = current_milli_time()
        while current_milli_time() - start_time < self.timeout - self.before:
            self._cloned_board = clone(
                UltimateTicTacToe(board=self.board, last_turn=self.last_turn))
            self.roll_out(self.expansion(self.selection(self.mct.get_root())))
        return self.choose_best_next_move()
Ejemplo n.º 12
0
def _as(ent, alias):
    """Return copy of ent with As alias set."""
    if isinstance(ent, T):
        return ent
    ret = clone(ent)
    ret.As = alias
    return ret
Ejemplo n.º 13
0
 def freeze(self):
     new = SymbolTable(self.scope, self.name, self.block)
     new.frozen = True
     new.local = clone(self.local)
     new.mutables = self.mutables
     new.args = self.args
     return new
Ejemplo n.º 14
0
def _var(ent):
    """Return entity without label or type."""
    ret = None
    if isinstance(ent, (N, R)):
        ret = clone(ent)
        if hasattr(ret, "label"):
            ret.label = None
        if hasattr(ret, "Type"):
            ret.Type = None
    elif isinstance(ent, T):
        ret = clone(ent)
        ret._from.label = None
        ret._to.label = None
        # leave the edge type alone in a triple...
    else:
        return ent
    return ret
Ejemplo n.º 15
0
def _plain(ent):
    """Return entity without properties."""
    ret = None
    if isinstance(ent, (N, R)):
        ret = clone(ent)
        ret.props = {}
    elif isinstance(ent, P):
        ret = clone(ent)
        ret.value = None
    elif isinstance(ent, T):
        ret = clone(ent)
        ret._from.props = {}
        ret._to.props = {}
        ret._edge.props = {}
    else:
        return ent
    return ret
Ejemplo n.º 16
0
    def clone(self):
        other = Schematic(self.name)

        other.blocks = {}

        for name, block in self.blocks.items():
            if hasattr(block, "bmp"):
                bmptemp = block.bmp
                block.bmp = None
                other.blocks[name] = clone(block)
                self.blocks[name].bmp = bmptemp

        other.connectors = clone(self.connectors)
        other.sim_settings = clone(self.sim_settings)
        other.parameters = clone(self.parameters)

        return other
Ejemplo n.º 17
0
    def get_dataset(self,
                    tickers,
                    window_size=200,
                    roi_size=100,
                    start=None,
                    end=None,
                    features=None,
                    minus_features=[]):

        # Default end value is yesterday and no higher
        if end is None:
            end = DataHandler.date_to_key(datetime.datetime.now()) - 1
        elif end > DataHandler.date_to_key(datetime.datetime.now()) - 1:
            raise ValueError(
                "end value must be no higher than yesterday: ",
                DataHandler.date_to_key(datetime.datetime.now()) - 1)

        # Default start value is 2000 days prior to start and no earlier than Jan 1 2005
        if start is None:
            start = end - 2000
            if start < 0:
                start = 0
        elif start < 0:
            raise ValueError("end value must be later than the epoch: 0")

        # Default features value is all features
        if features is None or len(features) == 0:
            features = clone(all_features)
        # Remove duplicates
        features = list(dict.fromkeys(features))
        # Remove requested minus features
        for feature in minus_features:
            if feature in features:
                del features[features.index(feature)]

        # Important values
        length = end - start
        n_windows = math.floor((length - roi_size) / window_size)
        print(length - roi_size, "/", window_size, "=", n_windows)
        n_features = len(features)
        n_instances = len(tickers) * n_windows

        # Create dataset
        data_x = np.empty([n_instances, window_size, n_features])
        data_y = np.empty([n_instances, 1])

        instance_idx = 0
        for ticker in tickers:
            ticker_data = self.get_data_for(ticker, start, end, features)
            for i in range(n_windows):
                data_x[instance_idx] = ticker_data[i * window_size:i *
                                                   window_size + window_size]
                data_y[instance_idx] = self.get_roi(
                    ticker_data, features, i * window_size + window_size,
                    roi_size)
                instance_idx += 1

        return (data_x, data_y)
Ejemplo n.º 18
0
 def test_get(self):
     graph = Graph(clone(MINIMAL))
     section = graph.section1
     # found
     p1 = section.property1
     self.assertEqual(p1, MINIMAL['section1']['property1'])
     # not found
     p1 = section.foobar
     self.assertTrue(p1 is None)
Ejemplo n.º 19
0
	def sendMessage(self, m):
		m.fromDir = m.LEFT
		log.debug("Node %-17s | TRANSMIT | %-17s | to node %-17s" % (self.log(), m.log(), self.left.log()))
		self.sendLeft(m)
		m = clone(m)
		m.fromDir = m.RIGHT
		m.delay = randint(self.minDelay, self.maxDelay)
		log.debug("Node %-17s | TRANSMIT | %-17s | to node %-17s" % (self.log(), m.log(), self.right.log()))
		self.sendRight(m)
Ejemplo n.º 20
0
 def test_get(self):
     graph = Graph(clone(MINIMAL))
     section = graph.section1
     # found
     p1 = section.property1
     self.assertEqual(p1, MINIMAL['section1']['property1'])
     # not found
     p1 = section.foobar
     self.assertTrue(p1 is None)
Ejemplo n.º 21
0
 def possible_next_states(self, only_better_states=True):
     possible_next_states = []
     current_value = self.evaluate_value()
     for i in range(len(self.graphStructure)):
         # we must clone it
         # otherwise we will be changing the source structure for ever and we'll only get one new state at the end
         new_state = clone(self)
         new_state2 = clone(self)
         increase_node_color(new_state.graphStructure[i])
         decrease_node_color(new_state2.graphStructure[i])
         if only_better_states:
             if new_state.evaluate_value() > current_value:
                 possible_next_states.append(new_state)
             if new_state2.evaluate_value() > current_value:
                 possible_next_states.append(new_state2)
         else:
             possible_next_states.extend([new_state, new_state2])
     return possible_next_states
Ejemplo n.º 22
0
 def test_accessing(self):
     graph = Graph(clone(MINIMAL))
     # property: found
     self.assertEqual(graph.section1.property1, '10')
     # property: not found
     self.assertEqual(graph.section1.propertyXX, None)
     self.assertEqual(graph.section10.property1, None)
     # section: found
     self.assertEqual(repr(graph.section1), repr(MINIMAL['section1']))
     # section: not found
     self.assertEqual(repr(graph.section100), repr({}))
Ejemplo n.º 23
0
 def test_accessing(self):
     graph = Graph(clone(MINIMAL))
     # property: found
     self.assertEqual(graph.section1.property1, '10')
     # property: not found
     self.assertEqual(graph.section1.propertyXX, None)
     self.assertEqual(graph.section10.property1, None)
     # section: found
     self.assertEqual(repr(graph.section1), repr(MINIMAL['section1']))
     # section: not found
     self.assertEqual(repr(graph.section100), repr({}))
Ejemplo n.º 24
0
def graph_idfs(graph_city_details_dictionary):
    i = 1
    while True:
        new_graph_city_details_dictionary = clone(
            graph_city_details_dictionary)
        start_node = new_graph_city_details_dictionary["Arad"]
        destination_node = new_graph_city_details_dictionary["Bucharest"]
        graph_ldfs(start_node, destination_node, i)
        if destination_node.parent is not None:
            return new_graph_city_details_dictionary
        else:
            i += 1
Ejemplo n.º 25
0
    def flatten(self):

        """Flattens all the subcircuits recursively to the main subcircuit.
        :return: None
        """

        # now loop through and grab all the subcircuit instances and remove from
        # device list:

        for x_name, x_device in self.devices.items():

            if x_name[0] == "X":  # if subckt instance device:

                subckt_name = x_device.subckt

                if subckt_name in self.subckts:  # if subckt def found:

                    subckt = self.subckts[subckt_name]

                    # loop through the subckt devices, clone them for the
                    # subckt instance, and mangle the names to prevent
                    # collisions:

                    for sub_name, sub_device in subckt.devices.items():
                        mangled_name = "{0}_{1}".format(x_name, sub_name)
                        new_device = clone(sub_device)

                        # replace subckt instance device node names with
                        # external node names & mangle the internal node names:

                        new_device.nodes = list(new_device.nodes)
                        for i, node in enumerate(new_device.nodes):
                            if node == 0 or node == 'ground' or node == 'gnd':
                                new_device.nodes[i] = 0
                            elif node in x_device.port2node:
                                new_device.nodes[i] = x_device.port2node[node]
                            else:
                                mangled_port = "{0}_{1}".format(x_name, node)
                                new_device.nodes[i] = mangled_port

                        # add device to top level:
                        self.device(mangled_name, new_device)

                    # delete the X device (which has now been replaced with
                    # actual device instances and is no longer needed):

                    del self.devices[x_name]

                else:  # if subckt definition not found:

                    msg = "Subcircuit {0} not defined for device {1}."
                    msg.format(x_device.subckt, x_name)
                    raise SubCircuitError(msg)
Ejemplo n.º 26
0
 def test_strict_get(self):
     graph = Graph(clone(MINIMAL), strict=True)
     section = graph.section1
     # found
     p1 = section.property1
     self.assertEqual(p1, MINIMAL['section1']['property1'])
     # not found
     try:
         unused = section.foobar
         self.assertTrue(False, msg='AttributeError not raised')
     except AttributeError:
         pass
Ejemplo n.º 27
0
 def test_strict_get(self):
     graph = Graph(clone(MINIMAL), strict=True)
     section = graph.section1
     # found
     p1 = section.property1
     self.assertEqual(p1, MINIMAL['section1']['property1'])
     # not found
     try:
         unused = section.foobar
         self.assertTrue(False, msg='AttributeError not raised')
     except AttributeError:
         pass
Ejemplo n.º 28
0
def execute_method(node, args=None):
    global LAST_EVALUATED, LAST_RETURNED, FROZEN_TABLES, CALL_STACK
    definition = node
    if not isinstance(node, (Definition, function)):
        definition = evaluate(node.value)

    if type(definition) is function:
        return definition(node)

    if type(definition) is not Definition:
        loc = None
        if is_node(definition):
            loc = node.value.location
        else:
            loc = CURRENT_LOCATION

        return EX.throw(
            loc,
            'Cannot make call to to type of `{}\''.format(to_type(definition)))

    if definition is None:
        return EX.throw(CURRENT_LOCATION, 'Cannot make empty call.')

    if args is None:
        args = list(map(evaluate, node.operands))
    else:
        print("Artificial args")
    FROZEN_TABLES = definition.frozen

    added = definition.table.scope == CURRENT_SCOPES[-1]
    if not added:
        CURRENT_SCOPES.append(definition.table.scope)

    call_table = clone(definition.table)
    call_table.clean()
    call_table.give_args(args)
    CALL_STACK.append(call_table)

    # Aaaaand, then we finally, make the call...
    result = definition.call()
    # Back to cleaning up our Symbol tables.

    definition.table.clean()
    FROZEN_TABLES = []
    if not added:
        CURRENT_SCOPES.pop()
    CALL_STACK[-1].clean()
    CALL_STACK.pop()
    del call_table

    LAST_EVALUATED = result
    LAST_RETURNED = LAST_EVALUATED
    return LAST_RETURNED
Ejemplo n.º 29
0
    def withTitle(self, title):
        """
        Creates a copy of the menu with a different title.

        Args:
            title (str): Title

        Returns:
            Menu: Cloned Menu with a different title.
        """
        newme = clone(self)
        newme.setTitle(title)
        return newme
Ejemplo n.º 30
0
 def begin(self):
     self.state = self.INITIATOR
     if self.state == StageNode.INITIATOR: # For Initiators, need process first compare here or it will miss these messages
         self.state = StageNode.CANDIDATE
         #print("Initiator Node %d has changed to Candidate." % self.id)
         log.debug("Initiator Node %d has changed to Candidate." % self.id)
     m = StageMessage()
     m.id = self.id
     m.type = StageMessage.INFO
     m.delay = randint(StageNode.minDelay, StageNode.maxDelay)
     self.sendRight(m)
     message = clone(m)
     message.dir = 3
     self.sendStageLeft(message)
Ejemplo n.º 31
0
def colorMap(nodes):
    nodes = sorted(nodes)
    for node in nodes:
        if node.color is not None:
            break
        colors = sample(node.colors, k=len(node.colors))
        node.updatePossibleColors()
        for color in colors:
            node.setColor(color)
            res = colorMap(clone(nodes))
            if res is not None:
                return res
            node.color = None
    return None
Ejemplo n.º 32
0
def getRouletteWheel(population, SCORES, scorefunc, scoreparams):
	""" Return a fitness-proportional roulette wheel for the population"""
	from population import functify
	wheel={}
	top = 0.0
	for p in population:
		if functify(p) not in SCORES: SCORES[functify(p)] = scorefunc(p, *scoreparams)
	totalscore = abs(sum((SCORES[functify(p)] for p in population)))
	
	for p in population:
		fit = SCORES[functify(p)]/totalscore
		wheel[(top, top+fit)] = clone(p)
		top += fit
	
#	print 'returning wheel'	##
	return wheel
Ejemplo n.º 33
0
 def __init__(self,
              board,
              last_turn,
              turn=Turns.X.value,
              timeout=100,
              before=1):
     self._cloned_board = UltimateTicTacToe(board=board,
                                            last_turn=last_turn)
     self._moves = self._cloned_board.get_free_moves()
     games = []
     for move in self._moves:
         _cloned_board = clone(self._cloned_board)
         board = _cloned_board.move(turn, *move)
         if board:
             board_list = _cloned_board.get_board_list()
             games.append(
                 (board_list, last_turn, turn, timeout, before, move))
     self.games = games
Ejemplo n.º 34
0
 def test_strict_accessing(self):
     graph = Graph(clone(MINIMAL), strict=True)
     # property: found
     self.assertEqual(graph.section1.property1, '10')
     # property: not found
     try:
         unused = graph.section1.propertyXX
         self.assertTrue(False, msg='AttributeError not raised')
     except AttributeError:
         pass
     # section: found
     self.assertEqual(repr(graph.section1), repr(MINIMAL['section1']))
     # section: not found
     try:
         unused = graph.section100
         self.assertTrue(False, msg='AttributeError not raised')
     except AttributeError:
         pass
Ejemplo n.º 35
0
 def test_strict_accessing(self):
     graph = Graph(clone(MINIMAL), strict=True)
     # property: found
     self.assertEqual(graph.section1.property1, '10')
     # property: not found
     try:
         unused = graph.section1.propertyXX
         self.assertTrue(False, msg='AttributeError not raised')
     except AttributeError:
         pass
     # section: found
     self.assertEqual(repr(graph.section1), repr(MINIMAL['section1']))
     # section: not found
     try:
         unused = graph.section100
         self.assertTrue(False, msg='AttributeError not raised')
     except AttributeError:
         pass
Ejemplo n.º 36
0
    def EVCentrality(self, maxit=100):
        """
            returns a dict of all Eigen Vector Centrality values
        """
        nodes = self.getNodes()
        V = defaultdict(lambda: 1)

        for i in range(0, maxit):
            W = defaultdict(lambda: 0)
            for node in nodes:
                for neighbour in self.next(node):
                    W[neighbour] = W[neighbour] + V[node]
            V = clone(W)
        S = 0
        for _, i in V.items():
            S += i
        for key, value in V.items():
            V[key] = float(value) / float(S)

        return V
Ejemplo n.º 37
0
def __crossover(first_chromosome, second_chromosome):
    child_chromosome = clone(
        first_chromosome)  # first initiate the child chromosome values
    child_chromosome_similarity_to_first_chromosome = randint(
        0, 10)  # child's possible similarity to first parent
    for i in range(len(child_chromosome.graphStructure)
                   ):  # or len(parents_chromosome.graphStructure)
        # skip parent's common value
        if first_chromosome.graphStructure[
                i].colour == second_chromosome.graphStructure[i].colour:
            continue
        # child must have some of non common value from both parent's
        else:
            winner_parent = __apply_child_similarity_dosage(
                child_chromosome_similarity_to_first_chromosome)
            if winner_parent == 1:
                continue
            else:
                child_chromosome.graphStructure[
                    i].colour = second_chromosome.graphStructure[i].colour
    return child_chromosome
Ejemplo n.º 38
0
def get_attr_to_suggest(dict_attr_from_graph):
    random.shuffle(mining_results)
    max_num_match_attr = 0
    chosen_key, chosen_val = None, None
    used_attr = None
    used_rules = None
    for dct in mining_results:
        mining_dict = clone(dct)
        is_suitable = False
        current_num_match_attr = 0
        temp_attr = dict()
        mining_keys = list(mining_dict.keys())
        random.shuffle(mining_keys)
        for mining_key in mining_keys:
            mining_val = mining_dict[mining_key]
            if mining_key in dict_attr_from_graph.keys():
                graph_val = dict_attr_from_graph[mining_key]
                if (isinstance(graph_val, list) and mining_val in graph_val) or mining_val==graph_val:
                    temp_attr[mining_key] = mining_val
                    current_num_match_attr +=1
                    is_suitable = True
                mining_dict.pop(mining_key)
        mining_dict_keys = set(mining_dict.keys()).difference(set(dict_attr_from_graph['ignore_attr']))
        if is_suitable and len(mining_dict_keys) > 0 and current_num_match_attr > max_num_match_attr:
            if len(mining_dict_keys)==1 and list(mining_dict_keys)[0]=="potential" and mining_dict["potential"] not in MAPPING_POTENTIAL.keys():
                continue
            while (True):
                chosen_key = random.choice(list(mining_dict_keys))           
                chosen_val = mining_dict[chosen_key]
                if not chosen_key=="potential" or chosen_val in MAPPING_POTENTIAL.keys():
                    break
            max_num_match_attr = current_num_match_attr
            used_attr = temp_attr   
            used_rules = dct
    logger.info('\n     MINING-SUGGESTOR:\nRULES: ' + str(used_rules) + "\nUSED ATTR: " + str(used_attr) + "\nCHOSEN: " + str({chosen_key: chosen_val}))
    return used_rules, used_attr, chosen_key, chosen_val    
Ejemplo n.º 39
0
 def roll_out(self, node):
     # second approach, use neural net to predict
     old_board = self._cloned_board.get_board_list()
     if not self._cloned_board.is_game_done():
         move = choice(self._cloned_board.get_free_moves())
         new_board = clone(old_board)
         new_board[move[0]][move[1]] = 1
         feature = np.concatenate(
             (old_board.flatten(), new_board.flatten()),
             axis=0).reshape(18, 9, 1)
         score = self.nn.predict(np.array([feature]))[0]
         if score[0] > score[1]:
             self.backpropogate(GameState.WIN, node)
         elif score[1] > score[0]:
             self.backpropogate(GameState.LOSE, node)
         else:
             self.backpropogate(GameState.DRAW, node)
     else:
         if self._cloned_board.get_winner() == None:
             self.backpropogate(GameState.DRAW, node)
         else:
             self.backpropogate(
                 GameState.WIN if node.get_turn() == Turns.X.value else
                 GameState.LOSE, node)
Ejemplo n.º 40
0
 def test_iter(self):
     graph = Graph(clone(MINIMAL))
     properties = list(graph.section1)
     self.assertEqual(properties, MINIMAL['section1'].items())
Ejemplo n.º 41
0
 def test_repr(self):
     graph = Graph(clone(MINIMAL))
     s = repr(graph.section1)
     self.assertEqual(s, repr(MINIMAL['section1']))
Ejemplo n.º 42
0
 def test_str(self):
     graph = Graph(clone(MINIMAL))
     s = str(graph.section1)
     self.assertEqual(s, 'property1=10\nproperty2=http://redhat.com\nproperty3=howdy')
Ejemplo n.º 43
0
 def __add__(self, r):
     ''' new sum dict '''
     sum = clone(self)
     sum.update(r)
     return sum
Ejemplo n.º 44
0
 def copy(self):
     ''' dict deep copy '''
     return clone(self)
Ejemplo n.º 45
0
 def test_set(self):
     graph = Graph(clone(MINIMAL))
     section = graph.section1
     self.assertTrue(section.thing is None)
     section.thing = 100
     self.assertEqual(section.thing, 100)
Ejemplo n.º 46
0
 def test_repr(self):
     graph = Graph(clone(MINIMAL))
     s = repr(graph)
     self.assertEqual(s, repr(MINIMAL))
Ejemplo n.º 47
0
 def test_unicode(self):
     graph = Graph(clone(MINIMAL))
     s = unicode(graph)
     self.assertEqual(s, '\n[section1]\nproperty1=10\nproperty2=http://redhat.com\nproperty3=howdy')
Ejemplo n.º 48
0
 def test_iter(self):
     graph = Graph(clone(VALID))
     sections = list(graph)
     self.assertEqual(len(sections), len(VALID))
     self.assertEqual(sections[0], (VALID.items()[0]))
     self.assertEqual(sections[1], (VALID.items()[1]))
Ejemplo n.º 49
0
def getBlock(kind): return clone(tstBlk[kind])
def rotBlock(block,rotations):
Ejemplo n.º 50
0
 def __add__(self, r):
     """ new sum dict """
     sum = clone(self)
     sum.update(r)
     return sum
Ejemplo n.º 51
0
 def copy(self):
     """ dict deep copy """
     return clone(self)
#def reroute((V,E), paths, a, u, n):
#	''' Reroute agent a through node n to node u'''
#	best = '', None	# cost, path
#	for r in n.residents:
#		path = paths[r]
#		cost = sum((e.weight for e in path)) + min((e.weight for e in E if (u in e and n in e) ))
#		if cost < best[0]:
#			best = cost, path+[min((e for e in E if (u in e and n in e) ), key=lambda x : x.weight)]
#		
#		paths[a] = best[1]
#	return (V,E), paths, a, u, n

def getNeighbor((VV,EE), paths, dist, rerouts):
	""" rerouts = [(terminal node, rerouted through)]"""
#	print 'getNeighbor'	##
	V,E = clone(VV), clone(EE)
	u, n = None, None
	while u is None:
		try:
			us = [v for v in V if v.residents and (rerouts and v.id not in [r[0] for r in rerouts]) and \
															any( (e for e in E if v in e and e.capacity==inf and \
															routable(e.src if v==e.dest else e.dest, paths) and \
															e.weight <= dist) ) ]
			
			U = max(paths, key=lambda a: sum((e.weight for e in paths[a])) )
			for v in V:
				if U in v.residents:
					u = v
					break
			else:
				u = choose(us)
Ejemplo n.º 53
0
def rotBlock(block,rotations):
  b = clone(block)
  for x in range(rotations): b.rotate(clockwise)
  return b
Ejemplo n.º 54
0
    def processMessage(self:Node, message:StageMessage): # message can be from left or right
        #log.debug("Node %17s | PROCESS  | %-17s" %(self.log(), message.log()))  ##

        #print("-" * 60)
        #print("Message queue for node %s" % self.log())
        #print("-" * 30 + "Right")
        #for m in self.messageQueue:
        #    print(m.log())
        #print("-" * 30 + "Left")
        #for mLeft in self.messageQueueLeft:
        #    print(mLeft.log())
        #print("-" * 30)

        if self.state != StageNode.LEADER and message.type == message.LEADER: #if Non-Leader received LEADER's msg
            #print("Node id " + str(self.id) + " is processing Leader's notice! ")
            log.debug("Node id " + str(self.id) + " is processing Leader's notice! ")
            self.messageQueue = []
            self.messageQueueLeft = []
            self.state = self.FOLLOWER
            #log.info("Node %17s | TRANSMIT | %-17s | to Node %03d" % (self.log(), message.log(), self.right.id))  ##
            self.sendRight(message) # Leader notification forward

        elif self.state == StageNode.CANDIDATE: # candidates processing # algorithm terminates,
            if (message):
                if message.id == self.id and message.stage == self.stage:
                    if message.type != message.LEADER: #Check the termination condition
                        self.state = self.LEADER
                        self.messageQueue = []
                        self.messageQueueLeft = []

                        m = StageMessage()
                        m.type = m.LEADER
                        m.id = self.id
                        m.delay = randint(StageNode.minDelay, StageNode.maxDelay)
                        m.stage = self.stage
                        #log.info("Node %17s | TRANSMIT | %-17s | to Right Node %03d" % (self.log(), m.log(), self.right.id))  ##
                        #print("Node id " + str(self.id) + " is Leader!")
                        log.debug("Node id " + str(self.id) + " is Leader!")
                        self.sendRight(m) # Leader notification

                    else: # clean msg queue for leader notification back to leader node
                        self.messageQueue = []
                        self.messageQueueLeft = []

            elif (self.messageQueueLeft) and self.messageQueueLeft[0].type != message.LEADER and self.messageQueueLeft[0].id == self.id: #Check the termination condition
                self.state = self.LEADER
                self.messageQueue = []
                self.messageQueueLeft = []

                m = StageMessage()
                m.type = m.LEADER
                m.id = self.id
                m.delay = randint(StageNode.minDelay, StageNode.maxDelay)
                m.stage = self.stage
                #log.info("Node %17s | TRANSMIT | %-17s | to Right Node %03d" % (self.log(), m.log(), self.right.id))  ##
                #print("Node id " + str(self.id) + " is Leader from left!")
                log.debug("Node id " + str(self.id) + " is Leader from left!")
                self.sendRight(m) # Leader notification

            if self.id != message.id and self.id < message.id: # become waiting to process another side, don't need send msg now, set processedMsg ...
                # 3 steps: Pop msg / set node to Waiting / set processedMsg to 0 or 1
                self.state = StageNode.WAITING
                if message.dir == 2:
                    m = self.messageQueue.pop(0)
                    self.processedMsg = 0
                    #print("Candidate Node id " + str(self.id) + " is processing Node " + str(
                    #    message.id) + " and became Waiting! ")
                elif message.dir == 3:
                    m = self.messageQueueLeft.pop(0)
                    self.processedMsg = 1
                    #print("Candidate Node id " + str(self.id) + " is processing Left Node " + str(
                    #    message.id) + " and became Waiting! ")
            elif self.id != message.id: # failed, set state to WAITING for processing.
                # 3 steps: Pop msg / set node to Waiting / set processedMsg to 0 or 1/ set self.fail
                self.fail = True
                self.state = StageNode.WAITING
                if message.dir == 2:
                    m = self.messageQueue.pop(0)
                    self.processedMsg = 0
                    #print("Node id " + str(self.id) + " is failed by Right Node " + str(message.id))
                elif message.dir == 3:
                    m = self.messageQueueLeft.pop(0)
                    self.processedMsg = 1
                    #print("Candidate Node id " + str(self.id) + " is processing Left Node " + str(
                    #    message.id) + " and became Waiting! ")

        elif self.state == StageNode.DEFEATED: # DEFEATED node only forward.
            #print("Defeated Node id " + str(self.id) + " is forwarding! ")
            log.debug("Defeated Node id " + str(self.id) + " is forwarding! ")
            # steps: forward in two sides one time, pop messages which have been processed in 2 queues
            if message.dir == 2: # check if it is sent from right
                m = self.messageQueue.pop(0)
                self.sendRight(message) # forward to right if from my left neighbour
                if self.messageQueueLeft: # sent right, then check left
                    messageLeft = self.messageQueueLeft.pop(0)
                    if messageLeft.dir == 2:
                        self.sendRight(messageLeft)
                    else:
                        self.sendStageLeft(messageLeft)
            else:
                self.sendStageLeft(message) # forward to left if sent from left neighbour
                messageLeft = self.messageQueueLeft.pop(0)
                if self.messageQueue: # sent left, then check right
                    m = self.messageQueue.pop(0)
                    if messageLeft.dir == 2:
                        self.sendRight(m)
                    else:
                        self.sendStageLeft(m)

        elif self.state == StageNode.WAITING: #
            if self.processedMsg == 0: # processed right, message from left, then process
                #remove message.dir ==3 because: elif self.processedMsg == 0 and message.dir ==2: # 2 sent to 3's right before 3 processed previous left msg
                if self.messageQueueLeft:
                    messageLeft = self.messageQueueLeft.pop(0)
                    if (not self.fail) and self.id < messageLeft.id: # waiting processing
                        # steps: set self.state to Candidate / stage + 1 / send msg to 2 sides
                        self.state = StageNode.CANDIDATE
                        self.stage += 1
                        m = StageMessage()
                        m.type = m.INFO #self.CANDIDATE # should set to message's type not node's state....
                        m.id = self.id
                        m.delay = randint(StageNode.minDelay, StageNode.maxDelay)
                        m.stage = self.stage
                        self.sendRight(m)
                        message = clone(m)
                        message.dir = 3
                        self.sendStageLeft(message)
                        #print("Waiting Node id " + str(self.id) + " is processing Left Node " + str(messageLeft.id))
                        log.debug("Waiting Node id " + str(self.id) + " is processing Left Node " + str(messageLeft.id))
                    else: # Defeated, set state.
                        self.state = self.DEFEATED
                        #print("Waiting Node id " + str(self.id) + " is DEFEATED!")
                        log.debug("Waiting Node id " + str(self.id) + " is DEFEATED!")
                else:
                    return

            elif self.processedMsg == 1: # processed Left, message from right, then process
                if self.messageQueue:
                    mRight = self.messageQueue.pop(0)
                    if (not self.fail) and self.id < mRight.id: # waiting processing
                        # steps: set self.state to Candidate / stage + 1 / send msg to 2 sides
                        self.state = StageNode.CANDIDATE
                        self.stage += 1
                        m = StageMessage()
                        m.type = m.INFO #self.CANDIDATE # should set to message's type not node's state....
                        m.id = self.id
                        m.delay = randint(StageNode.minDelay, StageNode.maxDelay)
                        m.stage = self.stage
                        self.sendRight(m)
                        message = clone(m)
                        message.dir = 3
                        self.sendStageLeft(message)
                        #print("Waiting Node id " + str(self.id) + " is processing Right Node " + str(message.id))
                        log.debug("Waiting Node id " + str(self.id) + " is processing Right Node " + str(message.id))
                    else: # Defeated, set state.
                        self.state = self.DEFEATED
                        #print("Waiting Node id " + str(self.id) + " is DEFEATED!")
                        log.debug("Waiting Node id " + str(self.id) + " is DEFEATED!")
                else:
                    return

        elif self.state == StageNode.LEADER:
            self.messageQueue = []
            self.messageQueueLeft = []
        else:
            #log.info("Node %17s | TRANSMIT | %-17s | to Node %03d" % (self.log(), message.log(), self.right.id))  ##
            #print("last else, there is error if this line is printed!!!!")
            sys.stderr.write("last else, there is error if this line is printed!!!!")
Ejemplo n.º 55
0
    def get_data_for(self, ticker, start=None, end=None, features=None):

        # Default end value is yesterday and no higher
        if end is None:
            end = DataHandler.date_to_key(datetime.datetime.now()) - 1
        elif end > DataHandler.date_to_key(datetime.datetime.now()) - 1:
            raise ValueError(
                "end value must be no higher than yesterday: ",
                DataHandler.date_to_key(datetime.datetime.now()) - 1)

        # Default start value is 2000 days prior to start and no earlier than Jan 1 2005
        if start is None:
            start = end - 2000
            if start < 0:
                start = 0
        elif start < 0:
            raise ValueError("end value must be later than the epoch: 0")

        # Default features value is all features
        if features is None or len(features) == 0:
            features = clone(all_features)
        # Remove duplicates
        features = list(dict.fromkeys(features))

        print("Getting data for ", ticker, "with a start of", start,
              "and an end of", end, "with the features:", features)

        # Load the full stored dataset of the ticker
        data = self.load_dataset(ticker)

        # Fill missing values with previous value, if no previous value, then the first value that appears
        for feature in features:
            if feature not in data:
                raise ValueError("feature " + feature +
                                 " does not exist in the " + ticker + "data")
            elif len(data[feature]) == 0:
                raise ValueError("feature " + feature + " is empty for " +
                                 ticker)
            prev_value = None
            back_idx = None
            for i in range(len(data[feature])):
                if data[feature][i] is None:
                    data[feature][i] = prev_value
                elif back_idx is None:
                    back_idx = i
                prev_value = data[feature][i]
            if back_idx is not None:
                for i in range(back_idx):
                    data[feature][i] = data[feature][back_idx]
            if data[feature][0] is None:
                raise ValueError("feature " + feature + " is empty for " +
                                 ticker)

        # Prune the unwanted features and date range
        keys = data.keys()
        for feature in list(keys):
            if feature in features:
                if len(data[feature]) >= end:
                    data[feature] = data[feature][start:end]
                else:
                    DataHandler.extend_list_to(data[feature], end)
                    data[feature] = data[feature][start:end]
            else:
                del data[feature]

        # Check that all features are there
        for feature in features:
            if feature not in data:
                raise ValueError("feature " + feature +
                                 " does not exist in the " + ticker + "data")

        #print(data)

        # Return the desired data
        return DataHandler.convert_data_to_numpy(data, start, end, features)
Ejemplo n.º 56
0
 def get_node_clone(node):
     if node not in node_map:
         node_map[node] = clone(node) 
         graph.add_node(node_map[node])
     return node_map[node]