示例#1
0
def dredgeRiverSingle(nature, direction, CX, CY):
    rivert = []

    # This is the start coordinate of the river, perpendicular to the
    # axis of the river.  N/S rivers choose positions on the E/W axis,
    # and vice-versa
    axis_size = CX if direction else CY
    line_pos = random.randint(0, axis_size)

    #river width, no more than half the map
    river_size = min(random.randint(3, 120), axis_size // 2)

    for i in range(0, axis_size):
        if direction:
            element = Rect(Point(i, line_pos), Point(i, line_pos + river_size))
        else:
            element = Rect(Point(line_pos, i), Point(line_pos + river_size, i))

        element.set_name('RIVER')
        rivert.append(element)

    prev = 0
    for f in rivert:
        prev = prev + random.randint(-1, 1)
        f.move(direction + 2, prev)  # in rect, 1=up, 2=right
    nature.extend(rivert)
示例#2
0
def createPlaceFree():
    p1 = Point(random.randint(0, config['CITY_SIZE_X']),
               random.randint(0, config['CITY_SIZE_Y']))
    p2 = Point(random.randint(p1.x + MIN_PLACE_SIZE, p1.x + MAX_PLACE_SIZE),
               random.randint(p1.y + MIN_PLACE_SIZE, p1.y + MAX_PLACE_SIZE))
    place = Rect(p1, p2)
    return place
示例#3
0
def createNature(old_place, sx, sy, dx, dy):
    p1 = Point(
        random.randint(old_place.top_left().x - sx - dx,
                       old_place.top_left().x + sx + dx),
        random.randint(old_place.top_left().y - sy - dy,
                       old_place.top_left().y + sy + dy))
    p2 = Point(p1.x + dx, p1.y + dy)
    place = Rect(p1, p2)
    return place
示例#4
0
def createPlacePoint(p):
    p1 = Point(
        random.randint(p.x - MAX_PLACE_SIZE * WEALTH,
                       p.x + MAX_PLACE_SIZE * WEALTH),
        random.randint(p.y - MAX_PLACE_SIZE * WEALTH,
                       p.y + MAX_PLACE_SIZE * WEALTH))
    p2 = Point(random.randint(p1.x + MIN_PLACE_SIZE, p1.x + MAX_PLACE_SIZE),
               random.randint(p1.y + MIN_PLACE_SIZE, p1.y + MAX_PLACE_SIZE))
    place = Rect(p1, p2)
    return place
示例#5
0
def createPlace(old_place):
    p1 = Point(
        random.randint(old_place.top_left().x - MAX_PLACE_SIZE,
                       old_place.top_left().x + MAX_PLACE_SIZE),
        random.randint(old_place.top_left().y - MAX_PLACE_SIZE,
                       old_place.top_left().y + MAX_PLACE_SIZE))
    p2 = Point(random.randint(p1.x + MIN_PLACE_SIZE, p1.x + MAX_PLACE_SIZE),
               random.randint(p1.y + MIN_PLACE_SIZE, p1.y + MAX_PLACE_SIZE))
    place = Rect(p1, p2)
    return place
示例#6
0
def createPlaceDefault(p,name):
    
    if not name in defaultPlace.keys():
        name='unknown'
        
    MPS=defaultPlace[name][1]
    mps=defaultPlace[name][0]
    if(defaultPlace[name][2]=='free'):
        p1=Point(random.randint(0,CITY_SIZE_X),random.randint(0,CITY_SIZE_Y))
        p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS))
        
    if(defaultPlace[name][2]=='center'):
        p1=Point(random.randint(p.x-MPS,p.x+MPS),random.randint(p.y-MPS,p.y+MPS))
        p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS))
        
    if(defaultPlace[name][2]=='urban'):
        p1=Point(random.randint(int(p.x-int(CITY_SIZE_X/3)-MPS),int(p.x+int(CITY_SIZE_X/3)+MPS)),random.randint(int(p.y-int(CITY_SIZE_Y/3)-MPS),p.y+int(CITY_SIZE_Y/3)+MPS))
        p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS))
        
    if(defaultPlace[name][2]=='rural'):
        p1=Point(random.randint(int(p.x-int(CITY_SIZE_X/3)-MPS),int(p.x+int(CITY_SIZE_X/3)+MPS)),random.randint(int(p.y-int(CITY_SIZE_Y/3)-MPS),p.y+int(CITY_SIZE_Y/3)+MPS))   
        if(random.randint(0,1)==0):
            p1.x=p1.x+int(CITY_SIZE_X/3)
        else:
            p1.x=p1.x-(CITY_SIZE_X/3)
        if(random.randint(0,1)==0):
            p1.y=p1.y+int(CITY_SIZE_Y/3)
        else:
            p1.y=p1.y-int(CITY_SIZE_Y/3)
        p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS))
        
    place=Rect(p1,p2)
    return place
示例#7
0
def map_all_and_work(zone, duty):
    workers = []
    pool = Pool()
    nw = Rect(Point(0, 0), Point(CITY_SIZE_X / 2, CITY_SIZE_Y / 2))
    ne = Rect(Point(CITY_SIZE_X / 2, 0), Point(CITY_SIZE_X, CITY_SIZE_Y / 2))
    sw = Rect(Point(0, CITY_SIZE_Y / 2), Point(CITY_SIZE_X / 2, CITY_SIZE_Y))
    se = Rect(Point(CITY_SIZE_X / 2, CITY_SIZE_Y / 2),
              Point(CITY_SIZE_X, CITY_SIZE_Y))
    workers.append(
        pool.apply_async(conflictSolver, [(0, 0), zone[0][0], nw, duty]))
    workers.append(
        pool.apply_async(conflictSolver, [(0, 1), zone[0][1], ne, duty]))
    workers.append(
        pool.apply_async(conflictSolver, [(1, 0), zone[1][0], sw, duty]))
    workers.append(
        pool.apply_async(conflictSolver, [(1, 1), zone[1][1], se, duty]))

    pool.close()
    pool.join()
    zone = [[[], []], [[], []]]
    scambi = False
    bimbi_perduti = 0
    for w in workers:
        res = w.get()
        #print(res)
        #print("index x:" +str(res[0][0]))
        #print("index y:" +str(res[0][1]))
        zone[res[0][0]][res[0][1]].extend(res[1])
        YY = res[0][0]
        XX = res[0][1]
        if ((YY - 1) >= 0):  #top
            if (len(res[2]) > 0):
                scambi = True
            zone[YY - 1][XX].extend(res[2])
        else:
            bimbi_perduti = bimbi_perduti + len(res[2])
        if ((XX - 1) >= 0):  #left
            if (len(res[3]) > 0):
                scambi = True
            zone[YY][XX - 1].extend(res[3])
        else:
            bimbi_perduti = bimbi_perduti + len(res[3])
        if ((XX + 1) <= 1):  #right
            if (len(res[4]) > 0):
                scambi = True
            zone[YY][XX + 1].extend(res[4])
        else:
            bimbi_perduti = bimbi_perduti + len(res[4])
        if ((YY + 1) <= 1):  #bottom
            if (len(res[5]) > 0):
                scambi = True
            zone[YY + 1][XX].extend(res[5])
        else:
            bimbi_perduti = bimbi_perduti + len(res[5])

    #print('abbiamo perso n '+str(bimbi_perduti))
    return (zone, scambi)
示例#8
0
 def __init__(self):
     DocItem.__init__(self)
     self.items = []
     # pointer to the local styles dict
     self.styles = default_styles
     self.style = ('body', 1)
     # left and top inner margins
     self.margins = Point(0, 0)
示例#9
0
    def _restore_rects(self, mp, cls, scale, ratio, threshold=None, top=None):
        # print ratio
        map_h, map_w = mp.shape[:2]
        tmp = math.sqrt(ratio)
        rect_h = (scale / tmp)
        rect_w = (scale * tmp)

        # print "h, w", rect_h, rect_w

        def cut_top(res):
            res = sorted(res, reverse=True, key=lambda val: val[0])
            if top is not None:
                res = res[:top]
            return res

        res = []

        for y in range(0, map_h):
            for x in range(0, map_w):
                base_center = Point()
                base_center.y = (y + 0.5) / map_h
                base_center.x = (x + 0.5) / map_w

                dy, dx, sh, sw = mp[y, x].reshape((4, ))
                dy, dx, sh, sw = 0, 0, 0, 0
                #sh, sw = 0, 0
                #dy, dx = 0, 0

                sh = min(max(sh, -10), 10)
                sw = min(max(sw, -10), 10)

                conf = cls[y][x]
                if threshold is not None or conf > threshold:
                    r_x = dx * rect_w + base_center.x
                    r_y = dy * rect_h + base_center.y
                    r_w = rect_w * math.exp(sw)
                    r_h = rect_h * math.exp(sh)
                    center = Point()
                    center.x = r_x
                    center.y = r_y
                    res.append((conf, center.rect_from_center(r_h, r_w)))

                    if top is not None and len(res) > 2 * top:
                        res = cut_top(res)

        return cut_top(res)
示例#10
0
    def _init(self):
        self.screen = {'width': 0, 'height': 0}
        for key in self.screen.keys():
            if self.tree.hierarchy.has_attr(key):
                try:
                    self.screen[key] = int(self.tree.hierarchy[key])
                except:
                    self.screen = {'width': 0, 'height': 0}
                    pass

        if self._has_screen_info():
            self.DEFAULT_MAX_DISTANCE = min(self.screen['width'],
                                            self.screen['height'])
            self.LARGE_BUTTON_MAX_SIZE = Point(self.screen['width'],
                                               self.screen['height'] / 10)
            self.LARGE_BUTTON_MIN_SIZE = Point(self.screen['width'] / 2,
                                               self.screen['height'] / 17)
            self.PAGE_BUTTON_BOUNDS = Bounds(
                Point(0, self.screen['height'] - (self.screen['height'] / 5)),
                Point(self.screen['width'], self.screen['height']))

        self.tokenizer = Tokenizer()
        self.action_list = []
        self.elements = []
        self.index_map = {}
        self.bounds_list = []

        self._extract_actions_for_webview(self.elements, self.index_map,
                                          self.bounds_list)
        self._extract_actions(self.tree.hierarchy, self.elements,
                              self.index_map, self.bounds_list)
        #sort action_list by x-order, y-order
        self.action_list.sort(key=lambda k: (k.y_order, k.x_order))
        #update action no
        for no, action in enumerate(self.action_list):
            action.no = no

        # bounds_vec = map(lambda b: Point(b.left+(b.width/2), b.top), self.bounds_list)
        bounds_vec = map(lambda b: Point(b.left, b.top), self.bounds_list)
        self.dm = DistMatrix(bounds_vec)
示例#11
0
def digCave(nature):
    caves = RESOURCES.count('CAVE')
    print('Digging %d cave%s' % (caves, 's' if caves > 1 else ''))
    for p in range(0, RESOURCES.count('CAVE')):
        CX = config['CITY_SIZE_X']
        CY = config['CITY_SIZE_Y']
        CX2 = config['CITY_SIZE_X'] // 2
        CY2 = config['CITY_SIZE_Y'] // 2
        CX3 = config['CITY_SIZE_X'] // 3
        CY3 = config['CITY_SIZE_Y'] // 3
        p = Point(CX2, CY2)
        MPS = 200
        mps = 30
        if (random.randint(0, 1) == 0):
            p1 = Point(random.randint(0, CX3), random.randint(0, CY))
        else:
            p1 = Point(random.randint(0, CX), random.randint(0, CY3))

        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))
        place = Rect(p1, p2)
        place.set_name('CAVE')
        nature.append(place)
示例#12
0
def createPlaceDefault(p,name):

	if not name in list(defaultPlace.keys()):
		name='unknown'

	mps=eval_eqn(defaultPlace[name][0])
	MPS=eval_eqn(defaultPlace[name][1])

	#print(name,mps,MPS)

	thirdx = config['CITY_SIZE_X']//3
	thirdx_up = thirdx + MPS
	thirdx_dn = thirdx - MPS

	thirdy = config['CITY_SIZE_Y']//3
	thirdy_up = thirdy + MPS
	thirdy_dn = thirdy - MPS


	if(defaultPlace[name][2]=='free'):
		p1=Point(random.randint(0,config['CITY_SIZE_X']),random.randint(0,config['CITY_SIZE_Y']))
		p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS))

	if(defaultPlace[name][2]=='center'):
		p1=Point(random.randint(p.x-MPS,p.x+MPS),random.randint(p.y-MPS,p.y+MPS))
		p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS))

	if(defaultPlace[name][2]=='urban'):
		p1=Point( random.randint(p.x-thirdx_dn, p.x+thirdx_up), random.randint(p.y-thirdy_dn, p.y+thirdy_up) )
		p2=Point( random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS) )

	if(defaultPlace[name][2]=='rural'):
		p1=Point(random.randint( p.x-thirdx_dn,p.x+thirdx_up), random.randint(p.y-thirdy_dn,p.y+thirdy_up))
		if(random.randint(0,1)==0):
			p1.x=p1.x+thirdx
		else:
			p1.x=p1.x-thirdx

		if(random.randint(0,1)==0):
			p1.y=p1.y+thirdy
		else:
			p1.y=p1.y-thirdy

		p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS))

	place=Rect(p1,p2)
	return place
示例#13
0
def test_temp(yy, xx, place):
    nw = Rect(Point(0, 0), Point(CITY_SIZE_X / 2, CITY_SIZE_Y / 2))
    ne = Rect(Point(CITY_SIZE_X / 2, 0), Point(CITY_SIZE_X, CITY_SIZE_Y / 2))
    sw = Rect(Point(0, CITY_SIZE_Y / 2), Point(CITY_SIZE_X / 2, CITY_SIZE_Y))
    se = Rect(Point(CITY_SIZE_X / 2, CITY_SIZE_Y / 2),
              Point(CITY_SIZE_X, CITY_SIZE_Y))
    if (xx == 0) and (yy == 0):
        if (not nw.overlaps(place)):
            print('errore micidiale 1')
    if (xx == 0) and (yy == 1):
        if (not ne.overlaps(place)):
            print('errore micidiale 2')
    if (xx == 1) and (yy == 0):
        if (not sw.overlaps(place)):
            print('errore micidiale 3')
    if (xx == 1) and (yy == 1):
        if (not se.overlaps(place)):
            print('errore micidiale 4')
示例#14
0
def test_temp(yy, xx, place):
    CX = config['CITY_SIZE_X']
    CY = config['CITY_SIZE_Y']

    # Make 4 rectangles, one for each qudrant
    nw = Rect(Point(0, 0), Point(CX // 2, CY // 2))
    ne = Rect(Point(CX // 2, 0), Point(CX // 2, CY // 2))
    sw = Rect(Point(0, CY // 2), Point(CX // 2, CY))
    se = Rect(Point(CX // 2, CY // 2), Point(CX, CY))

    if (xx == 0) and (yy == 0):
        if (not nw.overlaps(place)):
            return 1
    if (xx == 0) and (yy == 1):
        if (not ne.overlaps(place)):
            return 2
    if (xx == 1) and (yy == 0):
        if (not sw.overlaps(place)):
            return 3
    if (xx == 1) and (yy == 1):
        if (not se.overlaps(place)):
            return 4
    return False
示例#15
0
    def _get_bblox_info(self, rects, img_h, img_w, map_h, map_w, scale, ratio):
        tmp = math.sqrt(ratio)
        # convert everything to img 0-1 axes
        rect_h = (scale / tmp)
        rect_w = (scale * tmp)
        # print "h, w", rect_h, rect_w

        result = []
        for y in range(0, map_h):
            for x in range(0, map_w):
                base_center = Point()
                base_center.y = (y + 0.5) / map_h
                base_center.x = (x + 0.5) / map_w
                base_rect = base_center.rect_from_center(rect_h, rect_w)
                # l, t, r, b = cx - rect_w_2, cy - rect_h_2, cx + rect_w_2, cy + rect_h_2
                for rect_real in rects:
                    rect = rect_real.copy()
                    rect.stretch(1.0 / img_h, 1.0 / img_w)
                    inner = rect.intersection(base_rect)
                    outer = rect.union(base_rect)

                    r_height = rect.height()
                    r_width = rect.width()
                    is_same_oriented = (base_rect.width() <=
                                        base_rect.height()) == (r_width <=
                                                                r_height)
                    th_proportion = 4
                    is_high_disproportional = th_proportion * r_width < r_height or th_proportion * r_height < r_width
                    th_weak_const = th_proportion * min(
                        r_width, r_height) / max(r_width, r_height,
                                                 0.0001)  # < 1
                    weak_const = th_weak_const if is_same_oriented and is_high_disproportional else 1
                    '''
                    if is_same_oriented and is_high_disproportional:
                        if base_rect.width() > base_rect.height():
                            print '++++++++++'
                        print '################################'
                        print r_height, r_width
                        print th_weak_const, weak_const
                        print ',,,'
                        base_rect.dump()
                        rect.dump()
                        inner.dump()
                        outer.dump()
                        print inner.area(), outer.area()
                    '''

                    if inner.valid(
                    ) and inner.area() > 0.5 * outer.area() * weak_const:
                        '''
                        if is_same_oriented and is_high_disproportional:
                            print '################################'
                            print r_height, r_width
                            print th_weak_const, weak_const
                            print ',,,'
                            base_rect.dump()
                            rect.dump()
                            inner.dump()
                            outer.dump()
                            print inner.area(), outer.area(), weak_const



                        
                        print '-----'
                        base_rect.dump()
                        rect.dump()
                        inner.dump()
                        outer.dump()
                        print inner.area(), outer.area()
                        '''

                        center = rect.center()

                        max_shift = 2.0

                        dx = min((center.x - base_center.x) /
                                 (base_rect.width() + .0), max_shift)
                        dy = min((center.y - base_center.y) /
                                 (base_rect.height() + .0), max_shift)

                        max_stretch = 2.0

                        sh = math.log(
                            min(rect.height() / (base_rect.height() + .0),
                                max_stretch))
                        sw = math.log(
                            min(rect.width() / (base_rect.width() + .0),
                                max_stretch))

                        result.append(np.array([y, x, dy, dx, sh, sw]))
        return np.array(result)
示例#16
0
    def _restore_rects(self,
                       tdy,
                       tdx,
                       tsh,
                       tsw,
                       cls,
                       scale,
                       ratio,
                       threshold=None,
                       top=None):
        # print ratio
        map_h, map_w = cls.shape[1:3]
        print map_h, map_w
        print cls.shape
        tmp = math.sqrt(ratio)
        rect_h = (scale / tmp)
        rect_w = (scale * tmp)

        # print "h, w", rect_h, rect_w

        def cut_top(res):
            res = sorted(res, reverse=True, key=lambda val: val[0])
            if top is not None:
                res = res[:top]
            return res

        res = []

        for y in range(0, map_h):
            for x in range(0, map_w):
                base_center = Point()
                base_center.y = (y + 0.5) / map_h
                base_center.x = (x + 0.5) / map_w

                # print tdy.shape
                dy, dx, sh, sw = tdy[0, y, x, 0], tdx[0, y, x,
                                                      0], tsh[0, y, x,
                                                              0], tsw[0, y, x,
                                                                      0]
                dy, dx, sh, sw = 0, 0, 0, 0
                #sh, sw = 0, 0
                #dy, dx = 0, 0

                # print cls.shape
                conf = cls[0, y, x, 0]
                if conf > 0:
                    print conf
                if threshold is not None or conf > threshold:
                    r_x = dx * rect_w + base_center.x
                    r_y = dy * rect_h + base_center.y
                    r_w = rect_w * math.exp(sw)
                    r_h = rect_h * math.exp(sh)
                    center = Point()
                    center.x = r_x
                    center.y = r_y
                    res.append((conf, center.rect_from_center(r_h, r_w)))

                    if top is not None and len(res) > 2 * top:
                        res = cut_top(res)

        return cut_top(res)
示例#17
0
        print('*'),
        for place in nature:
            for place2 in nature:
                if (not (id(place) == id(place2))):
                    if (place.overlaps(place2)):
                        place.move(random.randint(0, 4),
                                   random.randint(20, 30))
print('[10/10]')
print('Building river')

for p in range(0, RESOURCES.count('RIVERX')):
    rivert = []
    liney = random.randint(0, CITY_SIZE_Y)
    river_size = random.randint(3, 120)
    for i in range(0, CITY_SIZE_X):
        element = Rect(Point(i, liney), Point(i, liney + river_size))
        element.set_name('RIVER')
        rivert.append(element)

    prev = 0
    for f in rivert:
        prev = prev + random.randint(-1, 1)
        f.move(1, prev)
    nature.extend(rivert)

for p in range(0, RESOURCES.count('RIVERY')):
    rivert = []
    linex = random.randint(0, CITY_SIZE_X)
    river_size = random.randint(3, 120)
    for i in range(0, CITY_SIZE_Y):
        element = Rect(Point(linex, i), Point(linex + river_size, i))
示例#18
0
    def _get_bblox_info(self, rects, img_h, img_w, map_h, map_w, scale, ratio):
        tmp = math.sqrt(ratio)
        # convert everything to img 0-1 axes
        rect_h = (scale / tmp)
        rect_w = (scale * tmp)
        # print "h, w", rect_h, rect_w

        result = []
        for y in range(0, map_h):
            for x in range(0, map_w):
                base_center = Point()
                base_center.y = (y + 0.5) / map_h
                base_center.x = (x + 0.5) / map_w
                base_rect = base_center.rect_from_center(rect_h, rect_w)
                # l, t, r, b = cx - rect_w_2, cy - rect_h_2, cx + rect_w_2, cy + rect_h_2
                for rect_real in rects:
                    rect = rect_real.copy()
                    rect.stretch(1.0 / img_h, 1.0 / img_w)
                    inner = rect.intersection(base_rect)
                    outer = rect.union(base_rect)

                    # r_l, r_t, r_r, r_b = np.array(rect, dtype=float)
                    # r_l, r_t, r_r, r_b = r_l / img_w, r_t / img_h, r_r / img_w, r_b / img_h
                    # i_l, i_t, i_r, i_b = max(l, r_l), max(t, r_t), min(r, r_r), min(b, r_b)
                    # o_l, o_t, o_r, o_b = min(l, r_l), min(t, r_t), max(r, r_r), max(b, r_b)
                    # print r_l, r_t, r_r, r_b
                    # print l, t, r, b
                    # print

                    r_height = rect.height()
                    r_width = rect.width()
                    is_same_oriented = (base_rect.width() <=
                                        base_rect.height()) == (r_width <=
                                                                r_height)
                    th_proportion = 4
                    is_high_disproportional = th_proportion * r_width < r_height or th_proportion * r_height < r_width
                    th_weak_const = th_proportion * min(
                        r_width, r_height) / max(r_width, r_height,
                                                 0.0001)  # < 1
                    weak_const = th_weak_const if is_same_oriented and is_high_disproportional else 1
                    '''
                    if is_same_oriented and is_high_disproportional:
                        if base_rect.width() > base_rect.height():
                            print '++++++++++'
                        print '################################'
                        print r_height, r_width
                        print th_weak_const, weak_const
                        print ',,,'
                        base_rect.dump()
                        rect.dump()
                        inner.dump()
                        outer.dump()
                        print inner.area(), outer.area()
                    '''

                    if inner.valid(
                    ) and inner.area() > 0.5 * outer.area() * weak_const:
                        '''
                        if is_same_oriented and is_high_disproportional:
                            print '################################'
                            print r_height, r_width
                            print th_weak_const, weak_const
                            print ',,,'
                            base_rect.dump()
                            rect.dump()
                            inner.dump()
                            outer.dump()
                            print inner.area(), outer.area(), weak_const



                        
                        print '-----'
                        base_rect.dump()
                        rect.dump()
                        inner.dump()
                        outer.dump()
                        print inner.area(), outer.area()
                        '''

                        # if i_l < i_r and i_t < i_b:
                        # o_area = (o_r - o_l) * (o_b - o_t)
                        # i_area = (i_r - i_l) * (i_b - i_t)
                        # if i_area > o_area * 0.5:
                        # r_c_x = 0.5 * (r_l + r_r)
                        # r_c_y = 0.5 * (r_t + r_b)
                        center = rect.center()
                        # r_h = r_b - r_t
                        # r_w = r_r - r_l

                        max_shift = 2.0

                        dx = min((center.x - base_center.x) /
                                 (base_rect.width() + .0), max_shift)
                        dy = min((center.y - base_center.y) /
                                 (base_rect.height() + .0), max_shift)
                        # dy = (r_c_y - cy) / (2 * rect_h_2)
                        # dx = (r_c_x - cx) / (2 * rect_w_2)

                        max_stretch = 2.0

                        sh = math.log(
                            min(rect.height() / (base_rect.height() + .0),
                                max_stretch))
                        sw = math.log(
                            min(rect.width() / (base_rect.width() + .0),
                                max_stretch))
                        # sh = math.log(r_h / (2 * rect_h_2))
                        # sw = math.log(r_w / (2 * rect_w_2))
                        result.append(np.array([y, x, dy, dx, sh, sw]))
        return np.array(result)
示例#19
0
def map_all_and_work(zone, duty):
    workers = []
    pool = Pool()
    '''this part has a magic number 4 zone that break the possibility to scale in the number of thread TODO fixt it'''
    nw = Rect(Point(0, 0),
              Point(config['CITY_SIZE_X'] / 2, config['CITY_SIZE_Y'] / 2))
    ne = Rect(Point(config['CITY_SIZE_X'] / 2, 0),
              Point(config['CITY_SIZE_X'], config['CITY_SIZE_Y'] / 2))
    sw = Rect(Point(0, config['CITY_SIZE_Y'] / 2),
              Point(config['CITY_SIZE_X'] / 2, config['CITY_SIZE_Y']))
    se = Rect(Point(config['CITY_SIZE_X'] / 2, config['CITY_SIZE_Y'] / 2),
              Point(config['CITY_SIZE_X'], config['CITY_SIZE_Y']))
    RANDOM_SEED = random.randint(0, 10000)

    workers.append(
        pool.apply_async(conflictSolver,
                         [(0, 0), zone[0][0], nw, duty, RANDOM_SEED])
    )  #now it's really consistent during testing! each thread has is own random!
    workers.append(
        pool.apply_async(conflictSolver,
                         [(0, 1), zone[0][1], ne, duty, RANDOM_SEED + 1]))
    workers.append(
        pool.apply_async(conflictSolver,
                         [(1, 0), zone[1][0], sw, duty, RANDOM_SEED + 2]))
    workers.append(
        pool.apply_async(conflictSolver,
                         [(1, 1), zone[1][1], se, duty, RANDOM_SEED + 3]))

    pool.close()
    pool.join()
    zone = [[[], []], [[], []]]
    scambi = False
    bimbi_perduti = 0

    czone = [[[], []], [[], []]]
    for w in workers:
        res = w.get()
        #print(res)
        #print("index x:" +str(res[0][0]))
        #print("index y:" +str(res[0][1]))
        zone[res[0][0]][res[0][1]].extend(res[1])
        YY = res[0][0]
        XX = res[0][1]
        if ((YY - 1) >= 0):  #top
            if (len(res[2].top) > 0):
                scambi = True
            zone[YY - 1][XX].extend(res[2].top)
        else:
            bimbi_perduti = bimbi_perduti + len(res[2].top)
        if ((XX - 1) >= 0):  #left
            if (len(res[2].left) > 0):
                scambi = True
            zone[YY][XX - 1].extend(res[2].left)
        else:
            bimbi_perduti = bimbi_perduti + len(res[2].left)
        if ((XX + 1) <= 1):  #right
            if (len(res[2].right) > 0):
                scambi = True
            zone[YY][XX + 1].extend(res[2].right)
        else:
            bimbi_perduti = bimbi_perduti + len(res[2].right)
        if ((YY + 1) <= 1):  #bottom
            if (len(res[2].bottom) > 0):
                scambi = True
            zone[YY + 1][XX].extend(res[2].bottom)
        else:
            bimbi_perduti = bimbi_perduti + len(res[2].bottom)

        czone[YY][XX] = res[3]
    '''Check corner collision '''
    if (scambi == False):
        for i in range(0, len(czone[0])):
            for j in range(0, len(czone)):
                try:
                    for place in czone[i][
                            j].right:  #check if I made some mistake with index LOL
                        for place2 in czone[i][j + 1].left:
                            if (place.overlaps(place2)):
                                scambi = True
                                q = get_element_index_list(
                                    zone[i][j + 1], place2)
                                zone[i][j + 1][q] = createPlace(
                                    zone[i][j + 1][random.randint(
                                        0,
                                        len(zone[i][j + 1]) - 1)])
                except IndexError:
                    pass
                try:
                    for place in czone[i][
                            j].bottom:  #check if I made some mistake with index LOL
                        for place2 in czone[i + 1][j].top:
                            if (place.overlaps(place2)):
                                scambi = True
                                q = get_element_index_list(
                                    zone[i + 1][j], place2)
                                zone[i + 1][j][q] = createPlace(
                                    zone[i + 1][j][random.randint(
                                        0,
                                        len(zone[i + 1][j]) - 1)])
                except IndexError:
                    pass
                try:
                    for place in czone[i][
                            j].bottom:  #check if I made some mistake with index LOL
                        for place2 in czone[i + 1][j + 1].top:
                            if (place.overlaps(place2)):
                                scambi = True
                                q = get_element_index_list(
                                    zone[i + 1][j + 1], place2)
                                zone[i + 1][j + 1][q] = createPlace(
                                    zone[i + 1][j + 1][random.randint(
                                        0,
                                        len(zone[i + 1][j + 1]) - 1)])
                except IndexError:
                    pass

    #print('abbiamo perso n '+str(bimbi_perduti))
    return (zone, scambi)
示例#20
0
def createPlaceDefault(p, name):

    if not name in defaultPlace.keys():
        name = 'unknown'

    MPS = defaultPlace[name][1]
    mps = defaultPlace[name][0]
    if (defaultPlace[name][2] == 'free'):
        p1 = Point(random.randint(0, CITY_SIZE_X),
                   random.randint(0, CITY_SIZE_Y))
        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))

    if (defaultPlace[name][2] == 'center'):
        p1 = Point(random.randint(p.x - MPS, p.x + MPS),
                   random.randint(p.y - MPS, p.y + MPS))
        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))

    if (defaultPlace[name][2] == 'urban'):
        p1 = Point(
            random.randint(int(p.x - int(CITY_SIZE_X / 3) - MPS),
                           int(p.x + int(CITY_SIZE_X / 3) + MPS)),
            random.randint(int(p.y - int(CITY_SIZE_Y / 3) - MPS),
                           p.y + int(CITY_SIZE_Y / 3) + MPS))
        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))

    if (defaultPlace[name][2] == 'rural'):
        p1 = Point(
            random.randint(int(p.x - int(CITY_SIZE_X / 3) - MPS),
                           int(p.x + int(CITY_SIZE_X / 3) + MPS)),
            random.randint(int(p.y - int(CITY_SIZE_Y / 3) - MPS),
                           p.y + int(CITY_SIZE_Y / 3) + MPS))
        if (random.randint(0, 1) == 0):
            p1.x = p1.x + int(CITY_SIZE_X / 3)
        else:
            p1.x = p1.x - (CITY_SIZE_X / 3)
        if (random.randint(0, 1) == 0):
            p1.y = p1.y + int(CITY_SIZE_Y / 3)
        else:
            p1.y = p1.y - int(CITY_SIZE_Y / 3)
        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))

    place = Rect(p1, p2)
    return place
示例#21
0
 def moveTo(self, x, y):
     """Moves this multi-item to point with coordinates x,y."""
     self.rect.moveTo(Point(x, y))
     self.refit()
示例#22
0
def createNatureFree(dx, dy):
    p1 = Point(random.randint(0, config['CITY_SIZE_X']),
               random.randint(0, config['CITY_SIZE_Y']))
    p2 = Point(p1.x + dx, p1.y + dy)
    place = Rect(p1, p2)
    return place
示例#23
0
#CREAZIONE EDIFICI
PLACESN = list(PLACES)
PLACES2 = list(PLACES)
print(PLACESN)
for i in range(0, len(PLACESN)):
    #place=createPlacePoint(Point(int(config['CITY_SIZE_X']/2),int(config['CITY_SIZE_Y']/2)))
    info = getDefaultPlace(PLACESN[i], defaultPlace)

    # not rural or free == center or urban
    if (not (info[2] == 'rural') or (info[2] == 'free')):
        #se no e gia tra gli edifici aggiungilo e rimuovlo
        #se non e tra gli edifici saltalo
        # If a building named in PLACESN does not exist, add it
        if (not contains(buildings, PLACESN[i])):
            place = createPlaceDefault(
                Point(int(config['CITY_SIZE_X'] / 2),
                      int(config['CITY_SIZE_Y'] / 2)), PLACESN[i])
            place.set_name(PLACESN[i])
            buildings.append(place)
            PLACES2.remove(PLACESN[i])
        else:
            pass

PLACESN = PLACES2

#print(PLACESN)

print('Building free house')
for i in range(0, int((homeNumber / 20)) - len(buildings)):
    place = createPlacePoint(
        Point(int(config['CITY_SIZE_X'] / 2), int(config['CITY_SIZE_Y'] / 2)))
    buildings.append(place)
示例#24
0
def createPlaceDefault(p, name):

    if not name in list(defaultPlace.keys()):
        name = 'unknown'

    mps = eval_eqn(defaultPlace[name][0])
    MPS = eval_eqn(defaultPlace[name][1])

    #print(name,mps,MPS)

    thirdx = config['CITY_SIZE_X'] // 3
    thirdx_up = thirdx + MPS
    thirdx_dn = thirdx - MPS

    thirdy = config['CITY_SIZE_Y'] // 3
    thirdy_up = thirdy + MPS
    thirdy_dn = thirdy - MPS

    if (defaultPlace[name][2] == 'free'):
        p1 = Point(random.randint(0, config['CITY_SIZE_X']),
                   random.randint(0, config['CITY_SIZE_Y']))
        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))

    if (defaultPlace[name][2] == 'center'):
        p1 = Point(random.randint(p.x - MPS, p.x + MPS),
                   random.randint(p.y - MPS, p.y + MPS))
        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))

    if (defaultPlace[name][2] == 'urban'):
        p1 = Point(random.randint(p.x - thirdx_dn, p.x + thirdx_up),
                   random.randint(p.y - thirdy_dn, p.y + thirdy_up))
        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))

    if (defaultPlace[name][2] == 'rural'):
        p1 = Point(random.randint(p.x - thirdx_dn, p.x + thirdx_up),
                   random.randint(p.y - thirdy_dn, p.y + thirdy_up))
        if (random.randint(0, 1) == 0):
            p1.x = p1.x + thirdx
        else:
            p1.x = p1.x - thirdx

        if (random.randint(0, 1) == 0):
            p1.y = p1.y + thirdy
        else:
            p1.y = p1.y - thirdy

        p2 = Point(random.randint(p1.x + mps, p1.x + MPS),
                   random.randint(p1.y + mps, p1.y + MPS))

    place = Rect(p1, p2)
    return place