Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.46 KB | None | 0 0
  1. import pdb
  2. import random
  3. import math
  4. # from app.simulated_annealing import SimulatedAnnealing
  5.  
  6. # Welcome message
  7.  
  8.  
  9. def welcome():
  10.     return """
  11.  Welcome to simple Simulated Annealing program using Python language.
  12.  First you must know how to input your value into this program.
  13.  
  14.  1. Input coordinate in first iterations with delimiters using ','
  15.    example: 22,26
  16.  2. Input range for random coordinate from start to end with delimiters using ':'
  17.    example: 22:44
  18.    (start is 22, end is 44)
  19.  3. Input Receiver point (TR) with dilimiters using ','
  20.    example: 22,23,25,44,90
  21.  4. Input Th as a normal value
  22.    example: -30
  23.  5. Input Coverage Area convert to decimal value
  24.    example: 2% to 0.02
  25.  6. Input Average RSSI as a normal value
  26.    example: -60
  27.  7. Input Total Iteration how long you want to calculate.
  28.    example: 10
  29.  
  30.  
  31.  by Gede Suka Arthawan
  32.    """
  33.  
  34. # Create randomize coordinate by start and end value
  35.  
  36.  
  37. def randomize(start, end):
  38.     new_coordinate = random.randint(start, end)
  39.     return new_coordinate
  40.  
  41. # Main function
  42.  
  43.  
  44. def main(**kwargs):
  45.     prev_rssi = kwargs['first_rssi']
  46.     prev_smax = 0
  47.     prev_coverage_area = kwargs['coverage_area']
  48.     blacklist = []
  49.     current_iteration = 0
  50.  
  51.     blacklist.append(kwargs['first_coordinate'])
  52.  
  53.     while True:
  54.         distances = []
  55.         new_rssi = []
  56.         sMax = 0
  57.         ranges = 0
  58.  
  59.         # Check current iteration and break it current iteration if same as total iteration
  60.         if current_iteration == kwargs['total_iteration']:
  61.             break
  62.  
  63.         new_x1 = randomize(kwargs['range_point_x1'][0],
  64.                            kwargs['range_point_x1'][1])
  65.         new_y1 = randomize(kwargs['range_point_y1'][0],
  66.                            kwargs['range_point_y1'][1])
  67.         new_coordinate = [new_x1, new_y1]
  68.  
  69.         # Cheking new coordinate is not in blacklist
  70.         if new_coordinate in blacklist:
  71.             continue
  72.  
  73.         # Save new coordinate into blacklist
  74.         blacklist.append(new_coordinate)
  75.  
  76.         # Checking length for receiver point X and Y
  77.         if len(kwargs['receiver_point_x2']) != len(kwargs['receiver_point_y2']):
  78.             print('Invalid length for Receiver value X or Y.')
  79.             break
  80.  
  81.           # Run calculate for distance as much as length of receiver point
  82.         for _d in range(len(kwargs['receiver_point_x2'])):
  83.             _tmp_distance = round(math.sqrt(
  84.                 (new_x1 - kwargs['receiver_point_x2'][_d])**2 + (new_y1 - kwargs['receiver_point_y2'][_d])**2), 2)
  85.             distances.append(_tmp_distance)
  86.  
  87.         # count sMax
  88.         sMax = max(distances)
  89.  
  90.         # Find new firstrange before calculate new rssi
  91.         first_range = round((kwargs['th'] * sMax) / prev_rssi, 2)
  92.  
  93.         # Run calculate for rssi as much as length of receiver point
  94.         for _r in range(len(kwargs['receiver_point_x2'])):
  95.             _tmp_rssi = round((kwargs['th'] / first_range) * distances[_r], 2)
  96.             new_rssi.append(_tmp_rssi)
  97.  
  98.         rssi = min(new_rssi)
  99.  
  100.         # count ranges
  101.         ranges = round((kwargs['th'] * sMax) / rssi, 2)
  102.  
  103.         # count covered area
  104.         covered_area = 0
  105.         for _ca in range(len(distances)):
  106.             if distances[_ca] < ranges:
  107.                 covered_area += distances[_ca]
  108.  
  109.         covered_area = round(covered_area, 2)
  110.  
  111.         # count coverege area
  112.         coverage_area = round((covered_area / kwargs['areas']), 2) * 100
  113.  
  114.         # Increment current iteration if all process is complated
  115.         current_iteration += 1
  116.  
  117.         print(f'''
  118.      ==> Data from previous iteration :
  119.          TH ----------------------------: {kwargs['th']}
  120.          Areas -------------------------: {kwargs['areas']}
  121.          RSSI --------------------------: {prev_rssi}
  122.          SMax --------------------------: {prev_smax}
  123.          Coverage area -----------------: {prev_coverage_area}
  124.  
  125.      ==> Result for current iteration :
  126.          Iteration ---------------------: {current_iteration}
  127.          Coordinate --------------------: {' | '.join(list(map(str, new_coordinate)))}
  128.          New distances -----------------: {' | '.join(list(map(str, distances)))}
  129.          New rssi ----------------------: {' | '.join(list(map(str, new_rssi)))}
  130.          RSSI --------------------------: {rssi}
  131.          SMax --------------------------: {sMax}
  132.          Ranges ------------------------: {ranges}
  133.          Covered area ------------------: {covered_area}
  134.          Coverage area -----------------: {coverage_area}
  135.          ''')
  136.  
  137.         prev_rssi = rssi
  138.         prev_smax = sMax
  139.         prev_coverage_area = coverage_area
  140.  
  141.  
  142. if __name__ == "__main__":
  143.     print(welcome())
  144.  
  145.     # first_coordinate   = input('Insert first coordinate: ')
  146.     # first_ranges       = float(input('Insert first ranges: '))
  147.     # first_rssi         = int(input('Insert first rssi: '))
  148.     # areas              = int(input('Insert area (m2): '))
  149.     # range_point_x1     = input('Insert range for random coordinate X1: ')
  150.     # range_point_y1     = input('Insert range for random coordinate Y1: ')
  151.     # receiver_point_x2  = input('Insert receiver point for X2: ')
  152.     # receiver_point_y2  = input('Insert receiver point for Y2: ')
  153.     # th                 = int(input('Insert Th : '))
  154.     # coverage_area      = float(input('Insert converage area: '))
  155.     # total_iteration    = int(input('Insert total iteration: '))
  156.  
  157.     first_coordinate = '22,26'
  158.     first_ranges = 182.4
  159.     first_rssi = -78
  160.     areas = 1715
  161.     range_point_x1 = '21:36'
  162.     range_point_y1 = '21:30'
  163.     receiver_point_x2 = '22,28,15,9,10'
  164.     receiver_point_y2 = '26,26,26,26,22'
  165.     th = -30
  166.     coverage_area = 0.02
  167.     total_iteration = 5
  168.  
  169.     first_coordinate = list(map(int, first_coordinate.split(',')))
  170.     receiver_point_x2 = list(map(int, receiver_point_x2.split(',')))
  171.     receiver_point_y2 = list(map(int, receiver_point_y2.split(',')))
  172.     range_point_x1 = list(map(int, range_point_x1.split(':')))
  173.     range_point_y1 = list(map(int, range_point_y1.split(':')))
  174.  
  175.     main(
  176.         first_coordinate=first_coordinate,
  177.         first_ranges=first_ranges,
  178.         first_rssi=first_rssi,
  179.         areas=areas,
  180.         range_point_x1=range_point_x1,
  181.         range_point_y1=range_point_y1,
  182.         receiver_point_x2=receiver_point_x2,
  183.         receiver_point_y2=receiver_point_y2,
  184.         th=th,
  185.         coverage_area=coverage_area,
  186.         total_iteration=total_iteration
  187.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement