勇闖新世界︰ W!o《卡夫卡村》變形祭︰圓局定向‧【補充一】

自然界裡的萬象並非都可以拿過來『實驗』的,所以『模擬』

Simulation is the imitation of the operation of a real-world process or system over time.[1] The act of simulating something first requires that a model be developed; this model represents the key characteristics or behaviors/functions of the selected physical or abstract system or process. The model represents the system itself, whereas the simulation represents the operation of the system over time.

Simulation is used in many contexts, such as simulation of technology for performance optimization, safety engineering, testing, training, education, and video games. Often, computer experiments are used to study simulation models. Simulation is also used with scientific modelling of natural systems or human systems to gain insight into their functioning.[2] Simulation can be used to show the eventual real effects of alternative conditions and courses of action. Simulation is also used when the real system cannot be engaged, because it may not be accessible, or it may be dangerous or unacceptable to engage, or it is being designed but not yet built, or it may simply not exist.[3]

Key issues in simulation include acquisition of valid source information about the relevant selection of key characteristics and behaviours, the use of simplifying approximations and assumptions within the simulation, and fidelity and validity of the simulation outcomes. ……

 

有時就成了唯一的選項了。其中又以『計算機模擬』

Computer simulation

A computer simulation is a simulation, run on a single computer, or a network of computers, to reproduce behavior of a system. The simulation uses an abstract model (a computer model, or a computational model) to simulate the system. Computer simulations have become a useful part of mathematical modeling of many natural systems in physics (computational physics), astrophysics, climatology, chemistry and biology, human systems in economics, psychology, social science, and engineering. Simulation of a system is represented as the running of the system’s model. It can be used to explore and gain new insights into new technology and to estimate the performance of systems too complex for analytical solutions.[1]

Computer simulations vary from computer programs that run a few minutes to network-based groups of computers running for hours to ongoing simulations that run for days. The scale of events being simulated by computer simulations has far exceeded anything possible (or perhaps even imaginable) using traditional paper-and-pencil mathematical modeling. Over 10 years ago, a desert-battle simulation of one force invading another involved the modeling of 66,239 tanks, trucks and other vehicles on simulated terrain around Kuwait, using multiple supercomputers in the DoD High Performance Computer Modernization Program[2] Other examples include a 1-billion-atom model of material deformation;[3] a 2.64-million-atom model of the complex maker of protein in all organisms, a ribosome, in 2005;[4] a complete simulation of the life cycle of Mycoplasma genitalium in 2012; and the Blue Brain project at EPFL (Switzerland), begun in May 2005 to create the first computer simulation of the entire human brain, right down to the molecular level.[5]

Because of the computational cost of simulation, computer experiments are used to perform inference such as uncertainty quantification.[6]

400px-Typhoon_Mawar_2005_computer_simulation_thumbnail

A 48 hour computer simulation of Typhoon Mawar using the Weather Research and Forecasting model

 

扮演的角色越來越重要。舉例而言『蒙地卡羅法』就是一種以機率統計理論為指南之通用且重要之數值計算方法。

Monte Carlo method

Monte Carlo methods vary, but tend to follow a particular pattern:

  1. Define a domain of possible inputs.
  2. Generate inputs randomly from a probability distribution over the domain.
  3. Perform a deterministic computation on the inputs.
  4. Aggregate the results.

For example, consider a circle inscribed in a unit square. Given that the circle and the square have a ratio of areas that is π/4, the value of π can be approximated using a Monte Carlo method:[6]

  1. Draw a square on the ground, then inscribe a circle within it.
  2. Uniformly scatter some objects of uniform size (grains of rice or sand) over the square.
  3. Count the number of objects inside the circle and the total number of objects.
  4. The ratio of the two counts is an estimate of the ratio of the two areas, which is π/4. Multiply the result by 4 to estimate π.

In this procedure the domain of inputs is the square that circumscribes our circle. We generate random inputs by scattering grains over the square then perform a computation on each input (test whether it falls within the circle). Finally, we aggregate the results to obtain our final result, the approximation of π.

There are two important points to consider here: Firstly, if the grains are not uniformly distributed, then our approximation will be poor. Secondly, there should be a large number of inputs. The approximation is generally poor if only a few grains are randomly dropped into the whole square. On average, the approximation improves as more grains are dropped.

Uses of Monte Carlo methods require large amounts of random numbers, and it was their use that spurred the development of pseudorandom number generators, which were far quicker to use than the tables of random numbers that had been previously used for statistical sampling.

 

220px-Pi_30KMonte Carlo method applied to approximating the value of π. After placing 30000 random points, the estimate for π is within 0.07% of the actual value. This happens with an approximate probability of 20%.

 

假使我們可以『模擬』熱點 AP 的 RSSI 讀取值,對定向『演算法』之實務將會有莫大的助益。就讓我們嘗試用『 scipy 』的程式庫製造類似的現象。由於我們不可能簡潔講解那麼大部頭的程式庫,此處只能列出幾個參考網址,請讀者自習的了︰

Numpy and Scipy Documentation

SciPy-數值計算庫

 

下面的程式範例,基本祇應用了一點點

【 scipy 程式庫用以產生自訂特定統計分佈基本類】

scipy.stats.rv_discrete

class scipy.stats.rv_discrete(a=0, b=inf, name=None, badvalue=None, moment_tol=1e-08, values=None, inc=1, longname=None, shapes=None, extradoc=None, seed=None)

A generic discrete random variable class meant for subclassing.

rv_discrete is a base class to construct specific distribution classes and instances for discrete random variables. It can also be used to construct an arbitrary distribution defined by a list of support points and corresponding probabilities.

Parameters:

a : float, optional

Lower bound of the support of the distribution, default: 0

b : float, optional

Upper bound of the support of the distribution, default: plus infinity

moment_tol : float, optional

The tolerance for the generic calculation of moments.

values : tuple of two array_like, optional

(xk, pk) where xk are integers with non-zero probabilities pk with sum(pk) = 1.

inc : integer, optional

Increment for the support of the distribution. Default is 1. (other values have not been tested)

badvalue : float, optional

The value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.

name : str, optional

The name of the instance. This string is used to construct the default example for distributions.

longname : str, optional

This string is used as part of the first line of the docstring returned when a subclass has no docstring of its own. Note: longname exists for backwards compatibility, do not use for new subclasses.

shapes : str, optional

The shape of the distribution. For example “m, n” for a distribution that takes two integers as the two shape arguments for all its methods If not provided, shape parameters will be inferred from the signatures of the private methods, _pmf and _cdf of the instance.

extradoc : str, optional

This string is used as the last part of the docstring returned when a subclass has no docstring of its own. Note: extradoc exists for backwards compatibility, do not use for new subclasses.

seed : None or int or numpy.random.RandomState instance, optional

This parameter defines the RandomState object to use for drawing random variates. If None, the global np.random state is used. If integer, it is used to seed the local RandomState instance. Default is None.

 

【參考程式】

pi@raspberrypi ~ $ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from scipy import stats
>>> import numpy as np

# 給定統計分佈
>>> xk = np.arange(11)
>>> pk = (0.02, 0.03, 0.01, 0.06, 0.05, 0.1, 0.28, 0.22, 0.07, 0.13, 0.02)
>>> custm = stats.rv_discrete(name='custm', values=(xk, pk))

# 繪出給定之統計分佈
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(1, 1)
>>> ax.plot(xk, custm.pmf(xk), 'ro', ms=12, mec='r')
[<matplotlib.lines.Line2D object at 0x21c2e30>]
>>> ax.vlines(xk, 0, custm.pmf(xk), colors='r', lw=4)
<matplotlib.collections.LineCollection object at 0x24e5550>
>>> plt.show()

# 用給定之統計分佈,產生隨機現象
>>> R = custm.rvs(size=100)
>>> R
array([ 8,  6,  7,  6,  7,  6,  8,  9,  7,  7,  8,  7,  7,  7,  3,  9,  6,
        3,  6,  5,  8,  4,  6,  8,  6,  6,  1,  9,  6,  9,  7,  7,  5,  6,
        7,  6,  5,  5,  9,  6,  4,  3,  1,  9,  5,  6,  6, 10,  7,  0,  6,
        4,  6,  7,  7,  4,  7,  6,  0,  6,  8,  5,  6,  3,  7,  6,  9,  6,
        9,  6,  1,  9,  7,  9,  7,  5,  6,  3,  4,  7,  9,  2,  1,  4,  3,
        6,  7,  5,  5,  9,  6,  3,  5,  6,  6,  6,  6,  7,  7,  5])
>>> 

# 畫出所模擬之 RSSI 讀取值
>>> fig, ax = plt.subplots(1, 1)
>>> xi = np.arange(100)
>>> def funcR(x):
...     return pk[R[x]]
... 

>>> funcR(3)
0.28

>>> xi
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

>>> vi = [funcR(i) for i in R]
>>> vi
[0.22, 0.07, 0.13, 0.07, 0.13, 0.07, 0.22, 0.22, 0.13, 0.13, 0.22, 0.13, 0.13, 0.13, 0.28, 0.22, 0.07, 0.28, 0.07, 0.28, 0.22, 0.22, 0.07, 0.22, 0.07, 0.07, 0.28, 0.22, 0.07, 0.22, 0.13, 0.13, 0.28, 0.07, 0.13, 0.07, 0.28, 0.28, 0.22, 0.07, 0.22, 0.28, 0.28, 0.22, 0.28, 0.07, 0.07, 0.07, 0.13, 0.07, 0.07, 0.22, 0.07, 0.13, 0.13, 0.22, 0.13, 0.07, 0.07, 0.07, 0.22, 0.28, 0.07, 0.28, 0.13, 0.07, 0.22, 0.07, 0.22, 0.07, 0.28, 0.22, 0.13, 0.22, 0.13, 0.28, 0.07, 0.28, 0.22, 0.13, 0.22, 0.22, 0.28, 0.22, 0.28, 0.07, 0.13, 0.28, 0.28, 0.22, 0.07, 0.28, 0.28, 0.07, 0.07, 0.07, 0.07, 0.13, 0.13, 0.28]

>>> ax.plot(xi, vi, 'ro', ms=12, mec='r')
[<matplotlib.lines.Line2D object at 0x2705f10>]
>>> plt.show()

 

【機率分佈對照參考圖】

2015-09-02-110308_652x563_scrot

 

Histogram-of-signal-strength

 

【產生 RSSI 對照比較】

2015-09-02-112821_1918x563_scrot

 

horst-history_1