def add_place(self):
        """ Create a new place component with default settings and forwards it to the ControllerDrawingArea which is responsible for displaying it. """
        if self._controller_drawing_area != None:
            # reset previous settings
            self._model.notify_reset()

            # instantiate new place object
            self._component = place.Place()
            self._component.label = "New Place"
            self._component.key = "new_comp"
            self._component.marking = 0
            self._component.radius = 15

            # check if places are already available and if yes the size of them is used as template (zoom could be activated)
            adapt = False
            if self._model.data != None:
                for key, item in self._model.data.places.items():
                    if item != None:
                        self._component.radius = item.radius
                        adapt = True
                        break
                # if no place is available it will be checked for available transitions which could be used as template to keep the proportions constant 
                # between different component types
                if not adapt:
                    for key, item in self._model.data.transitions.items():
                        if item != None:
                            self._component.radius = item.dimension[0]
                            adapt = True
                            break 
            # forward component
            if self._controller_drawing_area != None:
                self._controller_drawing_area.add_component(True, self._component)
Beispiel #2
0
def fromJsonToPlace(path_file, city_id):
    list_places = []
    data_json = json.load(open(path_file))
    if (len(data_json) != 0):
        nSize = len(data_json['categories'])
        #On enregistre uniquement les éléments avec description et photo
        try:
            types = []
            #Mise en forme des données sous liste
            for i in range(0, nSize):
                types.append(data_json['categories'][i]['name'])
            id_ = data_json['id']
            photo = data_json['bestPhoto']['prefix'] + data_json['bestPhoto'][
                'suffix']
            visitsCount = data_json['stats']['visitsCount']
            geometry = [
                data_json['location']['lat'], data_json['location']['lng']
            ]
            name = data_json['name']
            print("CITYID de la place enregistrée : " + str(city_id))
            list_places.append(
                p.Place(id_, name, photo, types, geometry, visitsCount,
                        city_id))
        except KeyError:
            print("informations manquantes")
        except IndexError:
            print("informations manquantes")
    else:
        print("hors sélection")
    return list_places
Beispiel #3
0
    def get_places(self,
                   place_name,
                   location,
                   radius=2000,
                   min_price=None,
                   max_price=None):
        def get_price_level(money):
            if money > 1500:
                return 3
            elif money > 600:
                return 2
            elif money > 200:
                return 1
            return 0

        if max_price is not None:
            max_price = get_price_level(max_price)
        if min_price is not None:
            min_price = get_price_level(min_price)

        request = GooglePlace(
            self.gmaps_places,
            place_name,
            radius=radius,
            # min_price=min_price,  # it is bug
            # max_price=max_price,
            # open_now=True,
            location=location,
            language='ru')
        if request['status'] != 'OK':
            return []
        elements = request['results']
        places = []
        for element in elements:
            name = element['name']
            position_lat_lng = element['geometry']['location']
            position = position_lat_lng['lat'], position_lat_lng['lng']
            info = {}
            for label in ['types', 'rating', 'address']:
                if label in element:
                    info[label] = element[label]
            info['type'] = place_name
            """if element.get('photos', []):
               reference = element['photos'][0]['photo_reference']
               photo = GooglePlacePhoto(self.gmaps_places, reference, max_height=200, max_width=200)
               print(photo)
               pass"""
            places.append(place.Place(name, position, info))
        return places
Beispiel #4
0
def find_path(start, finish, duration, duration_on_foot, money, temp_place, cafe_type, time_cafe, calc_score):
    """
    :param start: --- position tuple(float, float) or str
    :param finish: --- position tuple(float, float) or str
    :param duration: --- float
    :param duration_on_foot: --- float
    :param money: --- float
    :param temp_place: --- [name, ...]
    :param cafe_type: --- str or None
    :param time_cafe: --- float or None
    :return: [place, ...]
    """
    gmap = map_api.GMap()
    if isinstance(start, str):
        start = gmap.get_position_from_name(start, SPB_POSITION)
    if isinstance(finish, str):
        finish = gmap.get_position_from_name(finish, SPB_POSITION)
    print(start)
    if True:
        walk_places = []
        for place_type in temp_place:
            walk_places.append([])
            walk_places[-1] += gmap.get_places(
                place_type,
                location=start,
                min_price=0,
                max_price=money
            )
        if cafe_type is not None:
            walk_places.append([])
            walk_places[-1] += gmap.get_places(
                cafe_type,
                location=start,
                min_price=0,
                max_price=money
            )

        if False:
            import pickle
            for i in range(len(walk_places)):
                for j in range(len(walk_places[i])):
                    walk_places[i][j] = walk_places[i][j].to_dick()

            with open('H', 'wb') as file:
                pickle.dump(walk_places, file=file)
    else:
        import pickle
        with open('H', 'rb') as file:
            walk_places = pickle.load(file=file)

        for i in range(len(walk_places)):
            for j in range(len(walk_places[i])):
                walk_places[i][j] = Place.Place(**walk_places[i][j])

    best_path = []
    best_score = -10 ** 30

    for iteration in range(30):
        random.seed(iteration + 228)

        size = random.randint(1, len(walk_places) * 2)
        if cafe_type is not None:
            cafe_index = random.randint(0, size)
        else:
            cafe_index = None
        places = []
        for i in range(size):
            if i == cafe_index:
                places.append(walk_places[-1])
            else:
                if cafe_type is not None:
                    index = random.randint(0, len(walk_places) - 2)
                else:
                    index = random.randint(0, len(walk_places) - 1)
                places.append(walk_places[index])
        average_distance = max(5, (duration_on_foot - 10 * size) / size)
        mid_path = [Place.Place('start', start, {})]
        fail = False
        for place_type in places:
            best_place = None
            for j in range(30):
                place = random.choice(place_type)
                distance = gmap.get_duration(place.position, mid_path[-1].position)
                if distance is not None and 0.5 < distance / average_distance < 3:
                    best_place = place
                    break

            if best_place is None:
                fail = True
                break
            else:
                mid_path.append(best_place)
        if not fail:
            mid_path += [Place.Place('finish', finish, {})]
            score = calc_score(mid_path)
            print(score, iteration)
            if best_score < score:
                best_score = score
                best_path = mid_path
        else:
            print('fail', iteration)
    return best_path
Beispiel #5
0
 def _set_value(self, value):
     """Convert a location value into a place"""
     if type(value) == place.Place:
         self._value = value
     else:
         self._value = place.Place(value)
    def convert(self):
        """ Start converting process from matrices to components. """

        # Information: A lot of exceptions will be thrown because of the NoneType issue we had and it is necessary to prevent the appliation from crashing.

        # create a PetriNet object
        self._pn = petri_net.PetriNet()
        # set PetriNetData object
        self._pn.data = self._data

        try:
            # create the individual places
            for i in range(len(self._data.places)):
                # set default values for the design
                p = place.Place([0.0, 0.0], 15., [0., 0., 0.],
                                [255., 255., 255.])
                # set properties
                p.label = self._data.places[i]
                p.key = self._data.places[i]
                if self._data.initial_marking != None:
                    p.marking = self._data.initial_marking[i]
                try:
                    if self._data.capacities != None:
                        p.capacity = self._data.capacities[i]
                except IndexError:
                    pass
                # add place to the PetriNet object
                self._pn.add_place(p, )
        except TypeError:
            pass

        try:
            # create the individual transitions
            for i in range(len(self._data.transitions)):
                # set default values for the design
                t = transition.Transition([0.0, 0.0], [15, 30], [0., 0., 0.],
                                          [0., 0., 0.])
                # set properties
                t.label = self._data.transitions[i]
                t.key = self._data.transitions[i]
                if self._data.rates != None:
                    t.rate = self._data.rates[i]
                # add transition to the PetirNet object
                self._pn.add_transition(t)
        except TypeError:
            pass

        try:
            # create the individual arcs connecting two nodes
            for i in range(len(self._data.transitions)):
                for j in range(len(self._data.places)):
                    a_pre = None
                    a_post = None
                    a_test = None
                    a_inhib = None

                    # iteration through the matrices to figure out which arc needs to be created

                    try:
                        if self._data.stoichiometry.pre_arcs != None:
                            try:
                                if self._data.stoichiometry.pre_arcs[
                                        i, j] != None:
                                    if self._data.stoichiometry.pre_arcs[
                                            i, j] != 0:
                                        # create a standard pre arc
                                        a_pre = arc.Arc()
                                        a_pre.line_type = arc.Arc.LINE_TYPE_STRAIGHT
                                        a_pre.label = str(
                                            "Arc" + self._data.places[j] +
                                            "to" + self._data.transitions[i])
                                        a_pre.key = str(
                                            "Arc" + self._data.places[j] +
                                            "to" + self._data.transitions[i])
                                        a_pre.origin = self._pn.get_component(
                                            self._data.places[j])
                                        a_pre.target = self._pn.get_component(
                                            self._data.transitions[i])
                                        if self._data.stoichiometry.pre_arcs != None:
                                            a_pre.weight = self._data.stoichiometry.pre_arcs[
                                                i, j]
                                        # add arc to the PetriNet object
                                        self._pn.add_arc(a_pre)
                            except IndexError:
                                pass
                    except TypeError:
                        pass
                    try:
                        if self._data.stoichiometry.post_arcs != None:
                            try:
                                if self._data.stoichiometry.post_arcs[
                                        i, j] != None:
                                    if self._data.stoichiometry.post_arcs[
                                            i, j] != 0:
                                        # create a standard post arc
                                        a_post = arc.Arc()
                                        a_post.line_type = arc.Arc.LINE_TYPE_STRAIGHT
                                        a_post.label = str(
                                            self._data.transitions[i] + "to" +
                                            self._data.places[j])
                                        a_post.key = str(
                                            self._data.transitions[i] + "to" +
                                            self._data.places[j])
                                        a_post.origin = self._pn.get_component(
                                            self._data.transitions[i])
                                        a_post.target = self._pn.get_component(
                                            self._data.places[j])
                                        if self._data.stoichiometry.post_arcs != None:
                                            a_post.weight = self._data.stoichiometry.post_arcs[
                                                i, j]
                                        # add arc to the PetriNet object
                                        self._pn.add_arc(a_post)
                            except IndexError:
                                pass
                    except TypeError:
                        pass
                    try:
                        if self._data.test_arcs != None:
                            try:
                                if self._data.test_arcs[i, j] != None:
                                    if self._data.test_arcs[i, j] != 0:
                                        # create a test arc
                                        a_test = test_arc.TestArc()
                                        a_test.line_type = test_arc.TestArc.LINE_TYPE_ARC_LOWER
                                        a_test.label = str(
                                            "TestArc" +
                                            self._data.transitions[i] + "to" +
                                            self._data.places[j])
                                        a_test.key = str(
                                            "TestArc" +
                                            self._data.transitions[i] + "to" +
                                            self._data.places[j])
                                        a_test.target = self._pn.get_component(
                                            self._data.transitions[i])
                                        a_test.origin = self._pn.get_component(
                                            self._data.places[j])
                                        if self._data.test_arcs != None:
                                            a_test.weight = self._data.test_arcs[
                                                i, j]
                                        # add arc to the PetriNet object
                                        self._pn.add_arc(a_test)
                            except IndexError:
                                pass
                    except TypeError:
                        pass
                    try:
                        if self._data.inhibitory_arcs != None:
                            try:
                                if self._data.inhibitory_arcs[i, j] != None:
                                    if self._data.inhibitory_arcs[i, j] != 0:
                                        # create an inhibitory arc
                                        a_inhib = inhibitory_arc.InhibitoryArc(
                                        )
                                        a_inhib.line_type = inhibitory_arc.InhibitoryArc.LINE_TYPE_ARC_UPPER
                                        a_inhib.label = str(
                                            "InhibitoryArc" +
                                            self._data.transitions[i] + "to" +
                                            self._data.places[j])
                                        a_inhib.key = str(
                                            "InhibitoryArc" +
                                            self._data.transitions[i] + "to" +
                                            self._data.places[j])
                                        a_inhib.target = self._pn.get_component(
                                            self._data.transitions[i])
                                        a_inhib.origin = self._pn.get_component(
                                            self._data.places[j])
                                        if self._data.inhibitory_arcs != None:
                                            a_inhib.weight = self._data.inhibitory_arcs[
                                                i, j]
                                        # add arc to the PetriNet object
                                        self._pn.add_arc(a_inhib)
                            except IndexError:
                                pass
                    except TypeError:
                        pass

                    # determine the style of the line automatically
                    # if two standard arcs are available the curved arcs will be chosen
                    if a_pre != None and a_post != None:
                        a_pre.line_type = arc.Arc.LINE_TYPE_ARC_LOWER
                        self._pn.update(a_pre, a_pre.key)
                        a_post.line_type = arc.Arc.LINE_TYPE_ARC_UPPER
                        self._pn.update(a_post, a_post.key)
        except TypeError:
            pass


#        print len(self._pn.arcs)
# if an algorithm is defined the positions of the components will be determined
        if self._layout != None:
            self.__layout_components()