Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pdb
- import random
- import math
- # from app.simulated_annealing import SimulatedAnnealing
- # Welcome message
- def welcome():
- return """
- Welcome to simple Simulated Annealing program using Python language.
- First you must know how to input your value into this program.
- 1. Input coordinate in first iterations with delimiters using ','
- example: 22,26
- 2. Input range for random coordinate from start to end with delimiters using ':'
- example: 22:44
- (start is 22, end is 44)
- 3. Input Receiver point (TR) with dilimiters using ','
- example: 22,23,25,44,90
- 4. Input Th as a normal value
- example: -30
- 5. Input Coverage Area convert to decimal value
- example: 2% to 0.02
- 6. Input Average RSSI as a normal value
- example: -60
- 7. Input Total Iteration how long you want to calculate.
- example: 10
- by Gede Suka Arthawan
- """
- # Create randomize coordinate by start and end value
- def randomize(start, end):
- new_coordinate = random.randint(start, end)
- return new_coordinate
- # Main function
- def main(**kwargs):
- prev_rssi = kwargs['first_rssi']
- prev_smax = 0
- prev_coverage_area = kwargs['coverage_area']
- blacklist = []
- current_iteration = 0
- blacklist.append(kwargs['first_coordinate'])
- while True:
- distances = []
- new_rssi = []
- sMax = 0
- ranges = 0
- # Check current iteration and break it current iteration if same as total iteration
- if current_iteration == kwargs['total_iteration']:
- break
- new_x1 = randomize(kwargs['range_point_x1'][0],
- kwargs['range_point_x1'][1])
- new_y1 = randomize(kwargs['range_point_y1'][0],
- kwargs['range_point_y1'][1])
- new_coordinate = [new_x1, new_y1]
- # Cheking new coordinate is not in blacklist
- if new_coordinate in blacklist:
- continue
- # Save new coordinate into blacklist
- blacklist.append(new_coordinate)
- # Checking length for receiver point X and Y
- if len(kwargs['receiver_point_x2']) != len(kwargs['receiver_point_y2']):
- print('Invalid length for Receiver value X or Y.')
- break
- # Run calculate for distance as much as length of receiver point
- for _d in range(len(kwargs['receiver_point_x2'])):
- _tmp_distance = round(math.sqrt(
- (new_x1 - kwargs['receiver_point_x2'][_d])**2 + (new_y1 - kwargs['receiver_point_y2'][_d])**2), 2)
- distances.append(_tmp_distance)
- # count sMax
- sMax = max(distances)
- # Find new firstrange before calculate new rssi
- first_range = round((kwargs['th'] * sMax) / prev_rssi, 2)
- # Run calculate for rssi as much as length of receiver point
- for _r in range(len(kwargs['receiver_point_x2'])):
- _tmp_rssi = round((kwargs['th'] / first_range) * distances[_r], 2)
- new_rssi.append(_tmp_rssi)
- rssi = min(new_rssi)
- # count ranges
- ranges = round((kwargs['th'] * sMax) / rssi, 2)
- # count covered area
- covered_area = 0
- for _ca in range(len(distances)):
- if distances[_ca] < ranges:
- covered_area += distances[_ca]
- covered_area = round(covered_area, 2)
- # count coverege area
- coverage_area = round((covered_area / kwargs['areas']), 2) * 100
- # Increment current iteration if all process is complated
- current_iteration += 1
- print(f'''
- ==> Data from previous iteration :
- TH ----------------------------: {kwargs['th']}
- Areas -------------------------: {kwargs['areas']}
- RSSI --------------------------: {prev_rssi}
- SMax --------------------------: {prev_smax}
- Coverage area -----------------: {prev_coverage_area}
- ==> Result for current iteration :
- Iteration ---------------------: {current_iteration}
- Coordinate --------------------: {' | '.join(list(map(str, new_coordinate)))}
- New distances -----------------: {' | '.join(list(map(str, distances)))}
- New rssi ----------------------: {' | '.join(list(map(str, new_rssi)))}
- RSSI --------------------------: {rssi}
- SMax --------------------------: {sMax}
- Ranges ------------------------: {ranges}
- Covered area ------------------: {covered_area}
- Coverage area -----------------: {coverage_area}
- ''')
- prev_rssi = rssi
- prev_smax = sMax
- prev_coverage_area = coverage_area
- if __name__ == "__main__":
- print(welcome())
- # first_coordinate = input('Insert first coordinate: ')
- # first_ranges = float(input('Insert first ranges: '))
- # first_rssi = int(input('Insert first rssi: '))
- # areas = int(input('Insert area (m2): '))
- # range_point_x1 = input('Insert range for random coordinate X1: ')
- # range_point_y1 = input('Insert range for random coordinate Y1: ')
- # receiver_point_x2 = input('Insert receiver point for X2: ')
- # receiver_point_y2 = input('Insert receiver point for Y2: ')
- # th = int(input('Insert Th : '))
- # coverage_area = float(input('Insert converage area: '))
- # total_iteration = int(input('Insert total iteration: '))
- first_coordinate = '22,26'
- first_ranges = 182.4
- first_rssi = -78
- areas = 1715
- range_point_x1 = '21:36'
- range_point_y1 = '21:30'
- receiver_point_x2 = '22,28,15,9,10'
- receiver_point_y2 = '26,26,26,26,22'
- th = -30
- coverage_area = 0.02
- total_iteration = 5
- first_coordinate = list(map(int, first_coordinate.split(',')))
- receiver_point_x2 = list(map(int, receiver_point_x2.split(',')))
- receiver_point_y2 = list(map(int, receiver_point_y2.split(',')))
- range_point_x1 = list(map(int, range_point_x1.split(':')))
- range_point_y1 = list(map(int, range_point_y1.split(':')))
- main(
- first_coordinate=first_coordinate,
- first_ranges=first_ranges,
- first_rssi=first_rssi,
- areas=areas,
- range_point_x1=range_point_x1,
- range_point_y1=range_point_y1,
- receiver_point_x2=receiver_point_x2,
- receiver_point_y2=receiver_point_y2,
- th=th,
- coverage_area=coverage_area,
- total_iteration=total_iteration
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement