% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{integ2d}
\section*{\hspace*{-1.6cm} integ2d}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Approximate 2-D integral.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
som = integ2d(MAT)
som = integ2d(MAT,x)
som = integ2d(MAT,x,y)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty integ2d} approximates the 2-D integral of matrix {\ty MAT}
        according to abscissa {\ty x} and ordinate {\ty y}.\\

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c}
Name & Description & Default value\\
\hline
        {\ty MAT} & {\ty (M,N)} matrix to be integrated\\
        {\ty x}   & {\ty N}-row-vector indicating the abscissa integration path
                                & {\ty (1:N)}\\
        {\ty y}   & {\ty M}-column-vector indicating the ordinate integration path
                                & {\ty (1:M)}\\
 \hline {\ty som} & result of integration\\

\hline
\end{tabular*}

\end{minipage}
\vspace*{1cm}

{\bf \large \sf Example}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Consider the scalogram of a sinusoidal frequency modulation of 128 points,
and compute the integral over the time-scale plane of the scalogram :
\begin{verbatim}
         S = fmsin(128,0.2,0.3);
         [TFR,t,f] = tfrscalo(S,1:128,8,0.1,0.4,128,1);
         Etfr = integ2d(TFR,t,f)
         Etfr =
                128.0000
\end{verbatim}
We find for {\ty Etfr} the value of the signal energy, which is the
expected value since the scalogram preserves energy.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
integ.
\end{verbatim}
\end{minipage}


\newtheorem{ques}{Question}
\newtheorem{answ}{Answer}
\newtheorem{note}{Note}

\chapter{ Cross-Entropy Optimization can solve many hard problems on combinatorial optimization.
}
\section{Motivation}

      \subsection{Rare-event simulation}

      Suppose that there is a system with
      \Terms{state space} $\D \subseteq \R^n$ for some $n\in \N_+,$
      and \Terms{performance function} $f:\D \mapsto \R.$ The state
      $\X$ is assumed to be a random variable distributed in accordance
       with a probability density function (pdf)
      $p(\x).$ We want to compute the probability of the random event
      that $\EE=\{\x\in \D:\ f(\x)\le \gamma\}$ for a \Stress{small} constant
      $\gamma\in \R.$ Mathematically, we need to compute
      \begin{equation}\label{eq:Objective_RES}
            \PP(\EE)=\int_{\x\in \D} \1_{\{y: f(y) \le \gamma\}}(\x)p(\x) d\x=\ \E_{p(\cdot)} \Big[ \1_{\{y: f(y) \le \gamma\}}(\X) \Big].
      \end{equation}

\begin{note}
      The motivation is how to calculate $ P\left(I_{\left\{f(x) > \gamma\right\}}\right) $ efficiently when
      the $\gamma$ is large and the respective event $\varepsilon$ is rare.
\end{note}

      \subsection{Change of measure}

      For any other pdf $g(\cdot)$ with $g(\x)=0\implies p(\x)=0\ \forall \x \in \D,$ we have
      \begin{equation}\label{eq:CoM_RES}
            \PP[\EE]\!=\!\E_{p(\cdot)}\big[\1_{\{y:\ f(y)\le \gamma\}}(\X)\big] \!=\!\E_{g(\cdot)}\Big[\1_{\{y:\ f(y)\le \gamma\}}(\X)\frac{p(\X)}{g(\X)}\Big].
      \end{equation}
      The CMC estimator of \eqref{eq:Objective_RES} with CoM $g(\cdot)$ is
      \begin{equation}\label{eq:CoM_CMC}
            \frac{1}{N} \sum_{k=1}^{N} \1_{\{y:\ f(y)\le \gamma\}}(\X^{(k)})\frac{p(\X^{(k)})}{g(\X^{(k)})},
      \end{equation}
      where $\X^{(1)}, \ldots, \X^{(N)}$ are $i.i.d.$ from $g(\cdot),$
      and $N\in \N_+$ is the \Terms{sample size}.\\
      The optimal CoM is
      \begin{equation}\label{eq:OptCoM_RES}
                  g^*(\x)=\1_{\{y:\ f(y)\le \gamma\}}(\x) \frac{p(\x)}{\PP[\EE]}, \quad \forall \x \in \D
      \end{equation}
      with zero variance, but infeasible!

\begin{note}
      If we change the measure to one described by the density funtion
      $$ g^*(x) = I_{\left\{f(x) > \gamma\right\}}\left(x\right) \frac{p\left(x\right)}{P\left(\varepsilon\right)}, $$
      the new probability of $\varepsilon$ satisfies $\tilde P (\varepsilon) = 1$.
      And if we can generate some samples from the new probability measure, the finite average may equal exactly the expectation
      for any size of samples. (But how can we calculate the original probability of $\varepsilon$ ?)
\end{note}

      \subsection{Stochastic program}

		A smart idea is to find a pdf from a given family, which well mimics
		\eqref{eq:OptCoM_RES}. This forms the following optimization
		problem
		\begin{equation}\label{eq:SP_RES}
		\min_{\theta\in \Theta}\quad  \DD\Big[g^*\ \Big|\Big|\ g_{\theta}\Big],
		\end{equation}
		where
		\begin{itemize}
			\item $\DD[h\ ||\ r]\ge 0$ for any two pdfs $h,r,$ and  $\DD[h \ || \ r]=0$ if and only if $h=r$ almost surely,
			typically $\DD[\cdot ||\cdot]$ is chosen to be the \Terms{Kullback-Leibler divergence}, i.e.,
			\begin{itemize}\label{eq:KL-distance}
				\item $\DD\big[g^*\big|\big|\ g_{\theta}\big]=\E_{g^*}\big[\ln \frac{g^*(\X)}{g_{\theta}(\X)}\big]$
			\end{itemize}
			\item $\F=\{g_{\theta}:\ \theta\in \Theta\}$ is a family of pdfs
			defined on $\D$ and $\Theta\subseteq \R^m$ is a \Stress{convex}
			parametric set for some $m\in \N_+.$
		\end{itemize}
		With Kullback-Leibler divergence, \eqref{eq:SP_RES} is equivalent to, for \Stress{any} $\theta'\in \Theta,$
		\begin{equation}\label{eq:SP_RES_KL}
			\max_{\theta \in \Theta}\ \E_{g^*} \Big[ \ln g_{\theta}(\X)\Big]
			\!\iff\! \max_{\theta \in \Theta}\ \E_{g_{\theta'}} \Big[\1_{\{y:\ f(y)\le \gamma\}}(\X)\frac{p(\X)}{g_{\theta'}(\X)} \ln g_{\theta}(\X)\Big]
		\end{equation}

		\subsection{Stochastic iterative search}

		The difficulty of solving \eqref{eq:SP_RES_KL} by traditional
		techniques is that $\1_{\{y:\ f(y)\le \gamma\}}(\x)=0$ for most
		$\x\in\D \subseteq  \R^n$.  To overcome this difficulty,
		\Terms{Rubinstein (1997)} proposed a stochastic
		iterative search, called \Terms{Cross Entropy method}.
		The method works like peeling onions. Starting from an arbitrarily
		fixed $\theta_0\in \Theta,$ the method then iterates over the following
		three steps, for every $t=0,1, 2, \ldots, $ until $\gamma_t \ge \gamma,$
		\begin{itemize}
			\item generate $\X_t^{(1)}, \ldots, \X_t^{(N)}$ from pdf
			$g_{\theta_t}(\cdot),$ and non-decreasingly sort them as $\X_t^{[1]}, \ldots, \X_t^{[N]},$ i.e.,
			$f\big(\X_t^{[1]}\big)\le \cdots \le f\big(\X_t^{[N]}\big),$
			\item put $\gamma_t=f\big(\X_t^{[\lceil\alpha \cdot N\rceil]}\big),$ and
			\begin{equation}\label{eq:CE_Estimate}
				\theta_{t\!+\!1} = \arg\max_{\theta\in \Theta}\frac{1}{N}\sum_{k=1}^{N} \1_{\{y:\ f(y)\le \gamma_t\}}(\X_t^{[k]})\frac{p(\X_t^{[k]})}{g_{\theta_t}(\X_t^{[k]})} \ln g_{\theta}(\X_t^{[k]}),
			\end{equation}
			%\item put $\theta_{t\!+\! %1}=\theta_t+\rho(\hat{\theta}_{t\!+\!1}-\theta_t),$
		\end{itemize}
		where $\alpha\in (0,1), N \in \N_+$ are parameters that need to be finely tunned.

                  By the Cross-Entropy method, one can eventually find a $\theta^*\in \Theta$
                  such that $g_{\theta^*}$ well approximates the optimal CoM $g^*$ in \eqref{eq:OptCoM_RES}. Then, the probability $\PP[\EE]$ can be well estimated by CMC with CoM $g_{\theta^*}.$\\[1ex]
			\Note{How does it work?}\\[1ex]
			\begin{itemize}
				\item $\theta_{t\!+\!1}$ in \eqref{eq:CE_Estimate} is a stochastic (optimal) solution to
				\begin{equation}\label{eq:CE_Iter_Obj}
				\max_{\theta\in \Theta}\ \int_{\x\in \D} \1_{\{y:\ f(y)\le \gamma_t\}}(\x)p(\x) \ln g_{\theta}(\x)d\x,
				\end{equation}
				therefore, $g_{\theta_{t\!+\!1}}$ may well approximate
				the truncate density
				\begin{equation}\label{eq:CE_Iter_Obj_Truncate}
					\frac{\1_{\{y:\ f(y)\le \gamma_t\}}(\x)p(\x)}{
						\int_{\mathbf{z}\in \D} \1_{\{w:\ f(w)\le \gamma_t\}}(\mathbf{z})p(\mathbf{z}) d \mathbf{z}
						}\quad \forall \x\in \D,
				\end{equation}
				\item $\gamma_t=f\big( \X_t^{[\lceil \alpha \cdot N \rceil]} \big)$
				estimates the $\lceil (1-\alpha) \cdot N \rceil$-th order statistic of
				$f(\X)$ with $\X \sim g_{\theta_t},$ which are expected to decrease
				with $t.$
			\end{itemize}

\section{For continuous discrete optimization}
\subsection{General idea}

	\Terms{Rubinstein (1999)} extends the idea for rare event simulation into optimization. Given an abstract optimization problem
	\begin{equation*}
		\min_{\x \in \D}\ F(\x),
	\end{equation*}
	for a cost function $F(\x),$ and feasible region $\D\subseteq \R^n,$ we can
	\Stress{solve the program through constructing a probabilistic distribution concentrating on optimal solutions}. Mathematically, we can then solve the following program
	\begin{equation}\label{eq:Optimization_Obj}
		\max_{\theta \in \Theta}\ \E_{g_{\theta}} \big[\1_{\{y:\ F(y)\le \gamma\}}(\X)\big],
	\end{equation}
	where we only need to specify a family $\F=\{g_{\theta}:\ \theta\in \Theta\}$ of pdfs.
	The $\gamma$ can be the minimum cost value. In the whole optimization procedure, we do not need knowledge about $\gamma.$

      \subsection[Alg]{Algorithm}

		\Note{Input (Precondition):} a pdf family $\F=\{g_{\theta}:\ \theta\in \Theta\},$ an initial pdf index $\theta_0\in \Theta,$
		a smooth parameter $\rho\in (0,1),$ an elite rate $\alpha\in (0,1),$
		an initial solution $\x\in \D,$ and a sample size $N\in \N_+$\\[1ex]
		\Note{Output (Postcondition):} $\x$\\[1ex]
		\Note{Pseudo code (iterates over the following steps):}
		\begin{itemize}
			\item independently generate $\X_t^{(1)}, \ldots, \X_t^{(N)}$ from pdf
			$g_{\theta_t}(\cdot),$ and non-decreasingly sort them as $\X_t^{[1]}, \ldots, \X_t^{[N]},$ i.e.,
			$F\big(\X_t^{[1]}\big)\le \cdots \le F\big(\X_t^{[N]}\big),$
			\item if $F\big(\x\big)> F\big(\X_t^{[1]}\big), \x=\X_t^{[1]},$
			\item $\gamma_t=F\big(\X_t^{[\lceil\alpha \cdot N\rceil]}\big),$ and
			\begin{equation}\label{eq:CE_Estimate_Optimization}
			\hat{\theta}_{t\!+\!1} = \arg\max_{\theta\in \Theta}\frac{1}{N}\sum_{k=1}^{N} \1_{\{y:\ F(y)\le \gamma_t\}}(\X_t^{[k]}) \ln g_{\theta}(\X_t^{[k]}),
			\end{equation}
			\item $\theta_{t\!+\!1}=\theta_t+\rho\cdot (\hat{\theta}_{t\!+\!1}-\theta_t).$
		\end{itemize}

\begin{ques}
      Why we use a convex combination between two iterative steps?
\end{ques}
\begin{answ}
      The objective is to prevent a too fast convergence.
\end{answ}

\begin{ques}
      Does this algorithm work for a continuous objective function with millions' of arguments?
\end{ques}
\begin{answ}
      Yes, but we must consider a more specified parameter space to approximate the final distribution.
      For instance, if Gauss-type distributions require so many parameters to exceed the memory,
      we can try uniform distributions.
\end{answ}

    \subsection[continuous]{Continuous optimization}

    		Applying the Cross-Entropy algorithm to continuous problem, we
    		need to select a suitable family of pdfs.\\[1ex]
    		\Note{Optimization with Gaussian pdfs (Normal distribution)}
    		\begin{itemize}
    			\item $\Theta=\{(\mu, \Sigma): \mu\in \R^n, \Sigma\in \R^{n\times n} \text{ is semipositive definite}\}, g_{(\mu,\Sigma)}=\mathcal{N}(\mu, \Sigma)$ is Normal distribution with mean $\mu$ and co-variance matrix $\Sigma,$
    			\item $\hat{\theta}_{t\!+\!1}=(\hat{\mu}_{t\!+\!1}, \hat{\Sigma}_{t\!+\!1}),$
    			the mean and co-variance matrix of \Terms{elite} samples $\X_t^{[1]}, \ldots, \X_t^{[\lceil \alpha \cdot N \rceil]}$ in iteration $t,$
    			\item $\mu_{t+1}=\mu_t + \rho (\hat{\mu}_{t\!+\!1}-\mu_t), \Sigma_{t\!+\!1}=\Sigma_t+\rho(\hat{\Sigma}_{t\!+\!1}-\Sigma_t),$
    			\item we can employ a small constant $\delta>0$ to calibrate $\Sigma$ so as to avoid
    			overflow in practice, i.e., if any element in $\Sigma_{t\!+\!1}$ is smaller than
    			$\delta,$ then set it to be $\delta.$

    		\end{itemize}

\begin{ques}
      What is the performance of this algorithm by comparison with the simulated annealing method?
\end{ques}
\begin{answ}
      This algorithm performance better in general.
\end{answ}

\begin{ques}
      Can a known local minimum assist this algorithm?
\end{ques}
\begin{answ}
      I don't think so.
\end{answ}

    \subsection[Discrete]{Dicrete optimization}

    	We generally consider combinatorial optimization,
    	\[\D\subseteq \underbrace{\A\times\A\times \cdots \times \A}_{n}\] for some $\A$ with $|\A|<\infty$ and $n\in \N.$ \\[1ex]
    	\Note{Optimization with univariate marginal distributions}
    	\begin{itemize}
    		\item $\Theta=\big\{\theta = (p_{i,j})_{n\times |\A|}:\ p_{i,j}\ge 0, \sum_{j=1}^{|\A|} p_{i,j}=1, \forall i=1,\ldots,n, j=1,\ldots, |\A|\big\}, g_{\theta}$ is now a probability mass function
    		\item $\hat{\theta}_{t\!+\!1}=(\hat{p}_{i,j}^{t\!+\!1})_{n\times |\A|},$
    		$\hat{p}_{i,j}^{t\!+\!1}$ is the empirical frequency of the $j$-th element of $\A$ in the $i$-th position of the \Terms{elite} samples $\X_t^{[1]}, \ldots, \X_t^{[\lceil \alpha \cdot N \rceil]}$ in iteration $t,$
    		\item $p_{i,j}^{t\!+\!1}=p_{i,j}^{t} + \rho (\hat{p}_{i,j}^{t\!+\!1}-p_{i,j}^{t}),$
    	\end{itemize}

    \section[Theory]{Theory for discrete optimization}
    \subsection[Process]{Underlying stochastic process}

    	We call $\big(Y_t\big)_{t=0,1,2,\ldots,}$ a \Note{Markov Chain} (MC)
    	with \Note{state space} $\mathcal{Y}$ if every random variable
	(or vector) $Y_t$ is defined on probabilistic space
    	$\big(\Y, \A, \mu\big),$ and
    	\[
    	\PP\big[Y_{t+1}\in B_{t+1}\big|\ Y_{t}\in B_{t}, \ldots, Y_0\in B_{0}\big]=\PP \big[Y_{t+1}\in B_{t+1}\big|\ Y_{t}\in B_{t}\big],
    	\]
	where $B_0,\ldots, B_{t+1}\in \A$ are $\mu$-measurable sets.\\[1ex]

	\hspace{0.5cm}If
	\[
    	\PP \big[Y_{t+k}\in B\big|\ Y_{t}\in B'\big]=\PP \big[Y_{k}\in B\big|\ Y_{0}\in B'\big],
    	\]
	for any $t,k\in \N,$ any $B,B'\in \A,$ we call the Markov chain
	\Note{time-homogeneous}, otherwise \Note{time-inhomogeneous}. \\[1ex]

    	In the discrete-state case, one can enumerate all the sates as
	$0,1,\ldots,K$ ($K\in \N\cup \{\infty\}$). Then, for each step
	$t\in \N,$ we can use a matrix $Q_t\in [0,1]^{K\times K}$ to
	describe the transition of states. For time-homogeneous MC,
	$Q_t\equiv Q,$ where $Q=\big(q_{i,j}\big)_{K\times K}$ is a
	constant matrix with $q_{i,j}$ describing the probability of
	moving to state $j$ from state $i.$\\[1ex]

	\hspace{0.5cm}For discrete-state time-homogeneous MC, one
	can easily show that
	\[
	q^{(n)}_{i,j}:=\PP[Y_{n}=j\mid Y_{0}=i]=\sum_{k=1}^{K}
	q_{ik}\PP[Y_{n}=j\mid Y_{1}=k]=\sum_{k=1}^{K}
	q_{ik}q^{(n-1)}_{k,j},
	\]
	\[
	q^{(n)}_{i,j}=\sum_{k=1}^{K}
	q^{(m)}_{ik}q^{(n-m)}_{k,j},
	\]
	where $1\le m<n.$ Therefore, the $n$-step transition matrix
	$Q^{(n)}=(q_{i,j}^{(n)})=Q^n.$\\[1ex]

	\hspace{0.5cm}A probability mass function $\pi=(\pi_j)_K$ on $\Y$ is a
	\Note{limiting distribution (or stationary distribution)} if
	$
	\pi_j=\lim_{n\to \infty} q_{i,j}^{(n)}
	$
	for any $i,j=1,\ldots, K.$
\hspace{0.5cm}In the case that the state space $\Y$ is countable, the
process is called a \Note{discrete-state} Markov chain.

    	A Markov chain is said to be \Note{irreducible} if for any
	two states $i,j\in \Y,$ it exists $n_1, n_2\in \N$ such that
	$q_{i,j}^{(n_1)}>0$ and $q_{j,i}^{(n_2)}>0.$ A Markov chain
	is said to be \Note{aperiodic} if for every $i\in \Y,$ it exists a $n\in \N$ such that $q_{i,i}^{(n')}>0$	for any $n'\ge n$ (i.e. ``period" is 1). \\[1ex]
	\hspace{0.5cm}A state $i$ is \Note{transient} if $\PP[\min\{m\ge 1:\  Y_{m}=i\mid Y_0=i\}<\infty]<1,$
	otherwise it is called a \Note{recurrent}. For a recurrent state
	$i,$ if the mean recurrence time $\Stress{M_i=\sum_{n=1}^{\infty} n\PP[\min \{m\ge 1:\  Y_{m}=i\mid Y_0=i\}=n]}<\infty,$ $i$ is said to be \Note{positive recurrent}, otherwise \Note{null recurrent}.\\[1ex]
	\Note{Theorem (Steady analysis for discrete-state time-homogeneous MC)}\\
	An irreducible MC has a limiting distribution $\pi=(\pi_j)_K$ if and only if
	it is aperiodic and all of its states are positive recurrent. Moreover, $\pi_j=C/M_j,$ for every state $j=1,\ldots, K,$ and $C$ is
	a normalizing constant.\\[1ex]
	\hspace{0.5cm}When the limiting distribution $\pi$ exists,
	\[
	\lim_{n\to\infty}Q^{(n)}=\left(
	\begin{array}{c}
	\pi\\
	\ldots\\
	\pi
	\end{array}
	\right),
	\quad \pi=\pi\cdot Q.
	\]

	The above steady analysis applies to time-homogeneous MC. Actually, for time-inhomogeneous MC, the limiting distribution may also hold. However, this requires the so-called \Note{detailed balance}. \\[1ex]
	\Note{Theorem (Steady analysis under detailed balance)}\\
	Assume that there is a distribution $\pi=(\pi_k)_K,$ such that
	for any $n\in \N, i,j=1,\ldots,K,$
	\[
	\pi_j=\sum_{i=1}^{K} \pi_i\cdot \PP[Y_{n+1}=j\mid Y_{n}=i],
	\]
	then $\pi$ is a limiting distribution of the MC.\\[1ex]
	\Note{Remark:} The famous Markov Chain Monte-Carlo algorithm is
	designed based on this theorem.

\begin{note}
      For a time-homogeneous discrete-state Markov chain, we can use the limiting distribution
      with respect to the transition matrix to determine the recurrent state of the M.C..
\end{note}

\begin{ques}
      For a Markov chain, if we don't know the transition matrix beforehand, can we decide the recurrent state?
\end{ques}
\begin{answ}
      Other information about the transition must be available.
\end{answ}

    \subsection[Results]{Available results for discrete optimization}

    	So far, theory is available only for discrete case. For continuous case, the
    	theory is still almost blank. The following theory is for discrete optimization.
    	They do not impose any restrictions on the problems. Therefore, they apply to
    	all discrete problems.\\[1ex]
    	\Note{Theorem (Finite reachability, [Wu \& Kolonko, 2014, TEVC])}\\[1ex]
    	$\PP [\exists T:\ \X_T \cap \mathcal{S}^*\neq \emptyset]\to 1$ as $\rho \to 0$ or $N\to \infty.$\\[2ex]
    	\Note{Theorem (Convergence of solutions, [Wu \& Kolonko, 2014, TEVC])}\\[1ex]
    	$\PP [\exists \x \in \D\ \exists T\in \N_+\ \forall t\ge T:\ \X_t=(\x, \x,\ldots, \x)\in \D^{N}]=1$ (Genetic drift).\\[2ex]
    	\Note{Theorem (Convergence of distributions, [Wu \& Kolonko, 2014, TEVC])}\\[1ex]
    	$\PP [\exists \theta\in \Theta:\ \lim_{t\to \infty} \theta_t =\theta]=1.$

\begin{note}
      This algorithm can be applied to both continuous and discrete optimization problems.
      For discrete problem, some convergent results can be obtained.
      There are still many open issues for continuous problems.
\end{note}

    		The following lists more theory for particular problems.\\[1ex]
    		\Note{Theorem (Runtime for simple problems, [Wu, Kolonko \& M{\"ohring}, 2017, TEVC])}\\[1ex]
    		\begin{itemize}
    			\item \Terms{OneMax:} $\forall \epsilon>0,\ \PP[\tau \in O(n^{1.5+\epsilon})]\ge 1-e^{-\Omega(n^\epsilon)}.$
    			\item \Terms{LeadingOnes:} $\forall \epsilon>0,\ \PP[\tau \in O(n^{2+\epsilon})]\ge 1-e^{-\Omega(n^\epsilon)}.$
    		\end{itemize}
    		\Note{Theorem (Runtime for TSP, [Wu, M{\"ohring} \& Lai, 2017, ArXiv])}\\[1ex]
    		\begin{itemize}
    			\item Simple TSP: $\forall \epsilon>0,\ \PP[\tau \in O(n^{3+\epsilon}\ln n)]\ge 1-e^{-\Omega(n^{\epsilon})}$
    			\item Convex TSP: $\forall \epsilon>0,\ \PP [\tau \in O(n^2m^{5+\epsilon})] \ge 1-e^{-\Omega(m^\epsilon)}$
    			\item General TSP: $\forall \epsilon>0,\ \PP [\tau \in O(n^3m^{5+\epsilon}+n^{3k}m^{\epsilon})]\ge 1-e^{-\Omega(m^\epsilon)}$
    		\end{itemize}
    		where the TSP instance is positioned with $m\times m$-grid.

    	\section[Extension]{Extension}
    		The idea can be made more general. One can use more selection
    		methods and estimation approaches. This results in the more general
    		idea \Terms{Model-based search}, see, e.g., \Def{[Wu, 2016, Thesis], [Zloch et al 2006].}\\[1ex]
    		\Note{Solution $v.s.$ Model}
    		\begin{itemize}
    			\item \Stress{Solution-based:} genetic algorithm, simulated annealing, gradient decent, particle swarm optimization, etc
    			\item \Stress{Model-based:} cross-entropy algorithm, ant colony optimization, estimation of distribution algorithms (MIMIC, BOA, compact GA, etc).
    			\item Solution-based is efficient, easy to implement
    			\item Model-based is more stable, higher quality
    		\end{itemize}
\begin{note}
      This algorithm is one kind of more general model-based algorithms,
      which is quite different from the classical algorithms based on local information.
      The solution is more stable and with higher quality by comparison with the classical ones,
      but may cost longer time.
\end{note}

\begin{ques}
      Are the derivatives of the objective function helpful to this algorithm? How can this algorithm
      combine with the gradient-descent-type algorithm?
\end{ques}
\begin{answ}
      We are considering that.
\end{answ}


%!TEX root = RBM.tex

\subsection{Theoretical Anaylsis}

% Since TAP's result is relatively irrelevant to the run time and resources. We use the value estimated in the last subsection.

% \subsubsection{Practice}
% \para{Complexity}
% For horizontal comparison on complexity, we let the correctness \& stability to be approximately the same (i.e. the correctness the AIS could achieve in 2 seconds), and compare the run time for three algorithms.

% We have conducted 20 experiments for the acquistion of $Z(\theta)$.

% From our practice, we have found that xxx gives the most agreeable result while TAP completely underestimate the value.

% \para{Correctness \& Stability}
% For horizontal comparison on the correctness \& stability, we allow the same run time for AIS and RTS algorithms to compute (i.e. 10 seconds per experiment). We have conducted 20 experiments for the acquistion of $Z(\theta)$.

% From our practice, we have found that xxx gives the most agreeable result while TAP completely underestimate the value.

% We also notice that the variance of ... is the smallest which indicates it has the best stability.

\para{RTS}
From a theretical perspective, we have proven that the bias \& the variance of the RTS method are to be:
\begin{equation}
E{[ log \hat{Z}_{k}^{RTS} ]} - E{[Z_{k}]} \approx \frac{1}{2}{[\frac{\sigma^{2}_{1}}{\hat{c}^{2}_{1}}-\frac{\sigma^{2}_{k}}{\hat{c}^{2}_{k}}]}
\end{equation}
\begin{equation}
Var{[ log \hat{Z}_{k}^{RTS} ]} \approx \frac{\sigma^{2}_{1}}{\hat{c}^{2}_{1}} + \frac{\sigma^{2}_{k}}{\hat{c}^{2}_{k}} - \frac{2\sigma_{1k}}{\hat{c}_{k}\hat{c}_{1}}
\end{equation}
where $\sigma^{2}_{k} = Var{[\hat{c}_{k}]}$ and $\sigma_{1k} = Cov{[\hat{c}_{1},\hat{c}_{k}]}$

This has shown that the bias of RTS has no definite sign.

\para{AIS}
However, in AIS, Neal and Jarzynski et al.\cite{neal2001annealed,nonequilibrium} have shown that if we want the result to be unbiased, we would have to let $M=1$ in the iteration, which by doing so have lost the advantage of AIS. That is to say, on the other hand, if $M > 1$, we would have a negative bias due to Jenson Inequality.

From the result of our practice, we see when AIS and RTS show same good performance in estimation, RTS's efficiency is a little bit lower than that of AIS's.

\para{TAP}
Although TAP shows the best efficiency, its results are the most disapointing. Apparantly TAP has underestimate the value of the partition function.

We did not analyze deeply on the reasons why it failed to perform a satisfying result, but our intuition tells that maybe it is because the Legendre transform. In our practice, we only took the Legendre transform to the 2nd order, which might result in the underestimation.


% Copyright 2011-2015 David Hadka.  All Rights Reserved.
%
% This file is part of the MOEA Framework User Manual.
%
% Permission is granted to copy, distribute and/or modify this document under
% the terms of the GNU Free Documentation License, Version 1.3 or any later
% version published by the Free Software Foundation; with the Invariant Section
% being the section entitled "Preface", no Front-Cover Texts, and no Back-Cover
% Texts.  A copy of the license is included in the section entitled "GNU Free
% Documentation License".

\chapter{Example: Knapsack Problem}

In this chapter, we will walk through a complete example of creating a new optimization problem and solving it using the MOEA Framework.  This example serves as a review of the topics learned thus far.  We will also introduce several new concepts such as constraint handling.

The problem we will be solving is the multiobjective version of the knapsack problem.  The knapsack problem (discussed in much detail at \webpage{http://en.wikipedia.org/wiki/Knapsack_problem}) is a famous combinatorial problem that involves choosing which items to place in a knapsack to maximize the value of the items carried without exceeding the weight capacity of the knapsack.  More formally, we are given $N$ items.  Each item has a profit, $P(i)$, and weight, $W(i)$, for $i = {1, 2, \ldots, N}$.  Let $d(i)$ represent our decision to place the $i$-th item in the knapsack, where $d(i)=1$ if the item is put into the knapsack and $d(i)=0$ otherwise.  If the knapsack has a weight capacity of $C$, then the knapsack problem is defined as:

\begin{equation*}
  \text{Maximize} \; \sum_{i=1}^{N} d(i)*P(i) \; \text{such that} \; \sum_{i=1}^{N} d(i)*W(i) \leq C
\end{equation*}

The summation on the left (which we are maximizing) calculates the total profit we gain from the items placed in the knapsack.  The summation on the right side is a constraint that ensures the items placed in the knapsack do not exceed the weight capacity of the knapsack.

The multiobjective knapsack problem that we will be solving in this section is very similar, except that we now have $2$ knapsacks to hold the items.  Additionally, the profit and weights vary depending on which knapsack is holding each item.  For example, an item will have a profit of $\$25$ and a weight of $5$ pounds in the first knapsack, but will have a profit of $\$15$ and a weight of $8$ pounds in the second knapsack.  (It may seem unusual that the weight changes, but that is how the problem is defined in the literature.)   Thus, profit is now defined by $P(i,j)$ and weight by $W(i,j)$, where the $j = {1, 2}$ term is the knapsack index.  Lastly, each knapsack defines its own capacity, $C_1$ and $C_2$.  Combining all of this, the multiobjective knapsack problem is formally defined as:

\begin{equation*}
\begin{array}{l}
  \text{Maximize} \; \sum_{i=1}^{N} d(i)*P(i,1) \; \text{such that} \; \sum_{i=1}^{N} d(i)*W(i,1) \leq C_1 \; \text{and}\\
  \text{Maximize} \; \sum_{i=1}^{N} d(i)*P(i,2) \; \text{such that} \; \sum_{i=1}^{N} d(i)*W(i,2) \leq C_2
\end{array}
\end{equation*}

Once we have a firm understanding of the optimization problem, we can now work on solving this problem.  You can find all of the code for this example in the \folder{examples/org/moeaframework/examples/ga/knapsack} folder in the source code distribution.

\section{Data Files}
We begin by developing a way to store all of the information required by the knapsack problem --- profits, weights, capacities --- in a text file.  This will let us quickly generate and run new inputs for this problem.  Fortunately, two researchers, Eckart Zitzler and Marco Laumanns, have already created a file format for multiobjective knapsack problems at \webpage{http://www.tik.ee.ethz.ch/sop/download/supplementary/testProblemSuite/}.  For example, a simple $5$ item problem instance would appear as follows.

\begin{lstlisting}[language=plaintext]
knapsack problem specification (2 knapsacks, 5 items)
=
knapsack 1:
 capacity: +251
 item 1:
  weight: +94
  profit: +57
 item 2:
  weight: +74
  profit: +94
 item 3:
  weight: +77
  profit: +59
 item 4:
  weight: +74
  profit: +83
 item 5:
  weight: +29
  profit: +82
=
knapsack 2:
 capacity: +190
 item 1:
  weight: +55
  profit: +20
 item 2:
  weight: +10
  profit: +19
 item 3:
  weight: +97
  profit: +20
 item 4:
  weight: +73
  profit: +66
 item 5:
  weight: +69
  profit: +48
\end{lstlisting}

We will re-use this file format in this example.  One advantage is that you can download any of the example knapsack problems from \webpage{http://www.tik.ee.ethz.ch/sop/download/supplementary/testProblemSuite/} and solve them with the program we are writing.  Go ahead and save this example input file to \file{knapsack.5.2}.  We will then load and solve this data file later in this chapter.

\section{Encoding the Problem}
The next step is to decide upon the encoding for the decision variables.  Observe that we are deciding which items to place in the knapsacks.  Recalling \chptref{chpt:representations}, the bit string representation works well for situation where we are making many yes/no decisions.  For example, if $N=5$, we can represent the decision to include each item using a bit string with $5$ bits.  Each bit in the string corresponds to an item, and is set to \java{1} if the item is included and \java{0} if the item is excluded.  For instance, the bit string \java{00110} would place items 3 and 4 inside the knapsacks, excluding the rest.

\section{Implementing the Problem}
Having decided upon an encoding, we can now implement the knapsack problem as shown below.

\begin{lstlisting}[language=Java]
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.moeaframework.core.Problem;
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.BinaryVariable;
import org.moeaframework.core.variable.EncodingUtils;
import org.moeaframework.util.Vector;
import org.moeaframework.util.io.CommentedLineReader;

/**
 * Multiobjective 0/1 knapsack problem.
 */
 public class Knapsack implements Problem {

	/**
	 * The number of sacks.
	 */
	private int nsacks;

	/**
	 * The number of items.
	 */
	private int nitems;

	/**
	 * Entry {@code profit[i][j]} is the profit from including
	 * item {@code j} in sack {@code i}.
	 */
	private int[][] profit;

	/**
	 * Entry {@code weight[i][j]} is the weight incurred from
	 * including item {@code j} in sack {@code i}.
	 */
	private int[][] weight;

	/**
	 * Entry {@code capacity[i]} is the weight capacity of sack
	 * {@code i}.
	 */
	private int[] capacity;

	/**
	 * Constructs a multiobjective 0/1 knapsack problem instance
	 * loaded from the specified file.
	 *
	 * @param file the file containing the knapsack problem
	 *        instance
	 * @throws IOException if an I/O error occurred
	 */
	public Knapsack(File file) throws IOException {
		this(new FileReader(file));
	}

	/**
	 * Constructs a multiobjective 0/1 knapsack problem instance
	 * loaded from the specified input stream.
	 *
	 * @param inputStream the input stream containing the knapsack
	 *         problem instance
	 * @throws IOException if an I/O error occurred
	 */
	public Knapsack(InputStream inputStream) throws IOException {
		this(new InputStreamReader(inputStream));
	}

	/**
	 * Constructs a multiobjective 0/1 knapsack problem instance
	 * loaded from the specified reader.
	 *
	 * @param reader the reader containing the knapsack problem
	 *        instance
	 * @throws IOException if an I/O error occurred
	 */
	public Knapsack(Reader reader) throws IOException {
		super();

		load(reader);
	}

	/**
	 * Loads the knapsack problem instance from the specified
	 * reader.
	 *
	 * @param reader the file containing the knapsack problem
	 *        instance
	 * @throws IOException if an I/O error occurred
	 */
	private void load(Reader reader) throws IOException {
		Pattern specificationLine = Pattern.compile("knapsack problem specification \\((\\d+) knapsacks, (\\d+) items\\)");
		Pattern capacityLine = Pattern.compile(" capacity: \\+(\\d+)");
		Pattern weightLine = Pattern.compile("  weight: \\+(\\d+)");
		Pattern profitLine = Pattern.compile("  profit: \\+(\\d+)");

		CommentedLineReader lineReader = null;
		String line = null;
		Matcher matcher = null;

		try {
			lineReader = new CommentedLineReader(reader);
			line = lineReader.readLine(); // problem specification
			matcher = specificationLine.matcher(line);

			if (matcher.matches()) {
				nsacks = Integer.parseInt(matcher.group(1));
				nitems = Integer.parseInt(matcher.group(2));
			} else {
				throw new IOException("knapsack data file " +
						"not properly formatted: invalid specification " +
						"line");
			}

			capacity = new int[nsacks];
			profit = new int[nsacks][nitems];
			weight = new int[nsacks][nitems];

			for (int i = 0; i < nsacks; i++) {
				line = lineReader.readLine(); // line containing "="
				line = lineReader.readLine(); // knapsack i
				line = lineReader.readLine(); // the knapsack capacity
				matcher = capacityLine.matcher(line);

				if (matcher.matches()) {
					capacity[i] = Integer.parseInt(matcher.group(1));
				} else {
					throw new IOException("knapsack data file " +
							"not properly formatted: invalid capacity line");
				}

				for (int j = 0; j < nitems; j++) {
					line = lineReader.readLine(); // item j
					line = lineReader.readLine(); // the item weight
					matcher = weightLine.matcher(line);

					if (matcher.matches()) {
						weight[i][j] = Integer.parseInt(matcher.group(1));
					} else {
						throw new IOException("knapsack data file " +
								"not properly formatted: invalid weight line");
					}

					line = lineReader.readLine(); // the item profit
					matcher = profitLine.matcher(line);

					if (matcher.matches()) {
						profit[i][j] = Integer.parseInt(matcher.group(1));
					} else {
						throw new IOException("knapsack data file " +
								"not properly formatted: invalid profit line");
					}
				}
			}
		} finally {
			if (lineReader != null) {
				lineReader.close();
			}
		}
	}

	@Override
	public void evaluate(Solution solution) {
		boolean[] d = EncodingUtils.getBinary(
				solution.getVariable(0));
		double[] f = new double[nsacks];
		double[] g = new double[nsacks];

		// calculate the profits and weights for the knapsacks
		for (int i = 0; i < nitems; i++) {
			if (d[i]) {
				for (int j = 0; j < nsacks; j++) {
					f[j] += profit[j][i];
					g[j] += weight[j][i];
				}
			}
		}

		// check if any weights exceed the capacities
		for (int j = 0; j < nsacks; j++) {
			if (g[j] <= capacity[j]) {
				g[j] = 0.0;
			} else {
				g[j] = g[j] - capacity[j];
			}
		}

		// negate the objectives since Knapsack is maximization
		solution.setObjectives(Vector.negate(f));
		solution.setConstraints(g);
	}

	@Override
	public String getName() {
		return "Knapsack";
	}

	@Override
	public int getNumberOfConstraints() {
		return nsacks;
	}

	@Override
	public int getNumberOfObjectives() {
		return nsacks;
	}

	@Override
	public int getNumberOfVariables() {
		return 1;
	}

	@Override
	public Solution newSolution() {
		Solution solution = new Solution(1, nsacks, nsacks);
		solution.setVariable(0, EncodingUtils.newBinary(nitems));
		return solution;
	}

	@Override
	public void close() {
		//do nothing
	}

}
\end{lstlisting}

It is not vitally important to understand all of the code.  Much of the code is for loading the data file discussed in the previous section.  The key sections of the code you should pay attention to are the \java{evaluate} method starting on line 168 and the \java{newSolution} method on line 219.  Starting with the \java{newSolution} method, notice how line 220 creates a solution using the three-argument constructor, \java{new Solution(1, nsacks, nsacks)}.  The three argument constructor is used to define constraints.  In this example, we are defining a problem with \java{1} decision variable, \java{nsacks} objectives, and \java{nsacks} constraints --- one objective and one constraint for each knapsack.  Then on line 221 we set the one decision variable to be a bit string (binary encoding) with \java{nitems} bits.

The \java{evaluate} method on line 168 is where the knapsack equations from the beginning of this chapter are calculated.  We extract the bit string from the solution we are evaluating on line 169.  When the bit is set to \java{1}, the corresponding item is placed in both knapsacks.  Lines 175-182 sum up the profit and weight in each knapsack.  Lines 185-191 then check if any of the weights exceeds the capacity of any knapsack.  If the weight is less than the capacity, then the constraint is satisfied as we set the constraint value to 0 (line 187).  However, if the capacity is exceeded, then the constraint is violated and we set the constraint to a non-zero value (line 189).  To reiterate, constraints that are satisfied have a value of zero; violated constraints have non-zero values (both positive and negative).

Lastly, we set the objective values on line 194 and the constraint values on line 195.  Note on line 194 how we negate the objective values.  This is because we are trying to maximize the objectives (the profits).  See \sectref{sect:maximizing} for additional details on maximizing objectives.

\section{Solving the Problem}
With the problem implemented in Java, we can now solve the multiobjective knapsack problem using the optimization algorithms provided by the MOEA Framework.  In this example, we will use the NSGA-II algorithm as shown below.

\begin{lstlisting}[language=Java]
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.moeaframework.Executor;
import org.moeaframework.core.NondominatedPopulation;
import org.moeaframework.core.Solution;
import org.moeaframework.util.Vector;

/**
 * Example of binary optimization using the {@link Knapsack}
 * problem
 */
public class KnapsackExample {

	/**
	 * Starts the example running the knapsack problem.
	 *
	 * @param args the command line arguments
	 * @throws IOException if an I/O error occurred
	 */
	public static void main(String[] args) throws IOException {
		// solve using NSGA-II
		NondominatedPopulation result = new Executor()
				.withProblemClass(Knapsack.class,
						new File("knapsack.5.2"))
				.withAlgorithm("NSGAII")
				.withMaxEvaluations(50000)
				.distributeOnAllCores()
				.run();

		// print the results
		for (int i = 0; i < result.size(); i++) {
			Solution solution = result.get(i);
			double[] objectives = solution.getObjectives();

			// negate objectives to return them to their maximized
			// form
			objectives = Vector.negate(objectives);

			System.out.println("Solution " + (i+1) + ":");
			System.out.println("    Sack 1 Profit: " + objectives[0]);
			System.out.println("    Sack 2 Profit: " + objectives[1]);
			System.out.println("    Binary String: " +
					solution.getVariable(0));
		}
	}

}
\end{lstlisting}

Here, we are using the \java{Executor} to configure and solve the Knapsack problem.  Please refer to \chptref{chpt:executor} for more details.  You can now run this example code.  If all goes well, you will see output similar to:

\begin{lstlisting}[language=plaintext]
Solution 1:
    Sack 1 Profit: 259.0
    Sack 2 Profit: 133.0
    Binary String: 01011
\end{lstlisting}

In this case, only one Pareto optimal solution was found.  You can see the profits for each knapsack as well as identify which items were selected in this solution from the binary string being displayed.

\section{Conclusion}
This chapter walked you through a complete example of defining a new problem and solving it using the MOEA Framework.  You should now have a general understanding of using the MOEA Framework.  We recommend walking through the other examples in the \folder{examples} folder provided in the source code distribution.


\subsection{Group actions}\label{subsec:group_actions}

\begin{definition}\label{def:endomorphism_monoid}
  Given a \hyperref[def:category_size]{locally small} \hyperref[def:category]{category} \( \cat{C} \), we call \( \cat{C}(A) \) the \term{endomorphism monoid} over \( A \) and denote it by \( \End(A) \). If \( A \) is the only object in \( \cat{C} \), we can identify the entire category \( \cat{C} \) with the monoid \( \End(A) \).
\end{definition}

\begin{remark}\label{rem:monoid_action_endomorphisms}
  It is tempting to define a monoid action over an object \( A \) of an arbitrary locally small category rather than over a set rather than specifying the properties of an action (e.g. \enquote{linear action}). Unfortunately, even the simplest examples of monoid actions may fail to hold nice properties. See, e.g. \fullref{thm:monoid_is_action} or \fullref{thm:cayleys_theorem}.
\end{remark}

\begin{definition}\label{def:left_monoid_action}\mcite[159]{Knapp2016BasicAlgebra}
  Let \( \mscrM \) be a \hyperref[def:unital_magma/monoid]{monoid} and let \( A \) be a set. A \term{left monoid action} of \( \mscrM \) on \( A \) can be defined equivalently as:
  \begin{thmenum}
    \thmitem{def:left_monoid_action/homomorphism} A homomorphism from \( \mscrM \) to the \hyperref[def:multi_valued_function/endofunction]{endofunction} monoid \( \End(A) \).
    \thmitem{def:left_monoid_action/indirect_homomorphism} An indexed family of endofunctions \( \{ \tau_x \}_{x \in M} \) such that
    \begin{equation}\label{eq:def:left_monoid_action/indirect_homomorphism}
      \tau_{xy} = \tau_x \circ \tau_y \T{for all} x, y \in M.
    \end{equation}

    \thmitem{def:left_monoid_action/operation} A heterogeneous \hyperref[def:magma]{algebraic operation} \( \circ: \mscrM \times A \to A \), written using juxtaposition, such that
    \begin{thmenum}
      \thmitem{def:left_monoid_action/operation/identity} \( e \odot a = a \) holds for any \( a \in A \).
      \thmitem{def:left_monoid_action/operation/compatibility} \( (xy) \odot a = x \odot (y \odot a) \) holds whenever \( x, y \in M \) and \( a \in A \).
    \end{thmenum}

    See \fullref{rem:theory_of_left_monoid_actions} for the \hyperref[def:first_order_theory]{first-order logic theory} behind this operation.
  \end{thmenum}

  We say that \( \mscrM \) acts on \( A \) and optionally add adjectives, e.g. \enquote{\( \mscrM \) acts linearly/smoothly on \( A \)}.
\end{definition}
\begin{proof}
  \ImplicationSubProof{def:left_monoid_action/homomorphism}{def:left_monoid_action/indirect_homomorphism} Let \( \tau: \mscrM \to \End(A) \) be a homomorphism. Thus, we assign a morphism \( \tau(x) \) for each member \( x \in M \). Furthermore, since monoid operation on \( \End(A) \) is function composition and since \( \tau \) is a homomorphism, we have
  \begin{equation*}
    \tau(xy) = \tau(x) \circ \tau(y).
  \end{equation*}

  \ImplicationSubProof{def:left_monoid_action/indirect_homomorphism}{def:left_monoid_action/operation} Assume that we have a morphism \( \tau_x: A \to A \) for each \( x \in M \) that satisfies \eqref{eq:def:left_monoid_action/indirect_homomorphism}. Define the operation
  \begin{align*}
    {}&\odot{}: \mscrM \times A \to A \\
    x &\odot a \coloneqq \tau_x(a).
  \end{align*}

  It satisfies the necessary axioms:
  \begin{refenum}
    \refitem{def:left_monoid_action/operation/identity} If \( a \in A \), we have
    \begin{equation*}
      e \odot a
      =
      \tau_e(a)
      =
      \id(a)
      =
      a.
    \end{equation*}

    \refitem{def:left_monoid_action/operation/compatibility} If \( x, y \in M \) and \( a \in A \), we have
    \begin{equation*}
      (x y) \odot a
      =
      \tau_{x y}(a)
      =
      \tau_{x}(\tau{y}(a))
      =
      g \odot (h \odot a).
    \end{equation*}
  \end{refenum}

  \ImplicationSubProof{def:left_monoid_action/operation}{def:left_monoid_action/homomorphism} Assume that we have an operation \( \odot: \mscrM \times A \to A \) that satisfies the axioms for left action. Define the function
  \begin{align*}
    &\tau: \mscrM \to \End(A) \\
    &\tau(x) \coloneqq x \id.
  \end{align*}

  Then \( \tau \) is a monoid homomorphism because \( \tau(\varepsilon) = \id \) and
  \begin{equation}\label{def:left_monoid_action/homomorphism/proof}
    \varphi(xy)
    =
    xy \id
    =
    x (y \id)
    =
    x \id (y \id)
    =
    (x \id) (y \id)
    =
    \varphi(x) \varphi(y),
  \end{equation}
\end{proof}

\begin{remark}\label{rem:theory_of_left_monoid_actions}
  In order to fit the heterogeneous operation of \hyperref[def:left_monoid_action]{left monoid actions} into the framework of \hyperref[def:first_order_semantics/satisfiability]{first-order logic models}, we need the category \( \cat{C} \) to be a \hyperref[def:category_of_small_first_order_models]{category of \( \mscrU \)-small models}. A monoid action is then obtained, by extending the theory of \( \cat{C} \).

  \begin{thmenum}
    \thmitem{rem:theory_of_left_monoid_actions/functions} For each \( x \in M \), add a unary \hyperref[def:first_order_language/func]{functional symbol} \( \varphi_x \).

    \thmitem{rem:theory_of_left_monoid_actions/axiom} For each \( x, y \in M \), add the axiom
    \begin{equation}\label{eq:rem:theory_of_left_monoid_actions/axiom_schema}
      \forall a (\tau_{xy}(a) = \tau_x(\tau_y(a))).
    \end{equation}
  \end{thmenum}
\end{remark}

\begin{definition}\label{def:right_monoid_action}
  We say that \( \tau: \mscrM \to \End(A) \) is a \term{right monoid action} of \( \mscrM \) on \( A \) if the same function is a \hyperref[def:left_monoid_action]{left monoid action} of the \hyperref[def:magma/opposite]{opposite monoid} \( \mscrM^{-1} \) on \( A \).
\end{definition}

\begin{definition}\label{def:faithful_left_monoid_action}
  A left monoid action is said to be \term{faithful} if the corresponding homomorphism is injective.
\end{definition}

\begin{proposition}\label{thm:monoid_is_action}
  Any \hyperref[def:unital_magma/monoid]{monoid} \hyperref[def:left_monoid_action]{acts} on itself by \hyperref[def:multi_valued_function/endofunction]{endofunctions}.

  Compare this result to \fullref{thm:cayleys_theorem}.
\end{proposition}
\begin{proof}
  For completeness, we will verify all three definitions:

  \SubProofOf{def:left_monoid_action/homomorphism} The identity function \( \id: \mscrM \to \mscrM \) is the identity element of \( \fun(\mscrM) \). Define
  \begin{align*}
    &\tau: \mscrM \to \fun(\mscrM) \\
    &\tau(x) \coloneqq x \id
  \end{align*}

  It is a monoid homomorphism because both \eqref{eq:def:pointed_set/homomorphism} and \eqref{def:left_monoid_action/homomorphism/proof} hold.

  \SubProofOf{def:left_monoid_action/indirect_homomorphism} The proof is the same as above.

  \SubProofOf{def:left_monoid_action/operation} Define the operation
  \begin{balign*}
    {}&\odot{}: \mscrM \times \mscrM \to \mscrM \\
    x &\odot y \coloneqq xy
  \end{balign*}

  It immediately follows that
  \begin{refenum}
    \refitem{def:left_monoid_action/operation/identity} \( e \circ x = ex = x \) for all \( x \in M \).
    \refitem{def:left_monoid_action/operation/compatibility} \( (x y) \circ z = xyz = x \circ (y \circ z) \) for all \( x, y, z \in M \).
  \end{refenum}
\end{proof}

\begin{proposition}\label{thm:natural_numbers_monoid_action}
  The \hyperref[def:set_of_natural_numbers]{natural numbers} \( \BbbN \) act on any \hyperref[def:unital_magma/monoid]{monoid} by \hyperref[def:unital_magma/exponentiation]{exponentiation}.

  Compare this result to \fullref{thm:integers_group_action}.
\end{proposition}
\begin{proof}
  The action itself is given by \( n \mapsto (x \mapsto x^n) \). We must only verify that it is a homomorphism.

  We verify the explicit axioms from \fullref{def:left_monoid_action/operation}:
  \begin{refenum}
    \refitem{def:left_monoid_action/operation/identity} \( x^1 = x \) for all \( x \in M \) by definition.
    \refitem{def:left_monoid_action/operation/compatibility} \( (x^n)^m = x^{nm} \) is the literal statement of \eqref{eq:thm:magma_exponentiation_properties/repeated}.
  \end{refenum}
\end{proof}

\begin{definition}\label{def:automorphism_group}
  Given a \hyperref[def:category_size]{locally small} \hyperref[def:groupoid]{groupoid} \( \cat{G} \), we call \( \cat{G}(A) \) the \term{automorphism group} over \( A \) and denote it by \( \aut(A) \). If \( A \) is the only object in \( \cat{G} \), we can identify the entire category \( \cat{C} \) with the group \( \aut(A) \).
\end{definition}

\begin{definition}\label{def:symmetric_group}
  We define the \term{symmetric group} of order \( n \) as the group
  \begin{equation*}
    S_n \coloneqq \aut(\{ 1, 2, \ldots, n \})
  \end{equation*}
  of all bijections from the set \( \{ 1, 2, \ldots, n \} \) to itself.

  \begin{thmenum}
    \thmitem{def:symmetric_group/permutation} Members of \( S_n \) are called \term{permutations}.

    \thmitem{def:symmetric_group/inversion} We say that the pair \( (p(k), p(m)) \) is an \term{inversion} of the permutation \( p \) if \( k < m \) and \( p(k) > p(m) \).

    \thmitem{def:symmetric_group/permutation_parity} A permutation is said to be \term{even} or \term{odd} depending on whether it has an even or odd number of inversions.

    \thmitem{def:symmetric_group/permutation_sign} We define the \term{sign} of a permutation as
    \begin{align*}
       &\sgn: S_n \to \{ -1, 1 \}, \\
       &\sgn(p) \coloneqq \begin{cases}
        1,  &p \text{ is even} \\
        -1, &p \text{ is odd}
      \end{cases}
    \end{align*}

    \thmitem{def:symmetric_group/alternating} The subgroup of all even permutations is denoted by \( A_n \) and is called the \term{alternating group} of order \( n \).
  \end{thmenum}
\end{definition}

\begin{definition}\label{def:left_group_action}
  In analogy to \hyperref[def:left_monoid_action]{monoid actions}, we can define a \term{group actions} of the group \( \mscrG \) on a set \( A \) as
  \begin{thmenum}
    \thmitem{def:left_group_action/homomorphism} A homomorphism from \( \mscrG \) to the automorphism group \( \aut(A) \) (the set of all bijections on \( A \)).
    \thmitem{def:left_group_action/indirect_homomorphism} An indexed family of isomorphisms \( \{ \tau_x: A \to A \}_{x \in G} \) such that
    \begin{equation}\label{eq:def:left_group_action/indirect_homomorphism}
      \tau_{xy} = \tau_x \circ \tau_y \T{for all} x, y \in G.
    \end{equation}

    \thmitem{def:left_group_action/operation} The same heterogeneous algebraic operation \( \circ: \mscrG \times A \to A \) as in \fullref{def:left_monoid_action/operation}.
  \end{thmenum}
\end{definition}
\begin{proof}
  The proof of equivalence is the same as in \fullref{def:left_monoid_action}.
\end{proof}

\begin{definition}\label{def:right_group_action}
  As for \hyperref[def:right_monoid_action]{monoid actions}, we define \term{right group actions} as \hyperref[def:left_group_action]{left group action} of the \hyperref[def:magma/opposite]{opposite group} \( \mscrG^{-1} \) on \( A \).
\end{definition}

\begin{lemma}\label{thm:group_multiplication_is_bijection}
  For each element \( x \) of a group \( \mscrG \), the function \( \varphi_x \coloneqq x\id \), i.e.
  \begin{align*}
    &\varphi_x: \mscrG \to \mscrG \\
    &\varphi_x(y) \coloneqq xy,
  \end{align*}
  is an bijection (but not necessarily an isomorphism).
\end{lemma}
\begin{proof}
  \SubProofOf{def:function_invertibility/injective} If \( y_1, y_2 \in G \) and \( \varphi_x(y_1) = \varphi_x(y_2) \), we have
  \begin{equation*}
    xy_1 = \varphi_x(y_1) = \varphi_x(y_2) = xy_2.
  \end{equation*}

  By \fullref{thm:def:group/properties/cancellative}, \( y_1 = y_2 \). Therefore, \( \varphi_x \) is injective.

  \SubProofOf{def:function_invertibility/surjective} If \( z \in G \), then \( z = x(x^{-1} z) \). Therefore, \( z = \varphi_x(x^{-1} z) \) and \( \varphi_x \) is surjective.
\end{proof}

\begin{theorem}[Cayley's theorem]\label{thm:cayleys_theorem}
  Any \hyperref[def:group]{group} \hyperref[def:left_group_action]{acts} on itself by \hyperref[def:multi_valued_function/endofunction]{endofunctions}.

  Compare this result to \fullref{thm:monoid_is_action}.
\end{theorem}
\begin{proof}
  Follows directly from \fullref{thm:monoid_is_action} and \fullref{thm:group_multiplication_is_bijection}.
\end{proof}

\begin{proposition}\label{thm:integers_group_action}
  The \hyperref[def:set_of_integers]{integers} \( \BbbZ \) act on any \hyperref[def:group]{group} by \hyperref[def:group/exponentiation]{exponentiation}.

  Compare this result to \fullref{thm:natural_numbers_monoid_action}.
\end{proposition}
\begin{proof}
  Follows from \fullref{thm:natural_numbers_monoid_action}.
\end{proof}


\section{Arc Length}{}{}\label{sec:Arc Length}

In previous sections we used integration to answer the following questions:
	\begin{enumerate}
	\item		Given a region, what is its area?
	\item		Given a solid, what is its volume?
	\end{enumerate}

In this section, we address a related question: Given a curve, what is its length? This is often referred to as \textbf{arc length}.

Consider the graph of $y=\sin x$ on $[0,\pi]$ given in Figure \ref{fig:arcintro} (a). How long is this curve? That is, if we were to use a piece of string to exactly match the shape of this curve, how long would the string be?

As we have done in the past, we start by approximating; later, we will refine our answer using limits to get an exact solution.

The length of straight--line segments is easy to compute using the Distance Formula. We can approximate the length of the given curve by approximating the curve with straight lines and measuring their lengths.

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.5\textwidth}
		\begin{tikzpicture}
		\begin{axis}[ width=\linewidth,%
		tick label style={font=\scriptsize},axis y line=middle,axis x line=middle,name=myplot,axis on top,%
					%x=.37\marginparwidth,
					%y=.37\marginparwidth,
					xtick=\empty,%
					extra x ticks={.79,1.57,2.36,3.14},
					extra x tick labels={$\frac{\pi}4$,$\frac{\pi}2$,$\frac{3\pi}{4}$,$\pi$},
		%			ytick={1},
		%			yticklabels={$-0.002$,$0.002$,$0.004$},
					%minor y tick num=1,
		%			extra y ticks={1.8},%
		%			extra y tick labels={$y$},
		%			minor x tick num=4,
					ymin=-.2,ymax=1.25,%
					xmin=-.1,xmax=3.5,%
		]

		\addplot [{\colorone},smooth,thick,domain=0:3.14] {sin(deg(x))};

		\end{axis}

		\node [right] at (myplot.right of origin) {\scriptsize $x$};
		\node [above] at (myplot.above origin) {\scriptsize $y$};
		\end{tikzpicture}
         \label{fig:arcintroa}
        \caption{}
    \end{subfigure}%
    \begin{subfigure}[t]{0.5\textwidth}
    \begin{tikzpicture}
    \begin{axis}[ width=\linewidth,%
    tick label style={font=\scriptsize},axis y line=middle,axis x line=middle,name=myplot,axis on top,%
    			%x=.37\marginparwidth,
    			%y=.37\marginparwidth,
    			xtick=\empty,%
    			extra x ticks={.79,1.57,2.36,3.14},
    			extra x tick labels={$\frac{\pi}4$,$\frac{\pi}2$,$\frac{3\pi}{4}$,$\pi$},
    %			ytick={1},
    %			yticklabels={$-0.002$,$0.002$,$0.004$},
    			%minor y tick num=1,
    			extra y ticks={.71},%
    			extra y tick labels={$\frac{\sqrt{2}}2$},
    %			minor x tick num=4,
    			ymin=-.2,ymax=1.25,%
    			xmin=-.1,xmax=3.5,%
    ]

    \addplot [{\colorone},smooth,thick,domain=0:3.14] {sin(deg(x))};

    \draw [{\colortwo},thick] (axis cs:0,0) -- (axis cs:.79,.71) -- (axis cs: 1.57,1) -- (axis cs:2.36,.71) -- (axis cs: 3.14,0);

    \filldraw (axis cs:0,0) circle (1pt) (axis cs:.79,.71) circle (1pt) (axis cs: 1.57,1) circle (1pt) (axis cs:2.36,.71) circle (1pt) (axis cs: 3.14,0)circle (1pt);

    \end{axis}

    \node [right] at (myplot.right of origin) {\scriptsize $x$};
    \node [above] at (myplot.above origin) {\scriptsize $y$};
    \end{tikzpicture}
        \label{fig:arcintrob}
        \caption{}
    \end{subfigure}
    \caption{Graphing $y=\sin x$ on $[0,\pi]$ and approximating the curve with line segments. \label{fig:arcintro}}
\end{figure}

In Figure \ref{fig:arcintro} (b), the curve $y=\sin x$ has been approximated with 4 line segments (the interval $[0,\pi]$ has been divided into 4 equally--lengthed subintervals). It is clear that these four line segments approximate $y=\sin x$ very well on the first and last subinterval, though not so well in the middle. Regardless, the sum of the lengths of the line segments is $3.79$, so we approximate the arc length of $y=\sin x$ on $[0,\pi]$ to be $3.79$.

%Using 6 evenly spaced subintervals is not too much more work and gives a better approximation. The six lines are shown in Figure \ref{fig:arcintroc} and provide an arc length approximation of $3.81$.

%\begin{figure}[H]
%\begin{tikzpicture}
%\begin{axis}[width=\marginparwidth+25pt,%
%tick label style={font=\scriptsize},axis y line=middle,axis x line=middle,name=myplot,axis on top,%
%			%x=.37\marginparwidth,
%			%y=.37\marginparwidth,
%			xtick=\empty,%
%			extra x ticks={.52,1.05,1.6,2.1,2.6,3.14},
%			extra x tick labels={$\frac{\pi}6$,$\frac{\pi}3$,$\frac{\pi}{2}$,$\frac{2\pi}3$,$\frac{5\pi}6$,$\pi$},
%%			ytick={1},
%%			yticklabels={$-0.002$,$0.002$,$0.004$},
%			%minor y tick num=1,
%			extra y ticks={.866},%
%			extra y tick labels={$\frac{\sqrt{3}}2$},
%%			minor x tick num=4,
%			ymin=-.2,ymax=1.25,%
%			xmin=-.1,xmax=3.5,%
%]
%
%\addplot [{\colorone},smooth,thick,domain=0:3.14] {sin(deg(x))};
%
%\draw [{\colortwo},thick] (axis cs:0,0)--(axis cs:0.5236,0.5)--(axis cs:1.047,0.866)--(axis cs:1.571,1.)--(axis cs:2.094,0.866)--(axis cs:2.618,0.5)--(axis cs:3.142,0);
%
%\filldraw (axis cs:0,0) circle (1pt) (axis cs:0.5236,0.5) circle (1pt) (axis cs:1.047,0.866) circle (1pt) (axis cs:1.571,1.) circle (1pt) (axis cs:2.094,0.866) circle (1pt) (axis cs:2.618,0.5) circle (1pt) (axis cs:3.142,0);
%
%\end{axis}
%
%\node [right] at (myplot.right of origin) {\scriptsize $x$};
%\node [above] at (myplot.above origin) {\scriptsize $y$};
%\end{tikzpicture}
%\label{fig:arcintroc}
%\end{figure}

In general,  we can approximate the arc length of $y=f(x)$ on $[a,b]$ in the following manner. Let $a=x_1 < x_2 < \ldots < x_n< x_{n+1}=b$ be a partition of $[a,b]$ into $n$ subintervals. Let $\dx_i$ represent the length of the $i\,^\text{th}$ subinterval $[x_i,x_{i+1}]$.

\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[width=.5\linewidth,%
tick label style={font=\scriptsize},axis y line=middle,axis x line=middle,name=myplot,axis on top,%
			%x=.37\marginparwidth,
			%y=.37\marginparwidth,
			xtick=\empty,%
			extra x ticks={.3,1.37},
			extra x tick labels={$x_i$,$x_{i+1}$},
			ytick=\empty,
%			yticklabels={$-0.002$,$0.002$,$0.004$},
			%minor y tick num=1,
			extra y ticks={.48,1},%
			extra y tick labels={$y_i$,$y_{i+1}$},
%			minor x tick num=4,
			ymin=-.2,ymax=1.25,%
			xmin=-.1,xmax=1.6,%
]
%\addplot [{\colorone},smooth,thin,dashed,domain=0.1:1.55] {sin(deg(x+.2))};

\addplot [{\colorone},smooth,thick,domain=0.3:1.37] {sin(deg(x+.2))};

\draw (axis cs: .25,1) -- (axis cs:.1,1)
			(axis cs: .25,.48) -- (axis cs:.1,.48)
			(axis cs: .175,.48) -- node [pos=.5,fill=white] {\scriptsize $\Delta y_i$} (axis cs:.175,1);

\draw (axis cs: .3,.25) -- (axis cs: .3,.4)
			(axis cs: 1.37,.25) -- (axis cs: 1.37,.4)
			(axis cs: .3,.325) -- node [pos=.5,fill=white] {\scriptsize $\Delta x_i$} (axis cs: 1.37,.325);

\draw [{\colortwo},thick] (axis cs: .3,.48) -- (axis cs:1.37,1);

\draw [dashed,thin] (axis cs: .3,.48) -| (axis cs:1.37,1);

\filldraw (axis cs: .3,.48) circle (1pt) (axis cs:1.37,1) circle (1pt);

%\draw [{\colortwo},thick] (axis cs:0,0)--(axis cs:0.5236,0.5)--(axis cs:1.047,0.866)--(axis cs:1.571,1.)--(axis cs:2.094,0.866)--(axis cs:2.618,0.5)--(axis cs:3.142,0);
%
%\filldraw (axis cs:0,0) circle (1pt) (axis cs:0.5236,0.5) circle (1pt) (axis cs:1.047,0.866) circle (1pt) (axis cs:1.571,1.) circle (1pt) (axis cs:2.094,0.866) circle (1pt) (axis cs:2.618,0.5) circle (1pt) (axis cs:3.142,0);

\end{axis}

\node [right] at (myplot.right of origin) {\scriptsize $x$};
\node [above] at (myplot.above origin) {\scriptsize $y$};
\end{tikzpicture}
\label{fig:arcintro2}
\caption{Zooming in on the $i\,^\text{th}$ subinterval $[x_i,x_{i+1}$] of a partition of $[a,b]$.}
\end{figure}

%\mfigure{.35}{Zooming in on the $i\,^\text{th}$ subinterval $[x_i,x_{i+1}$] of a partition of $[a,b]$.}{fig:arcintro2}{figures/figarcintrod}
%
Figure \ref{fig:arcintro2} zooms in on the $i\,^\text{th}$ subinterval where $y=f(x)$ is approximated by a straight line segment. The dashed lines show that we can view this line segment as they hypotenuse of a right triangle whose sides have length $\dx_i$ and $\dy_i$. Using the Pythagorean Theorem, the length of this line segment is
$\ds \sqrt{\dx_i^2 + \Delta y_i^2}.$ Summing over all subintervals gives an arc length approximation
$$L \approx \sum_{i=1}^n \sqrt{\dx_i^2 + \Delta y_i^2}.$$

As shown here, this is \textit{not} a Riemann Sum. While we could conclude that taking a limit as the subinterval length goes to zero gives the exact arc length, we would not be able to compute the answer with a definite integral. We need first to do a little algebra.

In the above expression factor out a $\dx_i^2$ term:
\begin{align*}
\sum_{i=1}^n \sqrt{\dx_i^2 + \Delta y_i^2} &= \sum_{i=1}^n \sqrt{\dx_i^2\left(1 + \frac{\Delta y_i^2}{\dx_i^2}\right)}.\\
\intertext{Now pull the $\dx_i^2$ term out of the square root:}%\\
			&= \sum_{i=1}^n\sqrt{1 + \frac{\Delta y_i^2}{\dx_i^2}}\ \dx_i.\\
\intertext{This is nearly a Riemann Sum. Consider the $\Delta y_i^2/\dx_i^2$ term. The expression $\Delta y_i/\dx_i$ measures the ``change in $y$/change in $x$,'' that is, the ``rise over run'' of $f$ on the $i\,^\text{th}$ subinterval. The Mean Value Theorem of Differentiation (Theorem \ref{thm:mvt}) states that there is a $c_i$ in the $i\,^\text{th}$ subinterval where $\fp(c_i) = \Delta y_i/\dx_i$. Thus we can rewrite our above expression as:}
			&= \sum_{i=1}^n\sqrt{1+\fp(c_i)^2}\ \dx_i.\\
\intertext{This \textit{is} a Riemann Sum. As long as \fp\ is continuous, we can invoke Theorem \ref{thm:riemann_sum} and conclude }%\\
			&= \int_a^b\sqrt{1+\fp(x)^2}\ dx.
\end{align*}

\begin{formulabox}[\label{idea:arclength} Arc Length ]
{Let $f$ be differentiable on an open interval containing $[a,b]$, where $\fp$ is also continuous on $[a,b]$. Then the arc length of $f$ from $x=a$ to $x=b$ is
\index{integration!arc length}\index{arc length}
$$L = \int_a^b \sqrt{1+\fp(x)^2}\ dx.$$
}

\end{formulabox}

As the integrand contains a square root, it is often difficult to use the formula in Key Idea \ref{idea:arclength} to find the length exactly. When exact answers are difficult to come by, we resort to using numerical methods of approximating definite integrals. The following examples will demonstrate this.\\

\begin{example}{Finding arc length}{ex_arc1}
{
Find the arc length of $f(x) = x^{3/2}$ from $x=0$ to $x=4$. }
\end{example}

\begin{solution}
{We begin by finding $\fp(x)= \frac32x^{1/2}$. Using the formula, we find the arc length $L$ as
\begin{align*}
	L &=	\int_0^4 \sqrt{1+\left(\frac32x^{1/2}\right)^2}\ dx \\
		&=	\int_0^4 \sqrt{1+\frac94x} \ dx \\
		&= 	\int_0^4 \left(1+\frac94x\right)^{1/2}\ dx \\
		&=  \frac23\frac49\left(1+\frac94x\right)^{3/2}\Big|_0^4 \\
		&=\frac{8}{27}\left(10^{3/2}-1\right) \approx 9.07 \text{units}.
\end{align*}

\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[width=.4\linewidth,%
tick label style={font=\scriptsize},axis y line=middle,axis x line=middle,name=myplot,axis on top,%
			%x=.37\marginparwidth,
			%y=.37\marginparwidth,
%			xtick=\empty,%
%			extra x ticks={.79,1.57,2.36,3.14},
%			extra x tick labels={$\frac{\pi}4$,$\frac{\pi}2$,$\frac{3\pi}{4}$,$\pi$},
%			ytick={1},
%			yticklabels={$-0.002$,$0.002$,$0.004$},
			%minor y tick num=1,
%			extra y ticks={1.8},%
%			extra y tick labels={$y$},
%			minor x tick num=4,
			ymin=-.2,ymax=8.5,%
			xmin=-.1,xmax=4.5,%
]

\addplot [{\colorone},smooth,thick,domain=0:4,samples=50] coordinates {(0,0)(0.1,0.03162)(0.2,0.08944)(0.3,0.1643)(0.4,0.253)(0.5,0.3536)(0.6,0.4648)(0.7,0.5857)(0.8,0.7155)(0.9,0.8538)(1.,1.)(1.3,1.482)(1.6,2.024)(1.9,2.619)(2.2,3.263)(2.5,3.953)(2.8,4.685)(3.1,5.458)(3.4,6.269)(3.7,7.117)(4.,8.)};

\end{axis}

\node [right] at (myplot.right of origin) {\scriptsize $x$};
\node [above] at (myplot.above origin) {\scriptsize $y$};
\end{tikzpicture}
\caption{A graph of $f(x) = x^{3/2}$ from Example \ref{exa:ex_arc1}. \label{fig:arc1}}
\end{figure}
}
\end{solution}

\begin{example}{Finding arc length}{ex_arc2}{
Find the arc length of $\ds f(x) =\frac18x^2-\ln x$ from $x=1$ to $x=2$.}
\end{example}

\begin{solution}
{This function was chosen specifically because the resulting integral can be evaluated exactly. We begin by finding $\fp(x) = x/4-1/x$. The arc length is
\begin{align*}
L		&=  \int_1^2 \sqrt{1+ \left(\frac x4-\frac1x\right)^2}\ dx \\
		&= 	\int_1^2 \sqrt{1 + \frac{x^2}{16} -\frac12 + \frac1{x^2} } \ dx \\
		&=	\int_1^2 \sqrt{\frac{x^2}{16} +\frac12 + \frac1{x^2} } \ dx \\
		&=	\int_1^2	\sqrt{ \left(\frac x4 + \frac1x\right)^2}\ dx
\end{align*}
\begin{align*}
\phantom{L}
				&= \int_1^2 \left(\frac x4 + \frac1x\right) \ dx \\
		&=  \left(\frac{x^2}8 + \ln x\right)\Bigg|_1^2\\
		&=	\frac38+\ln 2 \approx 1.07 \ \text{units}.
\end{align*}

\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[width=.4\linewidth,%
tick label style={font=\scriptsize},axis y line=middle,axis x line=middle,name=myplot,axis on top,%
			%x=.37\marginparwidth,
			%y=.37\marginparwidth,
%			xtick=\empty,%
%			extra x ticks={.79,1.57,2.36,3.14},
%			extra x tick labels={$\frac{\pi}4$,$\frac{\pi}2$,$\frac{3\pi}{4}$,$\pi$},
%			ytick={1},
%			yticklabels={$-0.002$,$0.002$,$0.004$},
			%minor y tick num=1,
%			extra y ticks={1.8},%
%			extra y tick labels={$y$},
%			minor x tick num=4,
			ymin=-.26,ymax=1.2,%
			xmin=-.1,xmax=3.1,%
]
\addplot [{\colorone},smooth,thin] coordinates{(0.2,1.614)(0.3,1.215)(0.4,0.9363)(0.5,0.7244)(0.6,0.5558)(0.7,0.4179)(0.8,0.3031)(0.9,0.2066)(1.,0.125)(1.1,0.05594)(1.2,-0.002322)(1.3,-0.05111)(1.4,-0.09147)(1.5,-0.1242)(1.6,-0.15)(1.7,-0.1694)(1.8,-0.1828)(1.9,-0.1906)(2.,-0.1931)(2.1,-0.1907)(2.2,-0.1835)(2.3,-0.1717)(2.4,-0.1555)(2.5,-0.135)(2.6,-0.1105)(2.7,-0.082)(2.8,-0.04962)(2.9,-0.01346)(3.,0.02639)};

\addplot [{\colorone},smooth,very thick,] coordinates {(1.,0.125)(1.1,0.05594)(1.2,-0.002322)(1.3,-0.05111)(1.4,-0.09147)(1.5,-0.1242)(1.6,-0.15)(1.7,-0.1694)(1.8,-0.1828)(1.85,-0.1874)(1.9,-0.1906)(1.95,-0.1925)(2.,-0.1931)};

\end{axis}

\node [right] at (myplot.right of origin) {\scriptsize $x$};
\node [above] at (myplot.above origin) {\scriptsize $y$};
\end{tikzpicture}
\caption{A graph of $f(x) =\frac18x^2-\ln x$ from Example \ref{ex_arc2}. \label{fig:arc2}}
\end{figure}

A graph of $f$ is given in Figure \ref{fig:arc2}; the portion of the curve measured in this problem is in bold.
}
\end{solution}

\begin{example}{Circumference of a Circle}{Circumference of a Circle}\label{Circumference of a Circle}
Let $\ds f(x) = \sqrt{r^2-x^2}$, the upper half circle of radius
$r$. The length of this curve is half the circumference, namely $\pi
r$. Compute this with the arc length formula.
\end{example}

\begin{solution}
The derivative $f'$ is $\ds \ds -x/\sqrt{r^2-x^2}$ so the integral is
$$
  \int_{-r}^r \sqrt{1+{x^2\over r^2-x^2}}\,dx
  =\int_{-r}^r \sqrt{r^2\over r^2-x^2}\,dx
  =r\int_{-r}^r \sqrt{1\over r^2-x^2}\,dx.
$$
Using a trigonometric substitution, we find the antiderivative, namely
$\ds \arcsin(x/r)$. Notice that the integral is improper at both
endpoints, as the function $\ds \sqrt{1/(r^2-x^2)}$ is undefined when
$x=\pm r$. So we need to compute
$$
  \lim_{D\to-r^+}\int_D^0  \sqrt{1\over r^2-x^2}\,dx +
  \lim_{D\to r^-}\int_0^D  \sqrt{1\over r^2-x^2}\,dx.
$$
This is not difficult, and has value $\pi$, so the original integral,
with the extra $r$ in front, has value $\pi r$ as expected.
\end{solution}

The previous examples found the arc length exactly through careful choice of the functions. In general, exact answers are much more difficult to come by and numerical approximations are necessary. \\

\begin{example}{Approximating arc length numerically}{ex_arc3}
{
Find the length of the sine curve from $x=0$ to $x=\pi$.}
\end{example}

\begin{solution}
{This is somewhat of a mathematical curiosity; in Example \ref{ex_ftc4} we found the area under one ``hump'' of the sine curve is 2 square units; now we are measuring its arc length.

The setup is straightforward: $f(x) = \sin x$ and $\fp(x) = \cos x$. Thus
$$L = \int_0^\pi \sqrt{1+\cos^2x}\ dx.$$
This integral \textit{cannot} be evaluated in terms of elementary functions so we will approximate it with Simpson's Method with $n=4$.
\mtable{.5}{A table of values of $y=\sqrt{1+\cos^2x}$ to evaluate a definite integral in Example \ref{ex_arc3}.}{fig:arc3}{%
$$\begin{array}{cc}
x & \sqrt{1+\cos^2x} \\ \hline
 0 & \sqrt{2} \rule{0pt}{10pt}\\
 \pi/4 & \sqrt{3/2} \\
 \pi/2 & 1 \\
 3 \pi/4 & \sqrt{3/2} \\
 \pi  & \sqrt{2} \\
\end{array}$$
}
Figure \ref{fig:arc3} gives $\sqrt{1+\cos^2x}$ evaluated at 5 evenly spaced points in $[0,\pi]$. Simpson's Rule then states that
\begin{align*}
\int_0^\pi \sqrt{1+\cos^2x}\ dx &\approx	\frac{\pi-0}{4\cdot 3}\left(\sqrt{2}+4\sqrt{3/2}+2(1)+4\sqrt{3/2}+\sqrt{2}\right) \\
			&=3.82918.
\end{align*}
Using a computer with $n=100$ the approximation is $L\approx 3.8202$; our approximation with $n=4$ is quite good.
}
\end{solution}

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

%Here is another geometric application of the integral: Find the length
%of a portion of a curve. As usual, we need to think about how we might
%approximate the length, and turn the approximation into an integral.
%
%We already know how to compute one simple arc length, that of a line
%segment. If the endpoints are $\ds P_0(x_0,y_0)$ and $\ds P_1(x_1,y_1)$
%then the length of the segment is the distance between the points,
%$\ds \sqrt{(x_1-x_0)^2+(y_1-y_0)^2}$, from the Pythagorean theorem, as
%illustrated in Figure~\ref{fig:length of a line segment}.
%
%\figure[H]
%%\texonly
%\centerline{\vbox{\beginpicture
%\normalgraphs
%%\sevenpoint
%\setcoordinatesystem units <1.5truecm,1.5truecm>
%\setplotarea x from 0 to 5, y from 0 to 3
%\axis bottom /
%\axis left /
%\putrule from 2 1 to 4.5 1
%\putrule from 4.5 1 to 4.5 2.5
%\plot 2 1 4.5 2.5 /
%\put {$(x_1,y_1)$} [bl] <3pt,3pt> at 4.5 2.5
%\put {$(x_0,y_0)$} [tr] <-3pt,-3pt> at 2 1
%\put {$x_1-x_0$} [t] <0pt,-3pt> at 3.25 1
%\put {$y_1-y_0$} [l] <3pt,0pt> at 4.5 1.75
%\put {$\sqrt{(x_1-x_0)^2+(y_1-y_0)^2}$} [br] <-3pt,3pt> at 3.25 1.75
%\endpicture}}
%%\endtexonly
%%\figrdef{fig:length of a line segment}
%%\htmlfigure{Integration_applications-arc_length.html}
%\caption{\label{fig:length of a line segment}
%The length of a line segment.}
%%\endcaption
%\endfigure
%
%Now if the graph of $f$ is ``nice'' (say, differentiable) it appears
%that we can approximate the length of a portion of the curve with line
%segments, and that as the number of segments increases, and their
%lengths decrease, the sum of the lengths of the line segments will
%approach the true arc length; see
%Figure~\ref{fig:approximating arc length}.
%
%\figure[H]
%%\texonly
%\centerline{\vbox{\beginpicture
%\normalgraphs
%%\sevenpoint
%\setcoordinatesystem units <1.5truecm,0.8truecm>
%\setplotarea x from 0 to 8, y from 0 to 5
%\axis bottom /
%\axis left /
%\setquadratic\plot
%1.000 1.000 1.150 1.953 1.300 2.694 1.450 3.246 1.600 3.636
%1.750 3.884 1.900 4.012 2.050 4.041 2.200 3.990 2.350 3.874
%2.500 3.711 2.650 3.515 2.800 3.299 2.950 3.075 3.100 2.853
%3.250 2.644 3.400 2.454 3.550 2.290 3.700 2.157 3.850 2.060
%4.000 2.000 4.150 1.979 4.300 1.996 4.450 2.050 4.600 2.138
%4.750 2.255 4.900 2.396 5.050 2.554 5.200 2.721 5.350 2.886
%5.500 3.039 5.650 3.168 5.800 3.258 5.950 3.294 6.100 3.261
%6.250 3.140 6.400 2.912 6.550 2.556 6.700 2.051 6.850 1.374
%7.000 0.500 /
%\multiput {$\bullet$} at 1 1 2.050 4.041 3.250 2.644
%4.300 1.996 5.500 3.039 6.250 3.140 7 0.5 /
%\setlinear\plot
%1 1 2.050 4.041 3.250 2.644
%4.300 1.996 5.500 3.039 6.250 3.140 7 0.5 /
%\endpicture}}
%%\endtexonly
%%\figrdef{fig:approximating arc length}
%%\htmlfigure{Integration_applications-arc_length_line_segments.html}
%\caption{\label{fig:approximating arc length}
%Approximating arc length with line segments.}
%%\endcaption
%\endfigure
%
%Now we need to write a formula for the sum of the lengths of the line
%segments, in a form that we know becomes an integral in the limit.  So
%we suppose we have divided the interval $[a,b]$ into $n$ subintervals
%as usual, each with length $\Delta x =(b-a)/n$, and endpoints $\ds
%a=x_0$, $\ds x_1$, $\ds x_2$, \dots, $\ds x_n=b$.  The length of a
%typical line segment, joining $\ds (x_i,f(x_i))$ to $\ds
%(x_{i+1},f(x_{i+1}))$, is $\ds\sqrt{(\Delta x )^2
%  +(f(x_{i+1})-f(x_i))^2}$.  By the Mean Value Theorem, %(\xrefn{thm:mvt}),
%there is a number $\ds t_i$ in $\ds (x_i,x_{i+1})$
%such that $\ds f'(t_i)\Delta x=f(x_{i+1})-f(x_i)$, so the length of
%the line segment can be written as
%$$
%  \sqrt{(\Delta x)^2 + (f'(t_i))^2\Delta x^2}=
%  \sqrt{1+(f'(t_i))^2}\,\Delta x.
%$$
%Then arc length is:
%$$
%  \lim_{n\to\infty}\sum_{i=0}^{n-1} \sqrt{1+(f'(t_i))^2}\,\Delta x=
%  \int_a^b \sqrt{1+(f'(x))^2}\,dx.
%$$
%Note that the sum looks a bit different than others we have
%encountered, because the approximation contains a $\ds t_i$ instead of an
%$\ds x_i$. In the past we have always used left endpoints (namely, $\ds x_i$)
%to get a representative value of $f$ on $\ds [x_i,x_{i+1}]$; now we are
%using a different point, but the principle is the same.
%
%To summarize, to compute the length of a curve on the interval
%$[a,b]$, we compute the integral
%$$\int_a^b \sqrt{1+(f'(x))^2}\,dx.$$
%Unfortunately, integrals of this form are typically difficult or
%impossible to compute exactly, because usually none of our methods for
%finding antiderivatives will work. In practice this means that the
%integral will usually have to be approximated.
%
%\begin{example}{Circumference of a Circle}{Circumference of a Circle}\label{Circumference of a Circle}
%Let $\ds f(x) = \sqrt{r^2-x^2}$, the upper half circle of radius
%$r$. The length of this curve is half the circumference, namely $\pi
%r$. Compute this with the arc length formula.
%\end{example}
%
%\begin{solution}
%The derivative $f'$ is $\ds \ds -x/\sqrt{r^2-x^2}$ so the integral is
%$$
%  \int_{-r}^r \sqrt{1+{x^2\over r^2-x^2}}\,dx
%  =\int_{-r}^r \sqrt{r^2\over r^2-x^2}\,dx
%  =r\int_{-r}^r \sqrt{1\over r^2-x^2}\,dx.
%$$
%Using a trigonometric substitution, we find the antiderivative, namely
%$\ds \arcsin(x/r)$. Notice that the integral is improper at both
%endpoints, as the function $\ds \sqrt{1/(r^2-x^2)}$ is undefined when
%$x=\pm r$. So we need to compute
%$$
%  \lim_{D\to-r^+}\int_D^0  \sqrt{1\over r^2-x^2}\,dx +
%  \lim_{D\to r^-}\int_0^D  \sqrt{1\over r^2-x^2}\,dx.
%$$
%This is not difficult, and has value $\pi$, so the original integral,
%with the extra $r$ in front, has value $\pi r$ as expected.
%\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:Arc Length}}

\begin{enumialphparenastyle}

% % % % % % % % % % %
\begin{ex}
Find the arc length of the function on the given interval.
\begin{enumerate}
\item {$\ds f(x) = x$ on $[0, 1]$.}

\item {$\ds f(x) = \sqrt{8}x$ on $[-1, 1]$.}

\item {$\ds f(x) = \frac13x^{3/2}-x^{1/2}$ on $[0,1]$.}

\item {$\ds f(x) = \frac1{12}x^{3}+\frac1x$ on $[1,4]$.}

\item {$\ds f(x) = 2x^{3/2}-\frac16\sqrt{x}$ on $[0,9]$.}

\item {$\ds f(x) = \cosh x$ on $[-\ln 2, \ln 2]$.}

\item {$\ds f(x) = \frac12\big(e^x+e^{-x}\big)$ on $[0, \ln 5]$.}

\item {$\ds f(x) = \frac1{12}x^5+\frac1{5x^3}$ on $[.1, 1]$.}

\item {$\ds f(x) = \ln \big(\sin x\big)$ on $[\pi/6, \pi/2]$.}

\item {$\ds f(x) = \ln \big(\cos x\big)$ on $[0, \pi/4]$.}

\end{enumerate}

\begin{sol}
\begin{enumerate}
\item {$\sqrt{2}$}
\item {$6$}
\item {$4/3$}
\item {$6$}
\item {$109/2$}
\item {$3/2$}
\item {$12/5$}
\item {$79953333/400000 \approx 199.883$}
\item {$-\ln (2-\sqrt{3}) \approx 1.31696$}
\item {$\sinh^{-1} 1$}
\end{enumerate}
\end{sol}

\end{ex}
% % % % % % % % % % % %

% % % % % % % % % % %
\begin{ex}
Set up the integral to compute the arc length of the function on the given interval. Do \textbf{not} evaluate the integral. \label{ex_al1}
\begin{enumerate}
\item {$\ds f(x) = x^2$ on $[0, 1]$.\label{ex_07_04_ex_13}}

\item {$\ds f(x) = x^{10}$ on $[0, 1]$.}

\item {$\ds f(x) = \ln x$ on $[1, e]$.}

\item {$\ds f(x) = \sqrt{x}$ on $[0, 1]$.}

\item {$\ds f(x) = \sqrt{1-x^2}$ on $[-1, 1]$. (Note: this describes the top half of a circle with radius 1.)}

\item {$\ds f(x) = \sqrt{1-x^2/9}$ on $[-3, 3]$. (Note: this describes the top half of an ellipse with a major axis of length $ 6 $ and a minor axis of length $ 2 $.)}
\item {$\ds f(x) = \frac1x$ on $[1,2]$.}

\item {$\ds f(x) = \sec x$ on $[-\pi/4,\pi/4]$.\label{ex_07_04_ex_20}}

\end{enumerate}

\begin{sol}
\begin{enumerate}
\item {$\int_0^1 \sqrt{1+4x^2}\ dx$}
\item {$\int_0^1 \sqrt{1+100x^{18}}\ dx$}
\item {$\int_1^e \sqrt{1+\frac1{x^2}}\ dx$}
\item {$\int_0^1 \sqrt{1+\frac{1}{4x}}\ dx$}
\item {$\int_{-1}^1 \sqrt{1+\frac{x^2}{1-x^2}}\ dx$}
\item
{$\int_{-3}^3 \sqrt{1+\frac{x^2}{81-9x^2}}\ dx$}

\item {$\int_{1}^2 \sqrt{1+\frac1{x^4}}\ dx$}
\item {$\int_{-\pi/4}^{\pi/4} \sqrt{1+\sec^2x\tan^2x}\ dx$}

\end{enumerate}
\end{sol}

\end{ex}
% % % % % % % % % % % %

% % % % % % % % % % %
\begin{ex}
Use Simpson's Rule, with $n=4$, to approximate the arc length of each the function on the given interval in Exercise \ref{ex_al1}.

\begin{sol}
\begin{enumerate}
\item {$1.4790$}
\item {$1.8377$}
\item {$2.1300$}
\item
\item
\item
\item {$1.4058$}
\item {$1.7625$}
\end{enumerate}
\end{sol}

\end{ex}
% % % % % % % % % % % %

%%%%%%%%%%
\begin{ex}
 Find the arc length of $\ds f(x)=x^{3/2}$ on $[0,2]$.
\begin{sol}
 $\ds (22\sqrt{22}-8)/27$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $\ds f(x) = x^2/8-\ln x$
on $[1,2]$.
\begin{sol}
 $\ln(2)+3/8$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}

Find the arc length of $\ds f(x) = (1/3)(x^2 +2)^{3/2}$
on the interval $[0,a]$.
\begin{sol}
 $\ds a+a^3/3$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $f(x)=\ln(\sin x)$ on the
interval $[\pi/4,\pi/3]$.
\begin{sol}
 $\ds \ln((\sqrt2+1)/\sqrt3)$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Let $a>0$. Show that the length of $y=\cosh x$ on
$[0,a]$ is equal to $\ds \int _0 ^a \cosh x\,dx$.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $f(x)=\cosh x$ on $[0, \ln 2]$.
\begin{sol}
 $3/4$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Set up the integral to find the arc length of $\sin x$
on the interval $[0,\pi]$; do not evaluate the integral. If you have
access to appropriate software, approximate the value of the integral.
\begin{sol}
 $\approx 3.82$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Set up the integral to find the arc length of $\ds y=xe^{-x}$
on the interval $[2,3]$; do not evaluate the integral. If you have
access to appropriate software, approximate the value of the integral.
\begin{sol}
 $\approx 1.01$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Find the arc length of $\ds y=e^x$ on the interval $[0,1]$.
(This can be done exactly; it is a bit tricky and a bit long.)
\begin{sol}
 $\ds \sqrt{1+e^2}-\sqrt2+
{1\over2}\ln\left({\sqrt{1+e^2}-1\over\sqrt{1+e^2}+1}\right)+
{1\over2}\ln(3+2\sqrt2)$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


\documentclass[12pt]{scrartcl}

\input{preamble}

\makeatletter
\title{Hack 11.0}\let\Title\@title
\subtitle{Computer Science I\\
Encapsulation\\
{\small
\vskip1cm
Department of Computer Science \& Engineering \\
University of Nebraska--Lincoln}
\vskip-3cm}
%\author{Dr.\ Chris Bourke}
\date{~}
\makeatother

\begin{document}

\maketitle

\hrule

\input{instructions.tex}

Correctness:
\begin{itemize}
  \item 5 points for student tester (it is OK to test a function transitively; ie if createAirport uses initAirport and they only test createAirport; both are getting tested)
  \item 5 points for our ``visual'' inspection test
  \item 3 points for the 100 test cases for getAirDistance
  \item 3 points for the 100 test cases getEstimatedTravel time
\end{itemize}

\section*{Problem Statement}

There are thousands of commercial, military, and local airports in the US and
around the world.  The International Civil Aviation Organization maintains a
database of current and inactive airports around the world.  The database
uniquely identifies each airport by an alphanumeric GPS code.  Further, each
record contains the following pieces of data on each airport:
\begin{itemize}
  \item The name of the airport
  \item Its latitude in degrees in the range $[-90, 90]$ with negative values corresponding to the southern hemisphere
  \item Its longitude in degrees in the range $[-180, 180]$ with negative values corresponding to the western hemisphere
  \item The type of airport
  \item Its elevation in (whole) feet above sea level
  \item Its municipality and its country
\end{itemize}

You will design a C structure to encapsulate these attributes to model an
airport record from the ICAO database.  You will also design several functions
to support your structure including factory functions, functions to
create a string representation, print records, etc. You will also implement
several utility functions that use your structure to compute the air
distance(s) between airport locations using their latitude and longitude.
Recall that the air distance $d$ between two latitude/longitude points can be
estimated using the Spherical Law of Cosines.

 $$d = \arccos{(\sin(\varphi_1) \cdot \sin(\varphi_2) + \cos(\varphi_1) \cos(\varphi_2) \cos(\Delta) )} \cdot R$$
where
\begin{itemize}
  \item $\varphi_1$ is the latitude of location $A$, $\varphi_2$ is the latitude of location $B$
  \item $\Delta$ is the difference between location $B$'s longitude and location $A$'s longitude
  \item $R$ is the (average) radius of the earth, 6,371 kilometers
\end{itemize}
This formula assumes that latitude and longitude are in radians
$r$, $-\pi \leq r \leq \pi$.  To convert from degrees $d$ ($-180 \leq d \leq 180$)
to radians $r$, you can use the simple formula:
  $$r = \frac{d}{180} \pi$$

More details have been provided in a header file, \mintinline{text}{airport.h}.
You will need to design your structure and implement all of the specified
functions.

\section*{Instructions}

\begin{itemize}

  \item Place all of your function definitions in a source file named
  \mintinline{text}{airport.c} and hand it in with your header file,
  \mintinline{text}{airport.h}.  You may add any utility functions you
  wish but you must \emph{not} change any of the signatures of the required
  functions.

  \item In addition, you must create a main test driver program that
  demonstrates at least 3 cases per function.  Name this file
  \mintinline{text}{airportTester.c} and hand it in.

  \item You are encouraged to collaborate any number of students
  before, during, and after your scheduled hack session.

  \item You may (in fact are encouraged) to define any additional
  ``helper'' functions that may help you.

  \item Include the name(s) of everyone who worked together on
  this activity in your source file's header.

  \item Turn in all of your files via webhandin, making sure that
  it runs and executes correctly in the webgrader.  Each individual
  student will need to hand in their own copy and will receive
  their own individual grade.
\end{itemize}

\end{document}


The agenda for this section is as follows. First, we exemplify Stage~1 of our
framework by introducing a number of quantities of interest \g, which illustrate
the broad applicability of the framework and thereby give the reader a better
sense of its utility. Second, we turn to Stage~2 and highlight a transformation
$\transform$ that is the appropriate one to use in the majority of cases. Third,
we proceed directly to Stage~4 and illustrate a potential output of the
framework.

\subsection{\problemtitle}
\slab{frame-application-problem}

Assume the system, power, and temperature models described in
\sref{system-model}, \sref{power-model}, \sref{temperature-model}, respectively.
Assume also the application model utilized in \sref{dream-optimization-problem}
except for the requirement about being periodic.

Let us first touch upon the timing aspects of the system in question. Each of
the \nt tasks of the application has a start time and a finish time, which are
denoted by $b_i$ and $d_i$, respectively. Let also $\v{b} = (b_i)_{i = 1}^\nt$
and $\v{d} = (d_i)_{i = 1}^\nt$. Other timing characteristics can then be
derived from $\v{b}$ and $\v{d}$. For example, the end-to-end delay of the
application, which is the difference between the finish time of the latest task
and the start time of the earliest task, is as follows:
\begin{equation} \elab{frame-delay}
  \text{End-to-end delay}
  = \max_{i = 1}^\nt d_i - \min_{i = 1}^\nt b_i.
\end{equation}

Suppose now that the execution times of the tasks depend on the uncertain
parameters \vu introduced in \sref{frame-problem}. Then $\v{b}$ and $\v{d}$
depend on \vu. Hence, the end-to-end delay given in \eref{frame-delay} does too,
and it constitutes a quantity \g that the designer might be interested in
analyzing. Note that, since the $\min$ and $\max$ functions are
nondifferentiable, the same is true for \g. Therefore, \g is nonsmooth, which
renders the \up{PC} decomposition and similar techniques inappropriate in this
case in general, which is discussed in \sref{frame-motivation}.

\begin{remark} \rlab{frame-nonsmoothness}
The behavior of \g with respect to continuity, differentiability, and smoothness
cannot generally be inferred from the behavior of \vu. Even when the parameters
behave perfectly, \g might still exhibit nondifferentiability or even
discontinuity, which depends on how \g functions internally. For example, as
shown in \cite{tanasa2015}, even if the execution times of tasks are continuous,
end-to-end delays are very often discontinuous due to the actual scheduling
policy.
\end{remark}

Let us move on to power. The total energy consumed by the \np processing
elements during an application run can be estimated using a power profile \mp,
which is defined in \eref{power-profile}, as follows:
\begin{equation} \elab{frame-energy}
  \text{Total energy}
  = \sum_{i = 1}^\np \int \p_i(t) \d t
  \approx \dt \norm[1]{\mp}
\end{equation}
where $\p_i$ denotes the power consumption of processing element~$i$, \dt is the
sampling interval, and $\norm[1]{\cdot}$ stands for the Manhattan norm. Since
$\v{b}$ and $\v{d}$ depend on \vu, the power consumption of the system is also
dependent on \vu. Consequently, the total energy given in \eref{frame-energy}
depends on \vu and is a candidate for \g. \rref{frame-nonsmoothness} applies in
this context to the full extent.

Let us now turn to temperature. The maximum temperature that the platform
reaches during an application run can be estimated using a temperature profile
\mq, which is defined in \eref{temperature-profile}, as follows:
\begin{equation} \elab{frame-temperature}
  \text{Maximum temperature}
  = \max_{i = 1}^\np \sup_{t} \q_i(t)
  \approx \norm[\infty]{\mq}
\end{equation}
where $\q_i$ denotes the heat dissipation of processing element~$i$, and
$\norm[\infty]{\cdot}$ stands for the uniform norm. Since the power consumption
of the platform is affected by \vu, the corresponding heat dissipation is
influenced by \vu as well. Therefore, the maximum temperature in
\eref{frame-temperature} is also a potential quantity of interest \g. Note that,
due to the maximization involved in the calculations, the quantity is
nondifferentiable and hence cannot generally be adequately addressed using
polynomial approximations; recall also the concern in
\rref{frame-nonsmoothness}.

To summarize, we have covered three aspects of electronic systems, namely
timing, power, and temperature, and introduced a number of quantities that the
designer is typically interested in analyzing. These quantities will be
discussed further in the section on experimental results, \sref{frame-results}.

\inputfigure{frame-application}
Let us employ one of the introduced quantities in order to have a concrete
example to work with in this section. The problem being addressed is depicted on
the left-hand side of \fref{frame-application}. We consider a heterogeneous
platform with two processing elements denoted by PE1 and PE2 and an application
with four tasks denoted by T1--T4; the setup will be described in detail in
\sref{frame-results}. The data dependencies between the tasks and their mapping
onto the processing elements can be seen in \fref{frame-application}. The
quantity of interest \g is the application's end-to-end delay defined in
\eref{frame-delay}. The uncertain parameters \vu are the execution times of T2
and T4 denoted by $\u_1$ and $\u_2$, respectively.

The large rectangle on the left-hand side of \fref{frame-application} is a
``black box'' that evaluates \g given \vu. It takes an assignment of the
execution times $\u_1$ and $\u_2$ and outputs the calculated end-to-end delay
\g. In practice, this evaluation often involves a system simulator, such as
Sniper \cite{carlson2011}, in which case the modeling capabilities of this
simulator are naturally inherited by our technique.

Targeting the practical scenario described in \sref{chaos-formulation}, the
marginal distributions and correlation matrix of \vu are assumed to be
available. Without loss of generality, each marginal distribution is a
four-parameter beta distribution shown in \eref{beta-distribution}. Furthermore,
the execution times are assumed to be correlated based on the dependencies
between the corresponding tasks as defined by the structure of the application's
task graph. Specifically, the closer task~$i$ and task~$j$ are in the graph as
measured by the number of edges between vertex~$i$ and vertex~$j$, the more
strongly $\u_i$ and $\u_j$ are correlated.

\subsection{Probability Transformation}
\slab{frame-application-transformation}

At Stage~2 of our workflow outlined in \sref{frame-solution}, a suitable
transformation $\transform$, which is required in \eref{frame-transformation},
is chosen. We utilize the one shown in \eref{probability-transformation} and
reduce $\vu: \Omega \to \real^\nu$ to $\vz: \Omega \to [0, 1]^\nz$ so that the
latter is uniformly distributed. In this case, the rightmost $\transform_1$ in
\eref{probability-transformation} is simplified, since the marginals of \vz are
already uniform. Note that the model-order-reduction functionality of the chosen
$\transform$ is engaged; it eliminates redundant stochastic dimensions and,
therefore, assists the subsequent interpolation.

The result is that the obtained vector \vz conforms to the requirements listed
in \sref{frame-transformation}: the codomain of \vz is $[0, 1]^\nz$, and it has
the smallest number of dimensions that are necessary in order to preserve a
certain level of accuracy.

In \fref{frame-application}, $\transform$ is depicted as a small square. In this
particular example, the stochastic dimensionality is found to be the same before
and after $\transform$, which is indicated by the two incoming and two outgoing
arrows. The depicted component takes an assignment of the auxiliary variables
$\z_1$ and $\z_2$ and outputs the execution times $\u_1$ and $\u_2$ in
accordance with their joint distribution.

In the following, we proceed directly to Stage~4, since Stage~3, which is given
in the middle of \fref{frame-application}, is standard: using a number of
strategic invocations of the simulator of \g (the ``black box''), it delivers a
lightweight surrogate $\interpolant{\nz}{\ls}(\g)$, which is illustrated by a
rounded rectangle in \fref{frame-application}.

\subsection{Post-Processing}

Having constructed the interpolant $\interpolant{\nz}{\ls}(\g)$, the designer
starts to work solely with this interpolant, which corresponds to Stage~4 of our
framework.

Suppose the designer is interested in the probability distribution of \g. In
this scenario, $\interpolant{\nz}{\ls}(\g)$ should be sampled, which is
represented by the rightmost box in \fref{frame-application}. The operation
corresponds roughly to \aref{frame-evaluation}: the interpolant receives $\z_1$
and $\z_2$ and returns an approximation of the value of \g at that point. Recall
that the computational cost of this sampling is negligible, since \g is not
involved. The collected samples, which are denoted by $G$ in
\fref{frame-application}, are then utilized in order to estimate the
distribution of \g.

\inputfigure{frame-density}
\fref{frame-density} depicts the result. The blue line shows the \ac{PDF} of \g
computed by applying kernel density estimation \cite{hastie2013} to the data set
$G$. The orange line, which is barely visible behind the blue line, shows the
actual density of \g; its calculation is explained in \sref{frame-results}. It
can be seen that our solution closely matches the exact one. In addition, the
green line illustrates the estimate that the designer would obtain if \g was
sampled directly using the same number of evaluations as the one consumed by the
proposed framework. It can be seen that, given the same budget, the solution
delivered by our technique is substantially closer to the exact one than the one
delivered by direct sampling.