\chapter[Ionisation]{Ionisation}

 The class G4eIonisation calculates the continuous energy loss
  due to ionisation and
 simulates the 'discrete' part of the ionisation , the Moller and Bhabha
scattering or delta ray production by $e^-/e^+$ . The formulae used here
 are the same than those in GEANT3 (\cite{eion.geant3}).

\section{Method}

Let: \[\frac{d\sigma(E,T)}{dT}\]
be the differential cross-section
for the ejection of an electron with kinetic energy $T$ by an incident
${e^{\pm}}$ of total energy $E$ moving in a medium of density $\rho$.
 Let us denote the value of the {\em kinetic energy cut-off} or
 {\em production threshold for $\delta$-rays} by  $T_{cut}$.
Below this threshold the soft
electrons ejected are simulated as continuous energy loss by the incident
$e^+/e^-$, and above it they are explicitly generated.

The mean value of the energy lost by the incident ${\rm e^{\pm}}$ to
the soft $\delta$-rays is:
\begin{equation}
E_{soft}(E,T_{cut})
= \int_{0}^{T_{cut}} \frac{d \sigma (E,T)}{dT} T \: dT  \label{eion.a}
\end{equation}
whereas the total cross-section for the ejection of
an electron of energy \linebreak $T > T_{cut}$ is:
\begin{equation}
\sigma (E,T_{cut})
= \int_{T_{cut}}^{T_{Max}}\frac{d \sigma (E,T)} {dT} \: dT \label{eion.b}
\end{equation}
where $T_{Max}$ is the maximum energy transferable to the free electron:
\begin{equation}
\label{eion.c}
T_{Max} = \left\{ \begin{array}{ll}
             E-m & {for \hspace{.2cm} e^+}  \\
             (E-m)/2 & {for \hspace{.2cm} e^- ,} \\
              \end{array} \right .
\end{equation}
where $m$ is the electron mass.
The method of calculation of the continuous energy loss
and the total cross-section are
explained below.

\section{Continuous energy loss}
The integration of \ref{eion.a} leads to the Berger-Seltzer
formulae ( see e.g in
   \cite{eion.messel}) :

\begin{equation}
\label{eion.d}
\frac {dE}{dx} = \frac{2 \pi r_0^ 2 mn }{\beta^2}
       \left [\ln \frac{2(\tau + 2)} {(I/m)^2}+ F^{\pm} (\tau , \Delta )
- \delta \right ],
\end{equation}
 where

\[
\begin{array}{ll}
\gamma           & \frac{E}{m}                           \\
\beta^2          & 1-\frac{1}{\gamma^2}                  \\
\tau             & \gamma-1                              \\
\tau_c           & \frac{T_{cut}}{m}           \\
 T_{cut}         &  \mbox{energy cut for} \: e^{\pm}      \\
\tau_{max}       & \mbox{maximum possible energy transfer in $e^-$ mass:
  $\tau$ for $e^+$, $\tau/2$ for $e^-$}  \\
\Delta           & \min(\tau_c,\tau_{max})              \\
n                & \mbox{electron density of the medium}        \\
I                & \mbox{average mean ionisation energy}        \\
\delta           & \mbox{density effect correction}.
\end{array}
\]

The functions $ F^{\pm}$  are given by
\begin{equation}
\label{eion.e}
F^+ (\tau,\Delta) =\ln(\tau\Delta ) -
\frac{\Delta^2}{\tau}\left[\tau + 2 \Delta -
\frac{3\Delta^2 y } {2} -\left(\Delta - \frac{\Delta^3 }{3} \right) y^2
- \left (\frac{\Delta^2}{2} - \tau
       \frac{\Delta^3}{3} + \frac{\Delta^4 } {4} \right)
          y^3  \right]
\end{equation}

\begin{equation}
\label{eion.f}
F^- (\tau,\Delta ) = -1 -\beta^2 +\ln \left [(\tau - \Delta)
\Delta \right ] + \frac{\tau}{\tau -\Delta}+\frac{\left [
\frac{\Delta^2}{2} + ( 2\tau +1) \ln
\left (1- \frac{\Delta}{\tau} \right ) \right ]}{\gamma^2}
\end{equation}

where $ y = 1/(\gamma+1) $.

The density effect correction is calculated according to the
 formalism of Sternheimer ( \cite{eion.sternheimer}).

\section{Total cross-sections}
The integration of formula \ref{eion.b} gives the total cross-section
 (\cite{eion.messel}) for M\"{o}ller scattering ($e^- e^-$):
\begin{equation}
\label{eion.g}
\sigma ( Z,E,T_{cut} ) =\frac {2 \pi r_0^2 mZ}{\beta^2(E-m)}
 \left[\frac{(\gamma-1)} {\gamma^2}\left(\frac{1}{x}-1\right)
             +\frac{1}{x}-\frac{1}{1-x}-\frac{2\gamma-1}{\gamma^2}\ln
\frac{1-x}{x}\right]
\end{equation}
and for Bhabha scattering ($e^+ e^-$) :
\begin{equation}
\label{eion.h}
\sigma (Z,E,T_{cut}) =\frac{ 2 \pi r^2_0  mZ }
      {(E-m)}\left [\frac {1 }{\beta^2}  \left(\frac{1}{x}-1\right)
  + B_1 x + B_2 (1-x) -
 \frac {B_3 } {2} ( 1-x^2 ) +\frac{B_4}{3}(1-x^3)\right]
\end{equation}
where

\[
\begin{array}{ll}
\gamma = \frac{E}{m}       & \beta^2 = 1-\frac{1}{\gamma^2} \\ [.2cm]
 x     =\frac {T_{cut}}{E-m}  & \gamma=\frac{1}{\gamma + 1} \\ [.2cm]
 B_1=2-y^2                 &  B_2=(1-2y)(3+y^2)              \\ [.2cm]
B_3=(1-2y)^2+(1-2y)^3      &  B_4=(1-2y)^3
\end{array}
\]

The formulae [\ref{eion.g}] and [\ref{eion.h}]
give the total cross-section of the scattering
above the threshold energies

\begin{equation}
T_{\rm Moller}^{\rm thr} =2T_{cut}  \mbox{\hspace{2cm}and\hspace{2cm}}
T_{\rm Bhabha}^{\rm thr} = T_{cut}
\end{equation}
The interaction length for the production of $\delta$-rays is calculated
during initialisation .

\section{Simulation of the $\delta -ray$ production}
\subsection{Differential cross section}

The differential cross-section of the $\delta$-ray production can
be written as in equation \ref{eion.i} , \ref{eion.j}
 (\cite{eion.messel}). For the
electron/electron (M\"{o}ller) scattering we have:
\begin{equation}
\label{eion.i}
\frac{d\sigma }{d \epsilon }=\frac{2 \pi Z r^2 _0 m }{\beta^2 (E-m)}
\left[ \frac{(\gamma -1 )^2}  {\gamma^2 }+\frac{1}{\epsilon}
\left(\frac{1}{\epsilon}-\frac{2 \gamma -1 } {\gamma^2 } \right) +
\frac{1}{1- \epsilon}\left(\frac{1} {1- \epsilon} \frac{2 \gamma - 1}
{\gamma^2 }\right)  \right]
\end{equation}
and for the positron-electron (Bhabha) scattering:
\begin{equation}
\label{eion.j}
\frac{d \sigma}{d \epsilon}=\frac{2 \pi Z r^2_0 m }{(E-m)}\left[
\frac{1} {\beta^2 \epsilon^2}-\frac{B_1}{\epsilon}+B_2 - B_3 \epsilon
+ B_4 \epsilon^2\right]
 \end{equation}

where

\[
\begin{array}{lcllcl}
Z        & = & \mbox{atomic number of the medium}   &
E        & = & \mbox{energy of the incident particle} \\
M        & = & \mbox{rest mass of the incident particle} &
\gamma   & = & \frac{E}{M}   \\ [.2cm]
\beta^2  & = & 1- \frac{1} {\gamma^2 } &
 y       & = & \frac{1} {\gamma + 1}               \\ [.2cm]
 B_1     & = & 2-y^2                   &
 B_2     & = & (1-2y)(3+y^2 )                       \\
 B_3     & = & (1-2y)^2+(1-2y)^3 &
 B_4     & = & (1-2y)^3  \\
\epsilon & = & \frac{T} {E-m}
\end{array}
\]

with $T$ the kinematic energy of the scattered electron (of the lower
energy in the case of $e^+ e^-$ scattering).

The kinematical limits for the variable $\epsilon$ are:

\[
\epsilon_0 = \frac{T_{cut}}{E-m} \leq \epsilon \leq \frac{1}{2}
\mbox{\hspace{.2cm} for $e^- e^-$} \hspace{2cm}
\epsilon_0 = \frac{T_{cut}}{E-m} \leq \epsilon \leq 1
\mbox{\hspace{.2cm} for $e^+ e^-$}
\]

\subsection{Sampling}

Apart from the normalisation, the cross-section can be written as
\[
\frac{d\sigma}{d\epsilon}=f(\epsilon) g(\epsilon),
\]
where, for $e^- e^-$ scattering,
\begin{eqnarray*}
f(\epsilon)&=&\frac{1}{\epsilon^2} \frac{\epsilon_0 }{1- 2\epsilon_0} \\
g(\epsilon)&=&\frac{4}{9\gamma^2 - 10 \gamma + 5}\left[(\gamma -1)^2
\epsilon^2 - (2 \gamma^2 +2\gamma -1) \frac{\epsilon} {1- \epsilon }+
\frac{\gamma^2}{(1- \epsilon )^2 }\right]
\end{eqnarray*}
and for $e^+ e^-$ scattering
\begin{eqnarray*}
  f(\epsilon)&=&\frac{1}{\epsilon^2} \frac{\epsilon_0}{1- \epsilon_0 } \\
  g(\epsilon)&=&\frac{B_0 -B_1 \epsilon +B_2 \epsilon^2
     -B_3 \epsilon^3 +B_4 \epsilon ^4}{B_ 0-B_1\epsilon_0
+B_2\epsilon^2_0
    -B_3 \epsilon^3_0 +B_4 \epsilon^4_0 }
\end{eqnarray*}
Here $ B_0=\gamma^2/(\gamma^2-1)$ and
all the other quantities have been defined above.

The variable $\epsilon$ is sampled by:
\begin{enumerate}
\item sample $\epsilon$ from $f(\epsilon)$
\item calculate the rejection function $g(\epsilon)$ and accept the
sampled $\epsilon$ with a probability of $g(\epsilon)$.
\end{enumerate}

After the successful sampling of $\epsilon$,  the direction
 of the scattered electron is generated with respect to the direction of the
incident particle. The azimuthal angle $\phi$ is generated isotropically;
the polar angle
$\theta$ is calculated from the energy momentum conservation.
This information
is used to calculate the energy and momentum of both scattered
particles and to transform them into the {\em global} coordinate system.

\section{Status of this document}

 9.10.98     created by L. Urb\'an.

\begin{thebibliography}{99}

\bibitem[GEANT3]{eion.geant3}
  GEANT3 manual ,CERN Program Library Long Writeup W5013 (October 1994).
\bibitem[Mess70]{eion.messel}
  H.Messel and D.F.Crawford. Pergamon Press,Oxford,1970.
\bibitem[Ster71]{eion.sternheimer}
  R.M.Sternheimer. Phys.Rev. B3 (1971) 3681.

\end{thebibliography}


\documentclass[a4paper,11pt]{article}
\usepackage{setspace}
\usepackage[utf8]{inputenc}
%\usepackage{subfigure}

\pagestyle{plain}

\usepackage{amssymb, bm, blkarray, multicol}
\usepackage{listings,xcolor,lmodern}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[nottoc]{tocbibind}
\usepackage{upgreek}

\usepackage{mathtools}
\mathtoolsset{showonlyrefs}

\usepackage{hyperref}
\usepackage{url}
\usepackage{nicefrac}
\usepackage{microtype}

\usepackage{latexsym}
% \usepackage{a4wide}

\newtheorem{theorem}{THEOREM}
\newtheorem{lemma}[theorem]{LEMMA}
\newtheorem{corollary}[theorem]{COROLLARY}
\newtheorem{proposition}[theorem]{PROPOSITION}
\newtheorem{remark}[theorem]{REMARK}
\newtheorem{definition}[theorem]{DEFINITION}
\newtheorem{fact}[theorem]{FACT}

\newtheorem{problem}[theorem]{PROBLEM}
\newtheorem{exercise}[theorem]{EXERCISE}
\def \set#1{\{#1\} }

\lstset{
  basicstyle=\ttfamily,
  columns=fullflexible,
  frame=single,
  breaklines=true,
  postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}

\newcommand{\E}{\mathbb{E}}
\newcommand{\var}{\mathrm{Var}}
\newcommand{\cov}{\mathrm{Cov}}
\newcommand{\KL}{\mathrm{KL}}
\newcommand{\neighb}{\text{ne}}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\newcommand{\Perp}{\mathrel{\text{\scalebox{1.07}{$\perp\mkern-10mu\perp$}}}}

\usetikzlibrary{shapes.geometric}

\begin{document}
Existing tests test moments of the marginal distributions of $\theta$ and sometimes the evaluations of $p(y|\theta), p(\theta)$. What test functions should we choose for the MMD tests?

\section{MMD-SC}

Let $q_{\text{SC}}$ denote the transition kernel for Algorithm \ref{alg:sc-sampler}. Then $q_{\text{SC}}(\theta', y' | \theta, y) = q(\theta' | y') p(y' | \theta) q(\theta, y)$ and
\begin{align*}
    q_{\text{SC}}(\theta', y') &= \int q_{\text{SC}}(\theta', y' | \theta, y) q(\theta, y) d\theta dy \\
                    &= \int q_{\text{SC}}(\theta' | y') p(y' | \theta) q_{\text{SC}}(\theta, y) d\theta dy \\
                    &= q_{\text{SC}}(\theta' | y') \int p(y' | \theta) q_{\text{SC}}(\theta) d\theta \\
\end{align*}
If the marginals are correct, $q_{\text{SC}}(\theta) = p(\theta)$ and
\begin{align*}
    q_{\text{SC}}(\theta', y') = q_{\text{SC}}(\theta' | y') \int p(y' | \theta) q_{\text{SC}}(\theta) d\theta = q_{\text{SC}}(\theta' | y') p(y')
\end{align*}
$q_{\text{SC}}(\theta' | y') = p(\theta'|y') \rightarrow q_{\text{SC}}(\theta' | y') p(y') = p(\theta', y') \rightarrow q_{\text{SC}}(\theta) = p(\theta) $, but the other direction does not necessarily hold. Equality of the marginal distributions of $\theta$ is not enough to show equality of the joints.

However, $q_{\text{SC}}(\theta) = p(\theta)$ does imply $q_{\text{SC}}(y) = p(y)$, so
\begin{align*}
    q_{\text{SC}}(\theta' | y') &= \frac{q_{\text{SC}}(y' | \theta') q_{\text{SC}}(\theta')}{q_{\text{SC}}(y')} \\
                                &= \frac{q_{\text{SC}}(y' | \theta') p(\theta')}{p(y')} \\
\end{align*}

Thus, if $q_{\text{SC}}(y' | \theta') = p(y' | \theta')$, then $q_{\text{SC}}(\theta' | y') = p(\theta' | y')$ and the joint distributions are the same.

Is testing the evaluation of the likelihood enough? Example where it is not:
\begin{align*}
    \theta &\sim \mathcal{U}(\{0, 1\}) \\
    y &\sim \mathcal{U}(\{0, 1\}) \\
    p(y | \theta) &= 0.25
\end{align*}
vs
\begin{align*}
    \theta &\sim \mathcal{U}(\{0, 1\}) \\
    q(y=i|\theta) &= \begin{cases}
                            1 & \theta = i \\
                            0 & \text{otherwise}
                        \end{cases} \quad i \in \{0, 1\}
\end{align*}
The evaluations of $p(y | \theta)$ and the marginals are the same, but the joint distributions differ; the first model gives $(i, j) \quad \forall i, j \in \{0,1\}$, while the second gives $(i, i)$. This example relies on the independence of $\theta$ and $y$.

Can we come up with other examples? How might these arise in MCMC?

\section{MMD-BC}
Let $q_{\text{BC}}$ denote the joint distribution from Algorithm \ref{alg:bc-sampler}.
\begin{align*}
    q_{\text{BC}}(\theta', y') &= q_{\text{BC}}(\theta'|y')q(y') = q_{\text{BC}}(\theta'|y')p(y') \\
\end{align*}
By the same logic as for the MMD-SC test, if $q_{\text{BC}}(y' | \theta') = p(y' | \theta')$, then $q_{\text{BC}}(\theta' | y') = p(\theta' | y')$ and the joint distributions are the same.
\newpage

\begin{algorithm}
    \centering
    \caption{Marginal-conditional (MC) joint simulator}\label{alg:mc-sampler}
    \begin{algorithmic}[1]
        \State \text{Initialize} $\mathbf{g}_{MC} \in \mathbb{R}_{N\times |\mathbf{g}|}$
        \For{$n = 1, \ldots, N$}
            \State $\mathbf{\Theta}_{n} \sim P(\mathbf{\Theta})$
            \State $\mathbf{Y}_{n} \sim P(\mathbf{Y}|\mathbf{\Theta}_{n})$
            \State $\mathbf{g}_{MC}[n, :] = \mathbf{g}(\mathbf{\Theta}_{n}, \mathbf{Y}_{n})$
        \EndFor
        \State \textbf{return} $\mathbf{g}_{MC}$
    \end{algorithmic}
\end{algorithm}

\begin{algorithm}
    \centering
    \caption{Successive-conditional (SC) joint simulator}\label{alg:sc-sampler}
    \begin{algorithmic}[1]
        \State \text{Initialize} $\mathbf{g}_{SC} \in \mathbb{R}_{N\times |\mathbf{g}|}$
        \State $\mathbf{\Theta}_{0} \sim P(\mathbf{\Theta})$
        \For{$n = 1, \ldots, N$}
            \State $\mathbf{Y}_{n} \sim P(\mathbf{Y}|\mathbf{\Theta}_{n-1})$
            \State $\mathbf{\Theta}_{n} \sim \text{PosteriorSampler}(\mathbf{\Theta}_{n-1}, \mathbf{Y}_{n})$
            \State $\mathbf{g}_{SC}[n, :] = \mathbf{g}(\mathbf{\Theta}_{n}, \mathbf{Y}_{n})$
        \EndFor
        \State \textbf{return} $\mathbf{g}_{SC}$
    \end{algorithmic}
\end{algorithm}

\begin{algorithm}[H]
    \centering
    \caption{Backward-conditional (BC) joint simulator}\label{alg:bc-sampler}
    \begin{algorithmic}[1]
        \State \text{Initialize} $\mathbf{g}_{BC} \in \mathbb{R}_{N\times |\mathbf{g}|}$
        \For{$n = 1, \ldots, N_{BC}$}
            \State $\mathbf{\Theta}_{0} \sim P(\mathbf{\Theta})$
            \State $\mathbf{Y}_{n} \sim P(\mathbf{Y}|\mathbf{\Theta}_{0})$
            \For{$m = 1, \ldots, M$}
                \State $\mathbf{\Theta}_{n} \sim \text{PosteriorSampler}(\mathbf{\Theta}_{n}, \mathbf{Y}_{n})$
            \EndFor
            \State $\mathbf{g}_{BC}[n, :] = \mathbf{g}(\mathbf{\Theta}_{n}, \mathbf{Y}_{n})$
        \EndFor
        \State \textbf{return} $\mathbf{g}_{BC}$
    \end{algorithmic}
\end{algorithm}

\end{document}


\section{Introduction}

Sequence annotation is a typical task in Natural Language Processing (NLP) domain.
For language sample, we only know the obverse state, we want to learn the best probability of the hidden state.
This problem most time can't be Simplified as one-by-one classify the problem.
And sequence annotation some have disparity positive-negative sample ratio.
It means that a single classify mechanism can't work well in sequence annotation.
If you can extract global info, we will learn a global optimize hidden state layer.
For the traditional method, we can use the Hidden Markov Model(HMM), Conditional Random Fields(CRF), DNN to extract hidden state info from observing the result.
But it only a probability maximize tool, It can't extract semantic info from superficial features. In the previous work, I test for single CRF Layer from the pre-train embed. But single CRF layer can't work well in sequence annotation. It means you must provide an info extract layer to struct semantic representation mechanism.

In the last few years, deep learning techniques have significantly out-performed traditional methods in several NLP tasks,  and sequence annotation is no exception to this trend.
Especially Bert published on last year. Two of the most popular deep learning techniques for sequence annotation are BiLSTM + CRF and Transform model like Bert + CRF.
In this paper, we evaluation both two model.

In this paper, I test for two sequence annotation method in Chinese traditional Corps for two specific work of sequence annotation(CWS, NER). Our first model base on a BiLSTM model, equipped with CRF loss function.
The other model base on Bert which is a pre-training and fine-tuning model by the 12-layers transform.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            First Chapter                            %
%                         Problem Formulation                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{Posture Generation: Problem Formulation}
\label{cha:posture_generation_problem_formulation}

%\nomenclature[z-PG]{PG}{Posture Generation}
%\nomenclature[z-IK]{IK}{Inverse Kinematics}
%\nomenclature[z-DoF]{DoF}{degrees of freedom}
%\nomenclature[z-w.r.t]{w.r.t}{with respect to}
%\nomenclature[x-I]{$\mathbb{I}_n$}{Matrix identity of dimension $n$}
%\nomenclature[x-w]{$\wedge$}{cross product}
%\nomenclature[a-F]{F}{a frame}
%\nomenclature[a-W]{W}{the world frame}
%\nomenclature[a-w]{w}{a wrench or a force}
%\nomenclature[a-f]{f}{a force resultant}
%\nomenclature[a-m]{m}{a force moment}

\graphicspath{{Chapter2-PG/Figs/}}

%{{{List of contributions
%\section{List of contributions}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                   SECTION LIST OF CONTRIBUTIONS                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\begin{itemize}
  %\item Generalities, introduction
  %\item Presentation of the existing methods
  %\item From Inverse Kinematics to Generalized IK/posture Generation/pose estimation (addition of articular limits, forces, stability etc).
  %\item Topology of the parametrization space (Free-flyer, q, f, other)
  %\item Formulation as a nonlinear constrained optimization problem
  %\item Adrien \& Karim's formulations
  %\item Formulation of several types of cost/constraints
  %\begin{itemize}
    %\item Contact with planar surface
    %\item Collision avoidance
    %\item Auto-Collision avoidance
    %\item Static equilibrium: Newton/CoM projection
    %\item Forces in friction cones
    %\item Articular limits
    %\item Torque limits
    %\item Torque minimization
    %\item Goal Posture
  %\end{itemize}
  %\item Reasons why it is not enough and why we needed a new PG
    %\begin{itemize}
      %\item Having an easier way to formulate problems
      %\item Avoid having to de some gymnastic to remain on manifolds
      %\item Automatic variable management
      %\item Robustness
    %\end{itemize}
  %\item Utilization of posture generation in planning
%\end{itemize}
%}}}

In this Chapter, we present in detail the formulation of a posture generation problem.
We present the algorithms used to compute the kinematics of a robot and its derivatives as well as the joint torques.
Then we formulate some classical functions that are often used in posture generation: joint limits, contact constraints, collision avoidance, stability, torque limits and friction cones.
In~\Figref{fig:PG}, we illustrate those constraints with the result of a posture generation problem where the HRP-4 robot must climb on a stack of cubes while being statically stable, respecting its joint and torque limits, the contact forces must remain within their respective friction cones and the robot must avoid auto-collisions and non-desired contacts (i.e. collisions) with the environment.

%\begin{figure}[ht]
  %\centering
  %\includegraphics[width=0.7\textwidth]{PG.pdf}
  %\caption{HRP4 on a stack of cubes. The color of each box corresponds to the color of the item it depicts.}
%\label{fig:PG}
%\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                    SECTION PROBLEM FORMULATION                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\section{Kinematics Formulation}
%\label{sec:kinematics_formulation}

\section{Forward Kinematics}
\label{sec:forward_kinematics}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                   SUBSECTION FORWARD KINEMATICS                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In this section, we present a formulation of robotic systems that allows specifying most of the typical constraints encountered in robotics problems.

We consider a robotic system made of $n_B$ bodies and $n_J(=n_B+1)$ joints.
The global structure of the robot is described by an ordered graph called multibody graph.
The base body (World) has index $0$ and each of the remaining bodies get a different positive integer index.
We denote the body of index $i$, $B_i$.
$B_0$ refers to the World.
Each body $B_i$ has its reference frame $F_i$ attached to it.
$F_0$ denotes the World frame.
Bodies are linked together by joints that also are indexed by positive integers, we denote the joint of index $i$, $J_i$, and the body that comes after it is $B_i$.
Each joint defines the relation between its predecessor and successor bodies.
For joint $J_i$, they are respectively denoted $pred(i)$ and $succ(i)$, and $B_{pred(i)}$ is called the parent body of $B_{succ(i)}$.
We denote $\lambda(j)$ the index of the parent body of $B_j$.
The number of degrees of freedom of $J_i$ is denoted $dof^J_i$ and the number of degrees of freedom of the whole robot is denoted $dof$.
\Figref{fig:mbg} illustrates this numbering system for a simple robot with 4 joints and 5 bodies (including the basis)

The geometric relations between bodies and joints are described through transformations between their reference frames.
We use transformations as described in the Spatial Vector Algebra chapter of `Rigid Body Dynamics Algorithm' by Roy Featherstone~\cite{featherstone:book:2007}.
Motion vectors (vectors describing motion quantities as positions, velocities and accelerations) and their force counterpart are defined in~\cite{featherstone:book:2007}.

For any 3D vector $v\in\mathbb{R}^3$, $\hat{v}$ denotes the $3\times 3$ skew-symmetric matrix such that $\hat{v}u = v\wedge u$.
Where $\wedge$ denotes the cross product operator.

Let A and B be Cartesian frames with origins O and P respectively.
Let $\mathbf{t}$ be the coordinate vector expressing $\overrightarrow{OP}$ in A.
And $\mathbf{R}$ be the rotation matrix that transforms 3D vectors from A to B coordinates.
The transformation from A to B for a motion vector is defined by:
\begin{equation}
  {}^B X_A =
  \begin{bmatrix}
    \mathbf{R} & \mathbf{0} \\
    -\mathbf{R}\hat{\mathbf{t}} & \mathbf{R} \\
  \end{bmatrix}
\end{equation}
Its inverse is:
\begin{equation}
  {{}^B X_A}^{-1} = {}^A X_B =
  \begin{bmatrix}
    \mathbf{R}^T & \mathbf{0} \\
    \hat{\mathbf{t}}\mathbf{R}^T & \mathbf{R}^T \\
  \end{bmatrix}
\end{equation}
The transformation from A to B for a force vector is defined by:
\begin{equation}
  {}^B X_A^* =
  \begin{bmatrix}
    \mathbf{R} & -\mathbf{R}\hat{\mathbf{t}} \\
    \mathbf{0} & \mathbf{R} \\
  \end{bmatrix}
\end{equation}
Its inverse is:
\begin{equation}
  {}^B X_A^{-*} = {}^A X_B^* =
  \begin{bmatrix}
    \mathbf{R}^T & \hat{\mathbf{t}}\mathbf{R}^T \\
    \mathbf{0} & \mathbf{R}^T \\
  \end{bmatrix}
\end{equation}

\begin{figure}
  \centering
  \includegraphics[width=0.7\textwidth]{mbg.pdf}
  \caption{MultiBody graph}
\label{fig:mbg}
\end{figure}

%Each joint $J_i$ is defined in the reference frame of its predecessor body by a static transformation $X^x_i = \{\mathbf{R}^x_i, \mathbf{t}^x_i\}$ from the base of the body to the base of the joint.
Each joint $J_i$ is defined by a static transformation $X^x_i = \{\mathbf{R}^x_i, \mathbf{t}^x_i\}$ between the reference frame of its predecessor body and its own reference frame.
Each joint $J_i$ is associated with a motion subspace which representation matrix is denoted $S_i$.
Each column of $S_i$ described a degree of freedom of $J_i$ its upper part for the rotations and lower for translations (see Section~\ref{sec:joints_formulations}).
\begin{equation}
  S_i =
  \begin{bmatrix}
    S^R_{i,0} & \cdots &
    S^R_{i,j} & \cdots &
    S^R_{i,dof} \\
    S^t_{i,0} & \cdots &
    S^t_{i,j} & \cdots &
    S^t_{i,dof}
  \end{bmatrix}
\end{equation}

For a given joint configuration $\mathbf{q}$, the transformation due to the joint $J_i$ current configuration from its reference frame to the reference frame of its successor body is denoted \\$X^J_i (\mathbf{q}) = \{\mathbf{R}^J_i (\mathbf{q}), \mathbf{t}^J_i (\mathbf{q})\}$.

The transformation between $B_{\lambda(i)}$ and $B_i$ is denoted $X^{PtS}_i (\mathbf{q}) = \{\mathbf{R}^{PtS}_i, \mathbf{t}^{PtS}_i\}$ (PtS stands for `Parent to Son') can then be computed as:
\begin{equation}
  {X}^{PtS}_i (\mathbf{q}) = {}^{i}X_{\lambda (i)} (\mathbf{q}) = X^J_i (\mathbf{q}) X^x_i
  \label{eq:PtS}
\end{equation}

Let $\kappa (i) =\{0, i_1, i_2 \ldots i\}$ be the list of indexes of successive joints going from $B_0$ to $B_i$.
It can easily be computed by adding iteratively the parent of the current body:

\begin{algorithm}
  \caption{Joint Path to $B_i$}
\label{alg:JP}
\begin{algorithmic}
  \State{$j \leftarrow i$, $\kappa(i)=[i]$}
  \While{$j \neq 0$}
  \State{$j \leftarrow \lambda(j)$}
  \State{$\kappa(i) \leftarrow [\kappa(i),\ j]$}
  \EndWhile{}
\end{algorithmic}
\end{algorithm}

The transformation from the World base to $B_i$ is denoted \\ ${}^i X_0 (\mathbf{q}) = \{{}^i \mathbf{R}_0 (\mathbf{q}), {}^i \mathbf{t}_0 (\mathbf{q})\}$.
The formula~\Eqref{eq:PtS} can be used iteratively on all bodies of the robot to obtain the expression of ${}^i X_0 (\mathbf{q})$.

We obtain the full expression of ${}^i X_0$ as:
\begin{equation}
  {}^i X_0 (\mathbf{q}) = \prod_{j\in\kappa (i)}X^J_j (\mathbf{q}) X^x_j = \prod_{j\in\kappa (i)}\ {}^j X_{\lambda (j)}
  = {}^i X_{\kappa (1)}\ ^{\kappa (1)}X_{\kappa (2)} \dots ^{\kappa (\text{end}-1)}X_{W}
\end{equation}

Which can be computed recursively by a Forward Kinematics algorithm:

\begin{algorithm}
  \caption{Forward Kinematics}
\label{alg:FK}
\begin{algorithmic}
  \For{$i = 0:n_J$}
  \If{$\lambda (i) \neq -1$} ${}^i X_0 = {}^i X_{\lambda (i)}\ ^{\lambda (i)}X_0$
  \Else$\ {}^i X_0 = X^{PtS}_i$
  \EndIf{}
  \EndFor{}
\end{algorithmic}
\end{algorithm}

In the following section, we provide some detailed description of how to compute $X_J (\mathbf{q})$ for a variety of useful joints.
Using the joint descriptions and the Forward Kinematics algorithm, we are able to explicit a relation between $\mathbf{q}$ the joint parameters of the robot and the 3D position and orientation of any geometric quantity defined in the reference frame of a body of the robot.
Given a transformation ${}^p X_i$ defined in the frame of $B_i$, its value in the world frame is given by ${}^p X_0 (\mathbf{q}) = {}^p X_i\ {}^i X_0 (\mathbf{q})$

\section{Joints formulations}
\label{sec:joints_formulations}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                   SUBSECTION JOINTS FORMULATIONS                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The entire geometry of our system is described by the list of static transformations $X^x_j$ and of joint transformations $X^J_j (q)$.
In this section, we explicit the descriptions and formulations of common joints type.

Let us consider a joint $J$ that governs the transformation between two frames $F_1=\{O_1, x_1, y_1, z_1\}$ and $F_2=\{O_2, x_2, y_2, z_2\}$.
The most common type of joint encountered in robotics (and even virtual avatars) systems is the revolute joint, that allows a rotation around a fixed axis.
If $J$ is a revolute joint around the axis $(O_1,z_1)$ with parameter $q$, its motion subspace, rotation, and translation are as follows:
\begin{table} [H]
\centering
\begin{tabular}{cccc}
  \toprule
  Joint type & $S$ & $Rotation$ & $translation$ \\
  \midrule
  Revolute $(O_1,z_1)$
  &
  $\begin{bmatrix}
    0 \\ 0 \\ 1 \\ 0 \\ 0 \\ 0
  \end{bmatrix}$
  &
  $\begin{bmatrix}
    1 & 0 & 0 \\
    0 & \cos (q) & \sin (q) \\
    0 & -\sin (q) & \cos (q) \\
  \end{bmatrix}$
  &
  ${\bf 0}_{3\times1}$
  \\
  \bottomrule
\end{tabular}
\end{table}

Similar formulas can be devised for rotations around any other axis, provided that $R$ describes the rotation of angle $q$ around that axis.

In the case of a prismatic joint, all rotations are blocked, and only one translation along a given axis is allowed.
A prismatic joint along $x_1$ is described by the following formulas:
\begin{table}[H]
\centering
\begin{tabular}{cccc}
  \toprule
  Joint type & $S$ & $Rotation$ & $translation$ \\
  \midrule
  Prismatic $(x_1)$
  &
  $\begin{bmatrix}
    0 \\ 0 \\ 0 \\ 1 \\ 0 \\ 0
  \end{bmatrix}$
  &
  ${\bf 1}_{3\times3} $
  &
  $\begin{bmatrix}
    q \\ 0 \\ 0
  \end{bmatrix}$
  \\
  \bottomrule
\end{tabular}
\end{table}

Planar joints are also frequently used in robotics.
A planar joint describes a plane sliding on another plane. Assuming that the normal to both planes is $z_1 = z_2$, this type of joint allows free relative rotation of $F_2$ around $z_1$ and relative translations along $x_1$ and $y_1$.
We denote $q = \{q_1, q_2, q_3\}$ the joint parameters, $q_1$ corresponding to the rotation and $q_2,\ q_3$ to the translations.
We get:
\begin{table}[H]
\centering
\begin{tabular}{cccc}
  \toprule
  Joint type & $S$ & $Rotation$ & $translation$ \\
  \midrule
  Planar $(z_1)$
  &
  $\begin{bmatrix}
    0 & 0 & 0 \\ 0 & 0 & 0 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ 0 & 0 & 0
  \end{bmatrix}$
  &
  $\begin{bmatrix}
    \cos(q_1) & \sin(q_1) & 0 \\
    -\sin(q_1) & \cos(q_1) & 0 \\
    0 & 0 & 1 \\
  \end{bmatrix}$
  &
  $\begin{bmatrix}
    \cos(q_1)q_2 - \sin(q_1)q_3 \\ \sin(q_1)q_2 + \cos(q_1)q_3 \\ 0
  \end{bmatrix}$
  \\
  \bottomrule
\end{tabular}
\end{table}

A spherical joint blocks all translations and allows all rotations.
This joint must be parametrized by a 3D rotation.
The space of 3D rotations $SO(3)$ can be represented in many different ways.
The simplest and most intuitive way to parametrize $SO(3)$ is to use Euler Angles.
It comes down to decomposing the 3D rotation into a succession of three 1D rotations around different axes.
For example, the roll, pitch, yaw is a succession of a rotation of $F_1$ around its $x$ axis, followed by a rotation around the $y$ axis of the resulting frame, finally a rotation around the $z$ axis of the resulting  frame of the latter rotation.
The rotation matrix for such a convention is given by:
\begin{equation}
  {\bf R} =
  \begin{bmatrix}
    1 & 0 & 0 \\
    0 & \cos(q_3) & \sin(q_3) \\
    0 & -\sin(q_3) & \cos(q_3) \\
  \end{bmatrix}
  \cdot
  \begin{bmatrix}
    \cos(q_2) & 0 & -\sin(q_2) \\
    0 & 1 & 0 \\
    \sin(q_2) & 0 & \cos(q_2) \\
  \end{bmatrix}
  \cdot
  \begin{bmatrix}
    \cos(q_1) & \sin(q_1) & 0 \\
    -\sin(q_1) & \cos(q_1) & 0 \\
    0 & 0 & 1
  \end{bmatrix}
\end{equation}

Euler Angle formulations have the advantage to be simple and intuitive.
There are many other possible choices of the rotation order and conventions.
But they all suffer from the so-called gimbal-lock problem or more generally from singularities, which happens when two of the three rotation axes become aligned.
In such a configuration, the only rotations possible are one rotation around the two aligned axis and one rotation around the third axis.
Thus, one degree of freedom is lost.
Those singularities are prohibitive for the use of that type of formulation in a posture generation.
In~\cite{grassia1998}, Grassia states that any attempt to parametrize the entire set of 3D rotations by an open subset of Euclidean space (as do Euler angles) will suffer from gimbal lock.
Note that this singularity is only due to the user's choice of parametrization, it is not intrinsic to the manifold $SO(3)$.
%One can prove that there is no smooth mapping between $SO(3)$ and $\mathbb{R}^3$ that is free of singularities.
It is possible to parametrize $SO(3)$ without having to face singularities by parametrizing it over another non-Euclidean manifold.
The most common ones are the set of unit quaternion embedded in $\mathbb{R}^4$ and the set of rotation matrices embedded in $\mathbb{R}^{3\times 3}$.
%To avoid singularities, one must use a higher dimension parametrization, such as the most common $SO(3)$ representation in robotics that are the unit quaternions and the rotation matrices.
%The unit quaternions space is the subset of $\mathbb{R}^4$ where all elements have unit norm.
With the unit quaternion parametrization, a variable on $SO(3)$ is represented by 4 parameters $q = [q_w, q_x, q_y, q_z]$, and it is necessary to ensure that the quaternion is of norm 1, $\{q\in\mathbb{R}^4:||q||=1\}$.
Similarly, if a variable is parametrized by a rotation matrix, then the matrix $M$ representing it has 9 parameters and M must be orthogonal and have determinant 1: $\{M\in\mathbb{R}^{3\times 3}:M^T M = \mathbf{1}_3\  \text{and}\ \det (M) = 1\}$.
Similar issues can be found with the parametrization of other non-Euclidean manifolds, like $S^2$ for example.

A quaternion $q = [q_w, q_x, q_y, q_z]$ is a unit quaternion iff $q_w^2+q_x^2+q_y^2+q_z^2 = 1$.
It represents a rotation of angle $\theta$ around an axis ${\bf u}$ such that:
\begin{align}
  q_w &= \cos(\theta/2) \\
  q_x &= \sin(\theta/2)u_x \\
  q_y &= \sin(\theta/2)u_y \\
  q_z &= \sin(\theta/2)u_z \\
\end{align}
The rotation matrix associated with this quaternion is:
\begin{equation}
  {\bf R} = 2 \begin{bmatrix}
    \frac{1}{2} - {q_y}^2 - {q_z}^2 &	q_x q_y - q_z q_w &	q_x q_z + q_y q_w \\
    q_x q_y + q_z q_w	& \frac{1}{2} - {q_x}^2 - {q_z}^2 &	q_y q_z - q_x q_w \\
    q_x q_z - q_y q_w &	q_y q_z + q_x q_w	& \frac{1}{2} - {q_x}^2 - {q_y}^2 \\
  \end{bmatrix}
\end{equation}

That formulation does not suffer from singularities, but it requires to maintain 4 parameters for a 3D rotation.
And those 4 parameters must satisfy the unit norm constraint which in turn would become an additional constraint in the optimization formulation.
Given a parameter set $q = \{ q_w, q_x, q_y, q_z\}$, we get the following table.

\begin{table}[ht]
  \centering
  \begin{tabular}{cccc}
    \toprule
    Joint type & $S$ & $Rotation$ & $translation$ \\
    \midrule
    Spherical
    &
    $\begin{bmatrix}
      1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0
    \end{bmatrix}$
    &
    $2 \begin{bmatrix}
    \frac{1}{2} - {q_y}^2 - {q_z}^2 &	q_x q_y - q_z q_w &	q_x q_z + q_y q_w \\
    q_x q_y + q_z q_w	& \frac{1}{2} - {q_x}^2 - {q_z}^2 &	q_y q_z - q_x q_w \\
    q_x q_z - q_y q_w &	q_y q_z + q_x q_w	& \frac{1}{2} - {q_x}^2 - {q_y}^2 \\
    \end{bmatrix}$
    &
    $\begin{bmatrix}
      0 \\ 0 \\ 0
    \end{bmatrix}$
    \\
    \bottomrule
  \end{tabular}
\end{table}

Finally, a free joint allows free motion of its successor body with respect to its predecessor body.
It can be viewed as a combination of a spherical joint and 3 perpendicular prismatic joints.
Given a parameter set $q = \{ q_w, q_x, q_y, q_z, t_x, t_y, t_z\}$, we get the following table.

\begin{tabular}{cccc}
  \toprule
  Joint type & $S$ & $Rotation$ & $translation$ \\
  \midrule
  Spherical
  &
  $\mathbf{1}_6$
  &
  $2 \begin{bmatrix}
    \frac{1}{2} - {q_y}^2 - {q_z}^2 &	q_x q_y - q_z q_w &	q_x q_z + q_y q_w \\
    q_x q_y + q_z q_w	& \frac{1}{2} - {q_x}^2 - {q_z}^2 &	q_y q_z - q_x q_w \\
    q_x q_z - q_y q_w &	q_y q_z + q_x q_w	& \frac{1}{2} - {q_x}^2 - {q_y}^2 \\
  \end{bmatrix}$
  &
  ${\bf R}^{-1}\begin{bmatrix}
    t_x \\ t_y \\ t_z
  \end{bmatrix}$
  \\
  \bottomrule
\end{tabular}

\section{Jacobian computation}
\label{sec:jacobian_computation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                  SUBSECTION JACOBIAN COMPUTATION                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In order to solve our problem with a gradient-based nonlinear optimization algorithm, it is useful to compute the derivatives of every function used as constraint or cost with respect to any variable of the problem.
The transformations ${}^i X_0$ are used in many functions, therefore, having an efficient algorithm to compute their derivatives and the derivatives of any transformation defined in $B_i$ is necessary.

Given a static transformation ${}^p X_i$ defined in body $B_i$.
Its expression in the world frame is ${}^p X_0 = {}^p X_i\ {}^i X_0$ and its expression in the frame of $B_j$ is ${}^p X_j = {}^p X_i\ {}^i X_0\ {{}^j X_i}^{-1}$.

We denote $\text{Jac}_i$ the Jacobian of body $i$, and $\text{Jac}_i(X)$ the jacobian of the frame defined by $X$ in the referential of body $i$.
$q_i$ is the part of $q$ that corresponds to the degrees of freedom of joint $J_i$.
We denote $\text{Jac}_i.\text{cols}(j)$ the columns of $\text{Jac}_i$ associated with joint $J_j$.
The jacobian of the frame defined by ${}^p X_i$ in $B_i$ with respect to $q_j$ is given by
\begin{equation}
\label{partial_jacobian}
  \text{Jac}_i({}^p X_i).\text{cols}(j) = {}^p X_j\ S_i
\end{equation}

The complete Jacobian of a body $\text{Jac}_i$ is a $6\times \text{dof}$ matrix that can be computed by using the formula~\ref{partial_jacobian} on every index $j$ in $\kappa(i)$ and filling the rest of $\text{Jac}_i$ with zeros.

The algorithm that we use to compute $\text{Jac}_i({}{}^p X_i)$ writes as follows:

\begin{algorithm}
  \caption{Jacobian Computation}
\label{alg:jacobian_computation}
\begin{algorithmic}
  \State{$\text{Jac}_i({}^p X_i) = {\bf 0}_{6\times\text{dof}}$}
  \State{${}^p X_0 = {}^p X_i\ {({}^i X_0)}^{-1}$}
  \For{$j = 0:\text{size}(\kappa(i))$}
  \State{$k \leftarrow \kappa(j)$}
  \State{${}^p X_k = {}^p X_0\ {({}^k X_0)}^{-1}$}
  \State{$\text{Jac}_i({}^p X_i).\text{cols}(k) = {}^p X_k\ S_k$}
  \EndFor{}
\end{algorithmic}
\end{algorithm}

We write the jacobian of each body at its origin as follows:
\begin{equation}
  \mathbf{Jac}^0_i =
  \begin{bmatrix}
    \frac{\partial {}^i\mathbf{R}_0}{\partial q_0} & \cdots &
    \frac{\partial {}^i\mathbf{R}_0}{\partial q_j} & \cdots &
    \frac{\partial {}^i\mathbf{R}_0}{\partial q_{dof}} \\
    \frac{\partial {}^i\mathbf{t}_0}{\partial q_0} & \cdots &
    \frac{\partial {}^i\mathbf{t}_0}{\partial q_j} & \cdots &
    \frac{\partial {}^i\mathbf{t}_0}{\partial q_{dof}}
  \end{bmatrix}
=
  \begin{bmatrix}
    \omega_{i,0} & \cdots &
    \omega_{i,j} & \cdots &
    \omega_{i,dof} \\
    v_{i,0} & \cdots &
    v_{i,j} & \cdots &
    v_{i,dof}
  \end{bmatrix}
\end{equation}

%\section{Geometric constraints}
%\label{sec:geometric_constraints}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%          SECTION GEOMETRIC CONSTRAINTS                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Joint Limits}
\label{sec:joint_limits}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                      SUBSECTION JOINT LIMITS                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Most robotic joints have limits which define the range of value that can be accessed by the joint variables.
The joint limits for 1D joints like revolute and prismatic joints are trivial to formulate: We denote $q^-$ and $q^+$ the lower and upper values accessible and add a boundary constraint to the optimization problem:
\begin{equation}
\label{eq:joint_limits}
  \boxed{q^- \leq q \leq q^+}
\end{equation}

Most joints are easy to limit because their variables are independent.
Limiting the movements of a spherical joint, and by extension, of a free joint, is more complicated.
In humanoid robotics, spherical joints can be used to model the shoulder or hip joint of the robot.
A common approach to limit shoulder joint, inspired from the biomechanics field, considers the spherical motion (or swing) and the axial motion (or twist) separately as shown in~\Figref{fig:ballAndSocket}.

\begin{figure}[htb]
  \centering
  \includegraphics[width=0.7\linewidth]{ballAndSocket.pdf}
  \caption{Swing and Twist in ball and socket joint}
\label{fig:ballAndSocket}
\end{figure}

The spherical motion can be parametrized by a vector of the 3D unit sphere $S^2$ and constrained to lay within a limit cone, the axial motion can be parametrized in $\mathbb{R}$ and limited by equation~\Eqref{eq:joint_limits}.
This type of formulation is presented in~\cite{baerlocher}.

\section{Contact constraints}
\label{sec:contact_constraints}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                   SUBSECTION CONTACT CONSTRAINTS                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Most of the tasks the robots achieve (grasping, manipulation, walking, etc.) are made by making and breaking contacts.
A contact can be defined between 2 surfaces: one on the robot and the other on the entity (that can be another robot, object or the environment) to contact with.
The most usual types of contact constraint encountered are the planar contact and the fixed contact.
A planar contact constraint is used when a planar surface of a robot is put in contact with a planar surface of the environment.
We denote $F_1 = \{O_1, \vec{x_1}, \vec{y_1}, \vec{z_1}\}$ a frame defined on $S_1$, the surface of the first body involved in the contact, such that the 3D point $O_1$ is on $S_1$ and the vector $\vec{z_1}$ is normal to $S_1$ and pointing toward the inside of the body.
$F2 = \{O_2, \vec{x_2}, \vec{y_2}, \vec{z_2}\}$ is a frame on $S_2$, the surface of the second body involved, such that $O_2$ is on $S_2$ and the vector $\vec{z_2}$ is normal to $S_2$ and pointing away from the body.

Constraining $S_1$ and $S_2$ to be coplanar boils down to aligning $\vec{z_1}$ with $\vec{z_2}$ and to ensure that the projection of the distance between $O_1$ and $O_2$ along $\vec{z_1}$ is null.
Note that we avoid using the dot product of two vectors that are meant to be aligned e.g. $\vec{z_1}\cdot\vec{z_2} = 1$ because when that constraint is satisfied, its gradient is zero, which implies that in the optimization context it is unqualified.
That is why we prefer imposing orthogonality constraints.
This translates into adding the following set of constraints to our problem:

\begin{equation}
\label{eq:coplanarity}
\boxed{\left\{
  \begin{array}{l}
    \overrightarrow{O_1O_2} \cdot \vec{z_1} = 0\\
    \vec{x_1}\cdot\vec{z_2} = 0\\
    \vec{y_1}\cdot\vec{z_2} = 0\\
    \vec{z_1}\cdot\vec{z_2} \geq 0
  \end{array}
  \right.}
\end{equation}

This set of constraints leaves free the displacements of $F_2$ along $\vec{x_1}$ and $\vec{y_1}$ as well as its rotation around $\vec{z_1}$.
We call this a floating planar contact, the optimization algorithm will be able to choose the location of $F_2$ in the plane $\{O_1, \vec{x_1}, \vec{y_1}\}$.
This contact has 3 degrees of freedom.

\begin{figure}[htpb]
  \centering
  \includegraphics[width=0.8\linewidth]{contactConstraint.pdf}
  \caption{Floating and Fixed Contacts}
\label{fig:contactConstraint}
\end{figure}

If we constrain the location of $F_2$ in $\{O_1, \vec{x_1}, \vec{y_1}\}$ such that $O_1$ and $O_2$ are superimposed and $\vec{x_1}$, $\vec{y_1}$, $\vec{z_1}$ are aligned with respectively $\vec{x_2}$, $\vec{y_2}$, $\vec{z_2}$, we obtain a fixed contact with zero degrees of freedom.
This translates into adding the following set of constraints to our problem:

\begin{equation}
\label{eq:fixed_contact}
\boxed{\left\{
  \begin{array}{l}
    \overrightarrow{O_1O_2} = \vec{0}\\
    \vec{x_1}\cdot\vec{z_2} = 0\\
    \vec{y_1}\cdot\vec{z_2} = 0\\
    \vec{x_1}\cdot\vec{y_2} = 0\\
    \vec{z_1}\cdot\vec{z_2} \geq 0\\
    \vec{x_1}\cdot\vec{x_2} \geq 0\\
  \end{array}
  \right.}
\end{equation}

We illustrate those two types of contacts in~\Figref{fig:contactConstraint}

\section{Collision avoidance}
\label{sec:collision_avoidance}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                   SUBSECTION COLLISION AVOIDANCE                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In order to avoid unwanted collisions between bodies of robots, for two bodies $B_1$ and $B_2$, we want to define a continuously differentiable function $d_{\{B_1, B_2\}}(q)$ that has the properties of a pseudo-distance:
\begin{itemize}
  \item $d_{\{B_1, B_2\}}(q) > 0$ when the bodies are not touching each other
  \item $d_{\{B_1, B_2\}}(q) = 0$ when the bodies are in collision without interpenetration
  \item $d_{\{B_1, B_2\}}(q) < 0$ when the bodies are in collision with interpenetration
\end{itemize}

Using the cartesian distance between the exact surfaces of $B_1$ and $B_2$ might result in a discontinuous gradient of $d_{\{B_1, B_2\}}$ if the surfaces of $B_1$ and $B_2$ are not convex.
A conservative approach is to associate to each body, a strictly convex bounding volume and to compute the distance between those volumes.
\cite{escande:humanoids:2007} and~\cite{escande:itro:2014} proposes a method to generate a strictly convex Sphere-Torus-Patch Bounding Volumes (STP-BV) that guarantees the gradient continuity of the proximity distance.
The distance between the STP-BV of $B_1$ and $B_2$ computed by an enhanced GJK~\cite{gilbert-1988a} collision-detection algorithm is a continuously differentiable pseudo-distance.
Thus, we can use this function in our optimization algorithm to ensure that the distance between the bodies is greater than a safety distance $\epsilon_{12}$:

\begin{equation}
  \boxed{d_{\{B_1, B_2\}}(q) \geq \epsilon_{12}}
\end{equation}

This function can be used to avoid collisions between a robot and the environment as well as auto collisions between bodies of the same robot.
We denote $Coll$ the list of triplets $\{B_i, B_j, \epsilon_{ij}\}$ defining each collision that we want to avoid.

Then the set of constraints to add to our problem is:

\begin{equation}
  \boxed{\forall \{B_i, B_j, \epsilon_{ij}\} \in Coll,\ d_{\{B_i, B_j\}}(q) \geq \epsilon_{ij}}
\end{equation}

In many cases, it is possible to avoid the collision between two bodies of a robot by modifying the joint limits and reducing them to a span where the collision of interest cannot happen.
That approach is conservative and ad-hoc but can save some precious computation time.

%\section{Static constraints}
%\label{sec:static_constraints}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                SECTION STATIC CONSTRAINTS                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{External Forces}
\label{sec:external_forces}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     SUBSECTION EXTERNAL FORCES                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

For a robot to contact with the real world (or another robot), its geometric description is not enough.
The robot is subject to forces applied on its bodies as a reaction to that it applies, and which can be generated by contacts with the environment or with another actor (human, another robot, manipulated object...), by the effect of physical forces fields like gravitation or magnetism, or by contacts between two bodies of the robot.
Our posture generator must take those `External forces' into account, to be able to estimate the stability of the robot and compute the internal torques generated in the joints, or to be able to generate desired force-driven posture for given tasks when needed.

An external force applied on a rigid body can be represented by a \emph{wrench}, from screw theory, and is composed of a resultant part $f$ (sometimes called force) and a moment part (sometimes called couple).
Let $w$ be a wrench, $w|_F^O$ is the expression of $w$ calculated at the point $O$ expressed in the frame $F$.
We denote $\vec{f}$ the resultant part of $w$, and $\vec{f}|_F$ the expression of $\vec{f}$ in $F$.
$\vec{m}$ is the moment part of $w$ and $\vec{m}|_F^O$ the expression of $\vec{m}$ in $F$ calculated at the point $O$.

\begin{equation}
  w|_F^O = \left\{ \begin{array}{r}
    \vec{m}\\
    \vec{f}\\
  \end{array} \right\}^O_F
  = \left\{ \begin{array}{r}
    \vec{m}|_F^O\\
    \vec{f}|_F\\
  \end{array}\right\}
\end{equation}

The expression of the moment part on a different point $P$ is given by the Varignon formula:

\begin{equation}
  \vec{m}|_F^P = \vec{m}|_F^O + \overrightarrow{PO} \wedge \vec{f}|_F
\end{equation}

The resultant part is invariant with respect to the point at which the wrench is calculated.

We drop the frame subscript when the choice of the frame does not matter and all quantities are computed in the same frame.

\section{Static stability}
\label{sec:static_stability}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                    SUBSECTION STATIC STABILITY                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

We denote $g$ the acceleration of gravity on earth $g = 9.81 m.s^{-2}$.
The wrench associated with the action of gravity on a body of mass $M$ which center of mass is denoted $G$ with $\vec{z}$ the upward vertical vector in the world frame $F_0$ is:
\begin{equation}
  w_g|^G_{F_0} = \left\{ \begin{array}{r}
     \vec{0} \\
     -Mg\vec{z} \\
 \end{array}\right\}^G_{F_0}
\end{equation}

A solid is statically stable if it satisfies the Euler-Newton Equation.
We consider a robot on which $m$ external wrench $w_i = \left\{ \begin{array}{r}
    \vec{m_i}\\
    \vec{f_i}\\
\end{array} \right\}^{P_i}$ are applied.
We denote $P$ the application point at which the equation and all its terms are calculated:
\begin{equation}
  \sum\limits_i w_i|^P + w_g|^P = 0
\end{equation}
which is equivalent to:
\begin{equation}
\left\{
\begin{array}{r}
  \sum\limits_i \vec{m_i}|^P + \overrightarrow{GP}\wedge Mg\vec{z} = 0 \\
  \sum\limits_i \vec{f_i} - Mg\vec{z} = 0 \\
\end{array}
\right.
\end{equation}

This equation can be simplified by applying it at the center of mass of the body as:
\begin{equation}
  \left\{
  \begin{array}{r}
    \sum\limits_i \vec{m_i}|^G = 0 \\
    \sum\limits_i \vec{f_i} - Mg\vec{z} = 0 \\
  \end{array}
  \right.
\label{eq:stability}
\end{equation}

Satisfying equation~\Eqref{eq:stability} ensures the static equilibrium of a rigid body.
If the robot's actuators are powerful enough to maintain its posture under any external perturbation, namely, when they can generate infinite or at least large enough torques, then the robot can be approximated as a rigid body and satisfying equation~\Eqref{eq:stability} is enough to ensure its stability.
%In some cases, an articulated robot is considered as a rigid body and this equation alone can be used to ensure its stability.
%It is only valid if the robot can generate infinite torques in its articulations, or at least if we have some guarantee that the robot is able to generate large enough torques.
Otherwise, it is necessary to verify that the robot's actuators can generate large enough torques to maintain that posture.
The details concerning the torque computations are discussed in Section~\ref{sec:torque_limits}

Equation~\Eqref{eq:stability} can be used in an optimization problem.
We consider that each wrench $w_i$ applied on the system is defined by the position of its application point $P_i$ and the values $m_i$ and $f_i$ that represent the moment and resultant of $w_i$ at $P_i$.
$P_i$ depends on $q$ the joint parameter of the robot.
$m_i$ and $f_i$ are new variables that need to be added to the problem.
In summary, $w_i$ depends on $q$, $m_i$ and $f_i$.
We denote $f$ the concatenation of all the variables $m_i$ and $f_i$.

\begin{equation}
  \boxed{s(q,f) = \left\{
  \begin{array}{r}
    \sum\limits_i \vec{m_i} + \overrightarrow{P_i G}\wedge \vec{f_i} \\
    \sum\limits_i \vec{f_i} - Mg\vec{z} \\
  \end{array}
  \right\}
  = 0}
\end{equation}

The optimization problem~\Eqref{eq:optim_form_PG} becomes (we denote m the dimension of the force variables):

\begin{equation}
\label{eq:optim_form_PG_with_stab}
  \left\{
  \begin{array}{l}
    \min\limits_{q\in\mathcal{C}, f\in \mathbb{R}^m}{f(q)}\\
    \text{ s.t. }
    \left\{
    \begin{array}{l}
      s(q,f) = 0\\
      c_i(q) = 0,\ \forall i\in{E}\\
      c_i(q) \geq 0,\ \forall i\in{I}\\
    \end{array}
    \right.
  \end{array}
  \right.
\end{equation}

The derivation of the static stability constraint is straightforward.
All the terms of equation~\Eqref{eq:stability} are components of wrenches.
A wrench is completely defined by the frame in which it is expressed and its values of resultant and moment in that frame.
Deriving the stability condition boils down to deriving each term w.r.t its components values and w.r.t the transformation of its frame.

\begin{equation}
\left\{
\begin{array}{r}
  \partial\left(\sum\limits_i m_i|^G\right) = \sum\limits_i \partial(m_i|^G) \\
  \partial\left(\sum\limits_i f_i\right) = \sum\limits_i \partial(f_i) \\
\end{array}
\right.
\label{eq:derivation_stability}
\end{equation}

We will explicit a method to automatically compute those derivatives in a later chapter.

\section{Center of mass projection}
\label{sec:center_of_mass_projection}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                SUBSECTION CENTER OF MASS PROJECTION                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

When all the wrenches applied to the body are due to unilateral punctual contacts on the same horizontal plane $H = \{O, \vec{x}, \vec{y}\}$, the stability criterion~\Eqref{eq:stability} can be simplified.
The wrench $w_i$ generated by a unilateral punctual contact is a pure force resultant, its moment part is zero on the contact point.

\begin{equation*}
    \left. w_i \right|^{P_i} =
    \left\{
      \begin{array}{r}
      \vec{0}\\
      \vec{f_i}\\
  \end{array} \right\}^{P_i}
\end{equation*}

Equation~\Eqref{eq:stability} becomes:

\begin{equation}
\left\{
\begin{array}{r}
  \sum\limits_i \overrightarrow{OP_i}\wedge \vec{f_i} - \overrightarrow{OG} \wedge Mg\vec{z} = 0 \\
  \sum\limits_i \vec{f_i} - Mg\vec{z} = 0 \\
\end{array}
\right.
\end{equation}

We can write $\overrightarrow{OG} = \overrightarrow{OG_p} + z_G\vec{z}$ with $G_P$ the projection of $G$ on $H$. Replacing in the moment equation gives:

\begin{equation}
  \sum\limits_i \overrightarrow{OP_i}\wedge \vec{f_i} - \sum\limits_i\overrightarrow{OG_P} \wedge \vec{f_i} = 0
\label{eq:projCoM}
\end{equation}

With $f_i = f_i^x\vec{x} + f_i^y\vec{y} + f_i^z\vec{z}$, $G$ and $P_i$ can be written as $\overrightarrow{OG_P} = G_x \vec{x} + G_y\vec{y}$ and $\overrightarrow{OP_i} = P_{ix} \vec{x} + P_{iy} \vec{y}$. The two first lines of equation~\Eqref{eq:projCoM} give:

\begin{align}
\sum\limits_i \left\{\begin{array}{r} P_{iy}f_i^z\\-P_{ix}f_i^z\end{array}\right\}
= \left\{\begin{array}{r} G_{y}\\-G_{x}\end{array}\right\} \sum\limits_i f_i^z\\
  \overrightarrow{OG_P} = \frac{\sum\limits_i \overrightarrow{OP_i} f_i^z}{\sum\limits_i f_i^z}
\end{align}

Since all the contacts are unilateral, all the $f_i^z$ must be positive.
For any set of $f_i^z\geq0$, $G_P$ is a barycenter with positive coefficients of the $P_i$.
Any point $G_P$ that is included in the convex hull of all the $P_i$ is a solution.

Thus, we have the property: a rigid body that has all its contacts with the environment being punctual, unilateral and all lying on the same horizontal plane $H$, is said stable if and only if the projection of its center of mass on $H$ is inside the convex hull of all its contact points.

\section{Torque limits}
\label{sec:torque_limits}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                      SUBSECTION TORQUE LIMITS                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In general, satisfying equation~\Eqref{eq:stability} is not enough to ensure that a robot can be statically stable.
The joint torques that are required to hold the posture must be within the physical capabilities of the robot, namely, its torque limits.
We denote $\tau_i^-$ and $\tau_i^+$ the minimal and maximal torques that can be generated by the robot's actuators on joint $J_i$.
In some cases, the torque limits can depend on the joint parameters $\tau_i^-(q)$ and $\tau_i^+(q)$.
For example, it is the case with the Atlas robot that is hydraulically actuated.

Featherstone~\cite{featherstone:book:2007} proposes a recursive algorithm to compute the torques, accelerations, and velocities generated in a multi-articulated system by a set of external forces called the Inverse Dynamics Algorithm.
For the purpose of generating statically stable postures, the velocities and accelerations are not needed.
Thus, we devise a specialized algorithm to fit our needs and call it the Inverse Static Algorithm.

We denote $a_g$ the gravity acceleration vector with $\vec{a_c}$ its rotation part and $\vec{a_f}$ its translation part:
\begin{equation}
  a_g = \left\{ \begin{array}{r}
    \vec{a_c} \\
    \vec{a_f} \\
  \end{array} \right\}
  = \left\{ \begin{array}{r}
    \vec{0} \\
    g\vec{z}\\
  \end{array} \right\}
\end{equation}

The algorithm first computes the generalized forces $f^G_i$ applied to each body.
It is the sum of the action of gravity and of the external forces applied on a body calculated at the origin of the world frame, expressed in the world frame.
Then, the generalized forces are used to compute the torques.
We denote $\mathbf{I}_i$ the inertia matrix of body $i$.

\begin{algorithm}
  \caption{Inverse Static Algorithm}
\label{alg:IS}
\begin{algorithmic}
  \For{$i = 0:n_B$}
  \State$f^G_i = \mathbf{I}_i {}^i\mathbf{X}_0 a_g - {{}^i\mathbf{X}_0}^*f_i^{ext}$
  \EndFor{}
  \For{$i = n_J-1:0$}
  \State$\tau_i = {f^G_i}^T S_i$
  \If{$pred(i) \neq -1$}
  \State$f^G_{pred(i)} += {\mathbf{X}^{PtS}_i(\mathbf{q})}^{-*} f^G_i$
  \EndIf{}
  \EndFor{}
\end{algorithmic}
\end{algorithm}

We can write the torques as a function of the joint parameters and the external forces $\tau(\mathbf{q},\mathbf{f})$.
The torque limit constraint writes as:
\begin{equation}
  \boxed{\tau^- \leq \tau(\mathbf{q},\mathbf{f}) \leq \tau^+}
\end{equation}

We will detail the derivation of this constraint in Section~\ref{sec:torque_derivation}.

\section{Contact Forces and Friction Cones}
\label{sec:contact_forces_and_friction_cones}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%            SUBSECTION CONTACT FORCES AND FRICTION CONES             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Here we use the same frames and notations as introduced in Section~\ref{sec:contact_constraints}.

The contacts involved in a posture generation problem can be separated into two types:
\begin{itemize}
  \item Geometric Contacts, and
  \item Stability Contacts.
\end{itemize}
The Geometric Contact is a contact where the position of contact is reached with theoretically zero force, i.e. that contact does not support any forces (this is a necessary step to generate a sequence of quasi-static transitions).
Physically, that correspond to the transition state between a configuration without contact and a configuration with contact on which forces are applied.
We defined the Geometric Contacts in Section~\ref{sec:contact_constraints}.
The Stability Contact is a Geometric Contact that bears contact forces.

%In planning, contacts are added or removed one by one between successive postures.
%In order to add or remove a stability contact, it is necessary to go through a geometric contact (that guarantees the continuity on contact forces).
%First a geometric contact configuration is found, then on the next posture, that geometric contact is fixed ond forces are added on it, it becomes a stability contact.
%Similarly, to remove a stability contact, we first look for a posture with a fixed geometric contact in place of the stability contact to remove.
%And on the next posture, the contact can be removed.

We denote $w_{1\rightarrow 2}$ the force applied by body $B_1$ on body $B_2$.
Then the force applied by $B_2$ on $B_1$ is $w_{2\rightarrow 1} = -w_{1\rightarrow 2}$.
%These interaction forces must be taken into account in the stability and torque computations of each robot involved in the contact.

The interaction force resulting from a punctual contact (see~\Figref{fig:frictionCone}) on a point $P$ can be modeled as a pure force resultant along the $z_1$ direction $\vec{f_n} = f_z \vec{z_1}$ and the tangential efforts due to the friction in that contact can be modeled as $\vec{f_t} = f_x \vec{x_1} + f_y \vec{y_1}$.
%A punctual contact cannot carry moments on its contact point.

\begin{equation}
\label{eq:punctual_force}
\left. w_{2\rightarrow 1}\right|^P = \left\{
  \begin{array}{l}
    \vec{0} \\
    \overrightarrow{f_{2\rightarrow 1}} = \vec{f_t} + \vec{f_n} \\
  \end{array}
  \right\}^P
\end{equation}

\begin{figure}[htpb]
  \centering
  \includegraphics[width=0.6\linewidth]{frictionCone.pdf}
  \caption{Punctual unilateral stability contact with friction}
\label{fig:frictionCone}
\end{figure}

This formulation of the interaction force defines a bilateral contact, in the sense that the force can be in any direction, $B_1$ can push as well as pull on $B_2$.

To model a unilateral contact, we must constrain the normal component of $f_{2\rightarrow 1}$ to be oriented toward the inside of $B_1$.
This means that only pushing actions can be generated, not pulling ones.
This translates into:
\begin{equation}
  \overrightarrow{f_{2\rightarrow 1}}\cdot \vec{z_1} = f_z \geq 0
\end{equation}

Furthermore, to avoid slippage, the Coulomb friction law must be respected for each contact force $\vec{f}$.
%This law states that the contact force resultant must lay inside a friction cone of angle $\mu$, the friction coefficient.
Which translates into the following equation, with $\vec{f_n}$ and $f_t$, respectively the normal and tangential parts of $\vec{f}$ and $\mu$, the friction coefficient:

\begin{equation}
  \mu\|\vec{f_n}\| \geq \|\vec{f_t}\|
\end{equation}

Given the decomposition of $f_{2\rightarrow 1}$ in $F_1$, $f_{2\rightarrow 1} = f_x \vec{x_1} + f_y \vec{y_1} + f_z \vec{z_1}$, for any punctual contact in a posture generation problem, we can add the following set of constraint to our optimization problem:
\begin{equation}
  \label{eq:unilateralContact}
  \left\{
  \begin{array}{l}
    f_z \geq 0 \\
    \mu^2 f_z^2 - f_x^2 +f_y^2 \geq 0
  \end{array}
  \right.
\end{equation}

When it comes to planar contacts on surface $S$ with $\vec{n}$ the outbound normal to $S$, the interaction force can have components of forces and moments in all directions.
The forces components intrinsic to the planar contact model are a resultant part aligned with $\vec{n}$ $\vec{f_n} = f_z \vec{z_1}$ and a moment part tangential to $S$: $\vec{m_t} = m_x \vec{x_1} + m_y \vec{y_1}$.
The forces due to friction are a tangential friction resultant part $\vec{f_t} = f_x \vec{x_1} + f_y \vec{y_1}$ and a normal friction moment $\vec{m_n} = m_z \vec{z_1}$.

This type of force can be modeled by a set of unilateral punctual efforts applied on each vertex of a polygon describing the contact area.
And ensuring that each of them lay in their respective friction cone, thus satisfying the equation~\Eqref{eq:unilateralContact}.
As depicted in~\Figref{fig:planarContact}

\begin{figure}[htpb]
  \centering
  \setlength{\fboxsep}{0pt}%
  \setlength{\fboxrule}{1pt}%
  \fbox{\includegraphics[width=0.6\linewidth]{planarContact.pdf}}
  \caption{Modeling of a planar contact between the foot on HRP-4 and the ground.}
\label{fig:planarContact}
\end{figure}

\section{Cost Functions}
\label{sec:cost_functions}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                       SECTION COST FUNCTIONS                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In addition to constraints, it is often useful to add a cost function to our optimization problem.
The submanifold of feasible configurations $\mathcal{C}_F$ can contain an infinity of solutions and even some continuous solution areas in which all points are solutions.
The cost function helps to choose the `best' candidate solution.
Various types of cost functions can be chosen, for example, we can minimize the distance to a reference posture $q_R$:

\begin{equation*}
  f_\text{posture}(\mathbf{q}) = {\|\mathbf{q}-q_R\|}^2
\end{equation*}

The effect of that type of cost function is illustrated in~\Figref{fig:cost}.
On both images, the HRP-2Kai robot is stable, respects its joints and torques limits.
The only difference is that the right one minimizes the distance to a reference posture (standing straight with bent knees) while the left result does not use a cost function.

\begin{figure}[htpb]
  \centering
  \setlength{\fboxsep}{0pt}%
  \setlength{\fboxrule}{1pt}%
  \fbox{\includegraphics[width=0.5\linewidth]{cost.pdf}}
  \caption{Effect of cost function. Left: no cost function. Right: Distance to a half-sitting predefined posture.}
\label{fig:cost}
\end{figure}

One can also want to minimize the sum of norms of the contact forces:
\begin{equation*}
  f_\text{forces}(\mathbf{f}) = \sum\limits_i {\|f_i(\mathbf{f})\|}^2
\end{equation*}
or the torques in the robot's joints:
\begin{equation*}
  f_\text{torques}(\mathbf{q},\mathbf{f}) = \sum\limits_i {\|\tau_i(\mathbf{q},\mathbf{f})\|}^2
\end{equation*}
Some more custom cost functions can also be considered, for example, we may want a point $P_i$ on body $B_i$ with to be as far as possible in a direction $\vec{d}$
\begin{equation*}
  f_\text{point} (\mathbf{q}) = -{\overrightarrow{O_0 P_i}}\cdot{\vec{d}}
\end{equation*}

Any positively weighted combination of cost function can be used, in which case it is important to choose the weights $p_i$ carefully to scale all the costs so that they all can influence the result and none is completely dominated by another.
\begin{equation}
  f_\text{cost}(\mathbf{q},\mathbf{f}) = \sum\limits_i{p_i f_i(\mathbf{q},\mathbf{f})} = p_0 f_\text{posture}(\mathbf{q}) + p_1 f_\text{forces}(\mathbf{f}) + p_2 f_\text{torques}(\mathbf{q},\mathbf{f}) + \cdots
\end{equation}

\section{Conclusion}
\label{sec:Ch1_Conclusion}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                         SECTION CONCLUSION                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In this Chapter, we have seen how to formulate a robotics problem with several different tasks and objectives as an optimization problem.

We denote $\mathcal{T}_i$ the additional tasks added to the problem, which is described by the set of equalities $g_i(\mathbf{q},\mathbf{f}) = 0$ and inequalities $h_i(\mathbf{q},\mathbf{f}) \geq 0$.
The contact tasks are included in those and the equations describing them must encompass the geometric contact constraint equation like~\Eqref{eq:coplanarity} as well as the unilateral and friction constraints equations~\Eqref{eq:unilateralContact} in the case of a unilateral contact.

A typical robotics problem can be written as a combination of all those costs and constraints:
\begin{align}
\minimize_{\mathbf{q}, \mathbf{f}} & \quad f_\text{cost}(\mathbf{q},\mathbf{f}) \nonumber\\
\text{s.t.}&
\left\{
\begin{array}{lr}
q^- \le \mathbf{q} \le q^+\\
s(\mathbf{q},\mathbf{f}) = 0 \\
\tau^- \le \tau(\mathbf{f},\mathbf{q}) \le \tau^+\\
\forall \{B_i, B_j, \epsilon_{ij}\} \in Coll,\ d_{\{B_i, B_j\}}(\mathbf{q}) > \epsilon_{ij}\\
g_i(\mathbf{q},\mathbf{f}) = 0\ \ \forall\mathcal{T}_i,\\
h_i(\mathbf{q},\mathbf{f}) \geq 0\ \ \forall\mathcal{T}_i.
\end{array}\right.
\label{eq:PG}
\end{align}

In the following chapter, we present an extension to the contact constraint formulation that allows generating non-inclusive contacts, an algorithm to compute the exact derivatives of the torques in robot's joints, and our endeavor to apply a different optimization approach to solving posture generation problems.


\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{multicol}
\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\textwidth = 6.5 in
\textheight = 9 in
\oddsidemargin = 0.0 in
\evensidemargin = 0.0 in
\topmargin = 0.0 in
\headheight = 0.0 in
\headsep = 0.0 in
\parskip = 0.2in
\parindent = 0.0in

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}{Definition}

\title{{\tt kuramoto\underline{ }1d.m} - Kuramoto-Sivashinsky Solvers}
\author{Jeff Borggaard}
\begin{document}
\maketitle

\section*{Summary}
This software (and the similar {\tt kuramoto\underline{ }1da.m}) solves the
Kuramoto-Sivashinsky equation using $C^1$ Hermite cubic elements in a
periodic domain.

\section*{The Equations}
There are two popular forms of the Kuramoto-Sivashinsky equations.  The first
mimics the form originally derived by Kuramoto \cite{kuramoto}.
%
\begin{equation}
\label{eq:ks1}
 d
\end{equation}

The second has the form of the derivative of equation (\ref{eq:ks1}), namely
%
\begin{displaymath}
  w_t + w_{xx} + w_{xxxx} + 2 w w_x = 0.
\end{displaymath}
%
Note that if we scale the spatial variable so that the computational
domain occurs over the unit interval: $\bar{x} = x/L$, and define
$\epsilon = 1/L^2$, then we have the following version of the Kuramoto-Sivashinsky equation
%
\begin{equation}
\label{eq:ks2}
  w_t + \epsilon w_{\bar{x}\bar{x}} + \epsilon^2 w_{\bar{x}\bar{x}\bar{x}\bar{x}} + 2 \epsilon w w_{\bar{x}} = 0
\end{equation}
%
with $t>0$, $\bar{x} \in (0,1)$, $w(0,t) = w(1,t)$ and $w_{\bar{x}}(0,t) =
w_{\bar{x}}(1,t)$.  The initial condition is scaled so that
$w(\bar{x},0) = w_0(\bar{x})$.

A nice property of solutions to equation (\ref{eq:ks2}) is that the integral
%
\begin{equation}
\label{eq:w_ave}
  \int_0^1 w(\bar{x},t) \ d\bar{x}
\end{equation}
%
is independent of time.  In other words, the average of the solutions is
determined from the average of the initial conditions.  This imposes a
great deal of structure on the solutions as will be seen in the numerical
tests.

\section*{Parameters}
Input parameters
%
\begin{itemize}
  \item {\tt epsilon} - \newline
  This parameter has the effect of changing the length of the periodic
  domain.  See the scaling of the equations above.
\end{itemize}

Internal parameters
%
\begin{itemize}
  \item {\tt n\underline{ }gauss} - \newline
  The number of Gauss point used in element integration
  \item {\tt n\underline{ }nodes} - \newline
  The number of nodes (including the shared endpoints) used in the
  spatial discretization.  The number of Hermite elements is equal to
  {\tt n\underline{ }nodes-1}.
  \item {\tt t\underline{ }initial}, {\tt t\underline{ }step},
  {\tt t\underline{ }save}, and {\tt t\underline{ }final} - \newline
  Time integration parameters.  The solution is saved at multiples of
  {\tt t\underline{ }save}, so {\tt t\underline{ }save}/{\tt t\underline{ }step}
  should be a positive integer.
  \item {\tt resid\underline{ }tol} - \newline
  Residual tolerance for Newton solve at each timestep.
  \item {\tt step\underline{ }tol} - \newline
  Step tolerance for Newton solve at each timestep.
  \item {\tt max\underline{ }iterations} - \newline
  Number of Newton iterations attempted to achieve one of the convergence
  tolerances in each timestep.
\end{itemize}

\section*{Dynamical Systems Analysis}

\begin{table}
\begin{center}
\begin{tabular}{lll}
\hline
length $L$ & \multicolumn{1}{c}{$\epsilon$} & solution behavior \\
\hline
12.5664  &  0.00633257  & bifurcation \\
12.8767  &  0.00603102  & heteroclinic bifurcation \\
13.1403  &  0.00579148  & hopf bifurcation \\
402.2590 &  0.00000618  & ``chaos'' \\
\hline
\end{tabular}
\end{center}
\end{table}

\end{document}


    \filetitle{srf}{Shock (impulse) response function}{SVAR/srf}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
[Resp,Cum] = srf(V,NPer)
[Resp,Cum] = srf(V,Range)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{V} {[} SVAR {]} - SVAR object for which the impulse response
  function will be computed.
\item
  \texttt{NPer} {[} numeric {]} - Number of periods.
\item
  \texttt{Range} {[} numeric {]} - Date range.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\item
  \texttt{Resp} {[} tseries \textbar{} struct {]} - Shock response
  functions.
\item
  \texttt{Cum} {[} tseries \textbar{} struct {]} - Cumulative shock
  response functions.
\end{itemize}

\paragraph{Options}\label{options}

\begin{itemize}
\item
  \texttt{'presample='} {[} \texttt{true} \textbar{}
  \emph{\texttt{false}} {]} - Include zeros for pre-sample initial
  conditions in the output data.
\item
  \texttt{'select='} {[} cellstr \textbar{} char \textbar{} logical
  \textbar{} numeric \textbar{} \emph{\texttt{Inf}} {]} - Selection of
  shocks to which the responses will be simulated.
\end{itemize}

\paragraph{Description}\label{description}

\paragraph{Example}\label{example}


\subsection{Blind auctions}

Blind auctions are sealed-bid auctions. Participants cannot view other bids.

The price paid is the highest bid.


\subsection{Benzerlik Teoremleri}

\subimport{./FTOS/}{ftos.tex}
\newpage
\subimport{./SegmentTheorems/}{segmenttheorems.tex}
\newpage
\subimport{./SegmentTheorems/}{thales.tex}
\newpage
\subimport{./SegmentTheorems/}{essentialratiotheorem.tex}
\newpage
\subimport{./SegmentTheorems/}{butterfly.tex}
\newpage
\subimport{./SegmentTheorems/}{parelels.tex}
\newpage


\section*{Results}
While the prospect of private property drives cooperation,\footnote{I don't know how this assertion finds an operationalization in the model $\rightarrow$ maybe rephrase or even drop} cooperation is itself sensitive to property violation. While small quantities of property violation introduce some amount of randomness that enhances cooperation (see Figure \ref{fig:phase_transition}a), we focus on the limit amount of property violation beyond which cooperation transitions to defection.\footnote{as said above the overall rational is that private property enforcement is costly for society, and hence there is an incentive to find the maximum bearable level of private property.}\\

Our model operationalizes the action of property violation as a uniform random variable [$p(S \geqslant s) = 1$], with sure strategy update ($r=0$),\footnote{for strategy update we resort to the update method proposed by Helbing and Yu \cite{helbing2009outbreak}, which in our noiseless case is equivalent to the Fermi update with Fermi temperature equal to 1.} no random strategy reset ($q=0$) and no random migration to an empty site ($m=0$). And as players choose their best shot in the success-driven migration step, we can say that they are fully rational agents (in their migration range), which choice to expel another individual results only from odds of property enforcement.\footnote{These parameters settings ensure the simplest possible model that guaranties that the observed stylized facts are genuine to the property violation phenomenon.} \\

\subsection*{Population density, Migration Range and Cooperation Level}
The outcome of the property violation game is nevertheless constrained by the initial conditions, such as population density and migration range: Increased population density reduces the opportunities to find an empty location, and thus, mechanically increases the odds of property violation. The migration range on the one hand increases the chances to find an empty sweet spot in case of success-driven  or forced migrations. On the other hand, it also increases opportunities for property violators. Migration influences the capability by defectors to penetrate clusters of cooperators. However, expelled cooperators can move far enough to make the cluster move away from the defector. Hence, larger migration ranges increase tolerance to property violation (see Figure \ref{fig:phase_transition}b), but their beneficial effects are limited for highly densely populations. On the contrary, populations with very short migration ranges -- here, embodied by unitary migration distance ($M=1$) -- can hardly stand any property violation.

%Nevertheless, the intensity of property violation is determinant in the ability by defectors to break clusters of cooperators %(see Figure \ref{fig:viz} and online visualization tool).

\subsection*{Optimizing for Property Violation is Managing at the Edge of Collapse}
Property enforcement is costly, and thus, there is an incentive to limit enforcement to the strict minimum, which will ensure high cooperation (which is equivalent to decide how much property crime a society can stand). For large enough migration ranges ($M\geqslant 3$), a little property violation ($s < 1 \%$), brings the necessary randomness that reduces friction and enhances cooperation, similarly to previous results obtained with other types of noise involved in success-driven migration games \cite{helbing2009outbreak}. For larger property violation $s > 1\%$, cooperation decreases linearly according to $ c = 1 - 2.2\cdot s$, until a critical phase transition is reached (see Figure \ref{fig:phase_transition}a), which itself depends on the population density and the migration range as reported on Figure \ref{fig:phase_transition}b. There is no hard limit, but rather a transition from sure striving society with high cooperation to a sure collapse. The region in between is large compared to the absolute value of the upper limit: For instance, for $M=5$ and $d=0.5$, the phase transition spans from $s = 3.7\%$ to $s= 4.2\%$, with an equal chance to survive or collapse for property violation $s=4\%$. Hence, the ``danger zone" spans over more than 10\% of the limit value, and the decreasing probability of striving in the critical phase transition region, makes it very hard to determine whether the course of a society will end up positively or negatively.\footnote{We have verified that the outcome is independent from the initial grid organization, by running multiple simulations from the same initial grid.}

\subsection*{Decoupling for Sustainable Cooperation}
The sudden phase transition from high cooperation to collapse suggests a ``grain of sand" factor deciding whether a society operating at the limit level of property violation will strive or collapse.\footnote{Again, it's important to stress that it is natural for a society to operate close to the limit, since private property enforcement is costly, and even sometimes socially undesired.}\\

To better determine this ``grain of sand", we resort to simple of economics implied by the property game and opportunities evolve over time: To maximize their payoff, individuals are endowed with three basic rules,

\begin{itemize}
  \item {\bf strategy update rule:} the player updates its prisoner's dilemma strategy (i.e., cooperate or defect), as a function of her neighbors strategies,
  \item {\bf success driven migration:} the player migrates to the best empty site within her migration area determined by the Moore distance $M$,
  \item {\bf property violation:} the player attempts to expel with probability $s$ another player from her location within their migration area determined by the Moore distance $M$.
\end{itemize}

The three strategies are opportunity based, deterministic for the first two (i.e., if players are presented with better options they will update their strategy or migrate) and random for property violation, reflecting the unsure nature of taking the property of someone else. While decisions are idiosyncratic at the individual level, at the aggregate level, they reveal the expected utility $U$ carried by each rule, which can be formulated as

\begin{equation}
U_{rule} = p_{rule} \times u_{rule},
\label{U}
\end{equation}

with $p_{rule}$ the probability to execute one of the 3 {\it rules} defined above (i.e., $rule = \{$update, migration, expel$\}$, and $u_{rule}$ the payoff increase from executing a {\it rule}. For prisoner's dilemma strategy update and success-driven migration $p_{rule} = (1~|~u_{rule} > 0)$ (reflecting the deterministic decision to execute these rules) and for property violation $p_{rule} = (s~|~u_{rule} > 0)$, reflecting the unsure nature of attempting to expel another individual. For each rule, $U_{rule}(t)$ provides a direct measure -- at the aggregate level -- of opportunity associated with each rule and how it evolves. Similarly, one can define an expected mobility distance,

\begin{equation}
N_{rule} = p_{rule} \times n_{rule},
\end{equation}

where $p_{rule}$ is similar as in (\ref{U}) and $n_{rule}$ is the migration distance. $N_{rule}$ shows the expected travel distance for success-driven and property violation migration rules.\\

Starting from a population randomly scattered across the grid with an even number of cooperators and defectors, $U$ and $N$ undergo significant changes over time, with subtle yet determinant differences between a striving and a collapsing society (see Figure \ref{fig:tseries} for an illustration of a successful and a failed outcome with similar parameters $d=0.5$, $M=5$ and $s=0.037$; This particular case illustrates similar stylized facts obtained for different values of $d$, $M$ and $s$). In both the successful and the failed dynamics, {\it success-driven} migration provides the highest expected payoff increase ($U_{migration} > U_{update} > U_{expel}$)  in the early iterations $t < 1.5\times10^{5}$ (over $20\times10^6$ MCS iterations). After $t > 1.5\times10^{5}$, a significant change occurs: In striving societies (Figure \ref{fig:tseries}b) a switch of expected payoff occurs, which becomes : $U_{expell} > U_{update} > U_{migration}$, while in the failed society case (Figure \ref{fig:tseries}b), strategy update provides the highest expected increased payoff, and the expected increased payoff from success-driven and property violation migrations converge.\footnote{ nb: It is important to note that expected payoff is not necessarily increasing overall the entire population: players expelled from their site suffer a negative expected payoff increase (not shown), as well as neighbors around the focal player who changes prisoner's dilemma strategy to defect, or a defector migrating or performing a property violation.} At the time of the switch, we observe an inflection point of the cooperation level (purple dashed circle on Figure \ref{fig:tseries}b): After this point, cooperation still increases in the failed case, but at much lower pace,\footnote{Mostly driven by strategy updates, which provide the highest expected payoff increase? These updates are at first of the type : $D \rightarrow C$ and then, after the peak rather $C \rightarrow D$. The intuition is that, somewhen migration (whether success driven or property violation) does bring enough payoff increase, compared to strategy update, therefore something weird occurs which is that cooperation increases (more or larger clusters?), which in turn prepare for an invasion of defectors!} on the contrary to striving societies for which we observe a smooth continuous increase of cooperation, up to saturation and stabilization at a high level ($c > 0.8$). In the failed scenario illustrated in Figure \ref{fig:tseries}, the peak of cooperation is reached at  $t \approx 5\times10^5$, and collapse occurs after roughly$10^6$ iterations, nearly $850,000$ iterations after the switching point!\\

While it is hard to observe the detailed switching mechanisms occurring at the transition point,\footnote{This may be done in the future.} we find that the expected mobility (the probability to remains high in the failed scenario in comparison with the successful case (Figures \ref{fig:tseries}c and \ref{fig:tseries}d).\footnote{It is important to note that mobility by property violation and consecutively by individuals forced to move, is not nearly as large as the mobility induced by success-driven actions (c.f., Figures \ref{fig:tseries}b Inset 2).} Also, we find that in the successful scenario, a strong decoupling between expected payoff increase occurs (Figures \ref{fig:tseries}a inset 1), while for the collapsing scenario, dependence between expected payoff remain high and get even reinforced (Figures \ref{fig:tseries}b inset 2).\footnote{The correlogram before the switch also shows high dependence (not shown here), which is natural somehow, as the system gets organized {\bf [This correlogram (pre-switching) should be shown too, at least in the supplementary materials. In the failing scenario, it shows that property violation may influence ]}} Moreover, in the latter case, success-driven migration actions strongly drive private property violations (green correlogram) and strategy updates (red correlogram).\footnote{The correlation peak occurs for a negative lag (roughly 15-20 bins {\bf [I must check the size of a bin in terms of iterations]}) when considering the influence of private property violations and strategy updates on migration} Dependence between property violation and strategy update is also high (blue correlogram), but with no lag thus one cannot say whether $E \rightarrow U$ or $U \rightarrow E$. One can infer however that they are both influenced by success-driven migration (see red and green correlograms).\footnote{It is unclear however what makes success-driven migration the driver of other actions. Something special must occur before or at the switching point.}\\

Past the very first iterations where success-driven migration is the predominant way to increase payoff (in order to build and reach clusters of cooperators, regardless of whether the moving player is a cooperator or a defector), our findings draw two completely different stories leading to either collapse or well-established cooperative societies. In the latter scenario, expected payoff from success-driven migration rapidly decays to a level comparable to the expected payoff derived from strategy update and property violation. Additionally expected payoff increase from the three rules get highly independent: A spatio-temporal decoupling occurs and players no longer undergo long-range migration, and while property violation brings the highest expected payoff increase, it is tackled by some kind of locally established self-organization \footnote{(which remains to be explained/documented in further details)}. On the contrary, the collapse scenario is driven by long-range migrations,\footnote{We need perhaps more insights on the nature of these migrations, e.g., do they spread defectors or rather cooperators? Does this evolve over time?} which in turn trigger strategy updates and property violation.\footnote{These long-range spatio-temporal dependences look like systemic risk to me, which may appear in a seamless, subtle and hardly noticeable way!} \footnote{It would be interesting to check the dynamics with a residual level of property violation, e.g., $s=0.01$, and with a super high level of $s$, to see if these extreme cases magnify differences between success and failure.}

\subsection*{Enduring and Adaptive Societies Facing Property Violation}
One striking observation is that even though the system switches early on, and determines the final outcome, changes may remain unnoticed for a while, as cooperation increases for 4 times more iterations (since $t=0$) until collapse actually occurs.\footnote{In some simulations, the inflection point in the increase of cooperation is hardly noticed (e.g. Figure \ref{fig:adaptation})}\\

Thus, on the one hand it may take time to realize that a society is running towards the edge of collapse, and on the other hand, it may take time to take corrective action, such as taking actions to reduce private property violations.\footnote{As a result of Figure \ref{fig:phase_transition}b, one could also think of increasing migration ranges (which would require massive and long-term infrastructure investments) and decreasing population density, which concretely in our world would mostly require reducing population and would be ethically questionable. Here, we limit our study to private property violations as the main control parameter.}\\

Budgets allocated to property right enforcement may vary over time, in particular, a typical situation is that property crime is low and therefore there is a reduced sense of urgency and budgets might be reduced as a consequence. Also on the long term, institutions concerned with property enforcement may experience downturns of efficiency (e.g., through some kind of corruption).\\

Here, we ask two questions:

\begin{enumerate}
  \item {\bf Resistance:} Given that a cooperative society is in the process of establishing \footnote{I am currently running simulations for this.} or has stabilized at a high level of cooperation (given a level of property violation), how much more property violation and for how long can this society stand a higher level of property violation?
  \item {\bf Adaptation:} Given that a society is on the path to collapse (as shown e.g., on Figure \ref{fig:tseries}b), how early and by how much property violation shall be reduced to ensure it will strive by reaching a stable high level of cooperation?
\end{enumerate}

\paragraph{Resistance:} We find that when a society has stabilized at a high cooperation level ($s=4\%$), increasing a property violation a little ($0.05 \leqslant s \leqslant 0.06$) increases the chance that cooperation will collapse suddenly yet on the long run ( iterations > $3 \times 10^5$ after change of property violation level). For higher levels of  property violation ($s>0.06$), the cooperation curves decreases in a more or less parabolic shape, which seems to be invariant in the limit of $s \rightarrow \infty$, and it looks like that the zero-cooperation level can be hit at earliest around $3 \times 10^5$ after the increase of property violation (see Figure \ref{fig:resistance}).

\paragraph{Adaptation:} The tipping point  ($t \approx 10^5$) occurs way before the edge of actual collapse ($t \approx 10^{5.7}$), and thus, a very natural question is whether it is possible to take corrective actions to prevent the collapse of cooperation once the tipping point has passed and has
been detected. We find that unless property violation can be reduced drastically (e.g., 75\%, c.f., Figure \ref{fig:adaptation}d), action must be taken at latest when cooperation is at its maximum  to ensure resilience (see Figure \ref{fig:adaptation}c when property violation is halved). For more realistic property violation reduction (10\% to 25\%, c.f. Figures \ref{fig:adaptation}a and\ref{fig:adaptation}b), the policy maker must intervene as the cooperation still increases. We could of course imagine more complicated and progressive property violation mitigation schemes (which is actually more relevant for drastic actions to reduce of property violation that may take time to take effect), but the idea is that in order to avoid collapse, action must be taken {\bf before} the effects of property violation get actually widely noticed.\footnote{with more simulations, we could actually compute thoroughly the odds of survival given timing and effort deployed to reduce property violation, but I think what is presented here is enough to demonstrate that it is super important to avoid doing what politicians do -- or are forced to do by the ``real-politics" democratic mechanisms, which is taking action only when the problem get sufficiently noticeable by the population.}\\


% !Mode:: "TeX:UTF-8"
% !TEX program  = xelatex
\section{Summary}
If English is not your first language then writing the mathematical paper is doubly difficult, since you cannot guarantee the proper use of words even though with a large scientific and technical vocabulary. Therefore, Halmos comes up with three specific advice on how to use scientific and technical terms carefully,
\begin{enumerate}[label={\Alph*.}]
	\item Avoid using technical terms as much as possible, especially creating new ones;
	\item Be careful when thinking about the new terms you have to create by looking up the dictionary to make it as appropriate as possible;
	\item Correctly and consistently use the old terms without awkwardness.
\end{enumerate}

In addition to the terminology, you should also pay attention to the language itself. Science and technology English is generally not emotional, and its expression is relatively straightforward, which helps to make the readers easy to understand without creating too much imagination irrelevant. That is, the vocabulary using in science and technology English requires specific and sTable. Therefore, you need to accumulate basic mathematical vocabulary, mathematical symbols with their pronunciation, common phrases and language structures to better write rational mathematics papers.

\subsection{Basic Vocabulary}
Basic vocabulary is an important prerequisite because you have to grapple with a large vocabulary as you try to express your thoughts. There is a large mathematical vocabulary, but the spelling is relatively simple compared to other disciplines, and as long as you master a certain amount of mathematical vocabulary, writing an uncomplicated mathematics paper will not be too difficult.

Please see Table~2.1 to Table~2.13 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} about the basic vocabulary in different fields of mathematics.

\subsection{Symbol Overview}
Mathematical language is the universal language, therefore, when using English to write technical papers or using spoken English to express certain symbols in scientific communication, you should understand the English expression of the common mathematical symbols. In this way, you can do more with less.

Please see Table~2.14 to Table~2.17 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} about the common symbols in different fields of mathematics.

\subsection{Common Phrases}
The common mathematical phrases found in professional English textbooks or theoretically papers are very practical and require proper collection and organization because proficiently using these phrases can be beneficial to your writing.

Please see Table~2.18 to Table~2.20 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} about the common phrases.

\subsection{Language Structures}
Since mathematics writing has certain rules and formats in language structures and styles, there are many fixed modes in mathematics writing, which include some common sentence patterns, modified vocabulary, verbs in mathematical operations and transition statements.
\begin{itemize}[label={\dag}]
	\item Please see Table~2.21 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} about the common sentence patterns;
	\item Please see Table~2.22 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} about the modified vocabulary;
	\item Please see Table~2.23 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} about the verbs used in mathematical operations;
	\item Please see Table~2.24 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} about the common verbs used in mathematics writing;
	\item Please see Section~2.4.5 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} about the transition statements.
\end{itemize}

\nocite{2013数学之英文写作,Nicholas1998Handbook}
\bibliography{ref}


\section{Data}
\label{sec:data}

\newcommand{\ncomoving}{10,000}
\newcommand{\nfollow}{800}
\newcommand{\nfollowme}{363}
\newcommand{\ohpercent}{80}
\newcommand{\mypercent}{80}
\newcommand{\nconfirmed}{500}
\newcommand{\pairprob}{99.99\%}
\newcommand{\badpairprob}{50\%}

Here we use comoving pairs and groups of stars identified in the TGAS
catalogue by \citet{Oh2016}.
These stars do not have precise absolute ages: their absolute ages are
inferred by fitting isochrones to their photometric colours and parallaxes.
However their relative ages are very precise---they should be identical to
within a few million years as we assume that they formed at the same time from
the same molecular cloud.
We use the fact that these stars are coeval to test gyrochronology by
predicting their ages from their rotation periods.
We use the gyrochronal ages predicted for each star in the pair to infer the
intrinsic scatter the gyrochronology relations.

These comoving pairs were identified in the TGAS catalogue by identifying
stars with similar positions and proper motions by \citet{Oh2016}.
The probability that the stars were comoving was compared with the probability
that the stars were drawn from an isotropic, random distribution of
velocities.
In this way, \citet{Oh2016} identified \ncomoving\ comoving pair candidates.

Radial velocity (RV) follow-up of \nfollow\ comoving pair candidates was
performed in February 2017.
\ohpercent\% of these candidates were confirmed to have RVs consistent with
being a comoving pair.
Over seven nights, from the 26$^{th}$ of June to the 2${^nd}$ of July, we
obtained RVs of \nfollowme\ further comoving pair candidates which fall in
the \kepler\ field of view.
Since a small number of stars in the original \citet{Oh2016} catalogue fall in
the \kepler\ field, we used a slightly different sample with less stringent
false positive rates.
The original catalogue contains stars with a formal probability of being
comoving of \pairprob.
We used a catalogue of lower probability stars, specifically with a
\badpairprob\ formal probability of being a true comoving pair and selected
only stars within 10 parsecs of each other in order to reduce the false
positive rate.
\badpairprob\ may seem like a low probability, however with follow-up RV
observations we were able to identify many of the false positives in this
list.
We found \mypercent\% to be true comoving pairs based on their RVs.
We targeted stars in the \kepler\ field as these targets have ultra-precise
time-series photometry, from which we can measure rotation periods.
We selected the \nconfirmed\ confirmed comoving pairs to perform further
analysis.