\chapter{C++ main.cpp - code}
\label{app:main.cpp}

\lstset{language=C++,
	numbers=left,
	stepnumber=1,
	basicstyle=\ttfamily,
	keywordstyle=\color{blue},
	stringstyle=\color{red},
	commentstyle=\color{green},
	morecomment=[l][\color{magenta}]{\#}
}

\begin{lstlisting}[frame = single, label={lst:cppread1}]
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <chrono>

#include <pcl/io/openni2_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/png_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/console/parse.h>
#include <pcl/common/transforms.h>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
#include <pcl/compression/octree_pointcloud_compression.h>
#include <pcl/filters/voxel_grid.h>

#include <octomap/octomap.h>
#include <octomap/ColorOcTree.h>

#include "com_func.h"

#define SERV_TCP_PORT_READ 59004
#define SERV_TCP_PORT_WRITE 59003

using namespace std;
using namespace pcl;

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr transformedCloudCamB (new pcl::PointCloud<pcl::PointXYZRGBA> ());
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr transformedCloudCamA (new pcl::PointCloud<pcl::PointXYZRGBA> ());
pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA>* PointCloudEncoder;
pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA>* PointCloudDecoder;
octomap::ColorOcTree rayMap(0.04);

int nrows = 4;
int ncols = 4;

Eigen::Matrix4f transformCamA = Eigen::Matrix4f::Zero(nrows,ncols);
Eigen::Matrix4f transformCamB = Eigen::Matrix4f::Zero(nrows,ncols);

int positionIndex = 0;
bool debug = false;
bool rayMapinit = false;

class SimpleOpenNIViewer
{
public:
SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {}
void cloud_cb_2_ (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr &cloud)
{
if (!viewer.wasStopped()){
//            auto startPCgenCamB = chrono::steady_clock::now();
// Create the filtering object
pcl::PassThrough<pcl::PointXYZRGBA> pass;

// read cloud transformation data from file
//            cout << "transformCamB = " << endl << transformCamB << endl;

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr CloudCamBfiltered (new pcl::PointCloud<pcl::PointXYZRGBA> ());
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr CloudCamBVoxelGrid (new pcl::PointCloud<pcl::PointXYZRGBA> ());

pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (0.0, 3.5);
//pass.setFilterLimitsNegative (true);
pass.filter (*CloudCamBfiltered);

pcl::VoxelGrid<pcl::PointXYZRGBA> voxelFilter;
voxelFilter.setInputCloud(CloudCamBfiltered);
voxelFilter.setLeafSize (0.01f, 0.01f, 0.01f);
voxelFilter.filter (*CloudCamBVoxelGrid);

// Executing the transformation
pcl::transformPointCloud (*CloudCamBVoxelGrid, *transformedCloudCamB, transformCamB);

//    (row, column)
viewer.showCloud (transformedCloudCamB, cloudnameB);
//            if(debug == true){
stringstream stream;
stream << "inputCloudCamB.pcd";
string filename = stream.str();
io::savePCDFileASCII(filename, *CloudCamBfiltered);
//            PCL_ERROR("Problem saving %s.\n", filename.c_str());
stream.str(std::string());
stream << "transformedCloudB.pcd";
filename = stream.str();
io::savePCDFileASCII(filename, *transformedCloudCamB);
stream.str(std::string());
stream << "inputCLoudCamB.png";
filename = stream.str();
io::savePNGFile(filename, *cloud, "rgb");
//            cout<< "Saved " << filename << "." << endl;
//            cloud = transformedCloudCamB;
//            }
//            auto endPCgenCamB = chrono::steady_clock::now();
//            cout << "Elapsed Time in milliseconds (camB PointCloud generation): " << chrono::duration_cast<chrono::milliseconds>(endPCgenCamB - startPCgenCamB).count() << "ms" << endl;
}
}

void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr &cloud)
{
if (!viewer.wasStopped()){
//            auto startPCgenCamA = chrono::steady_clock::now();
// Create the filtering object
pcl::PassThrough<pcl::PointXYZRGBA> pass;

//            cout << "transformCamA = " << endl << transformCamA << endl;

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr CloudCamAfiltered (new pcl::PointCloud<pcl::PointXYZRGBA> ());
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr CloudCamAVoxelGrid (new pcl::PointCloud<pcl::PointXYZRGBA> ());

pass.setInputCloud (cloud);
pass.setFilterFieldName ("z");
pass.setFilterLimits (0.0, 4.1);
//pass.setFilterLimitsNegative (true);
pass.filter (*CloudCamAfiltered);

pcl::VoxelGrid<pcl::PointXYZRGBA> voxelFilter;
voxelFilter.setInputCloud(CloudCamAfiltered);
voxelFilter.setLeafSize (0.01f, 0.01f, 0.01f);
voxelFilter.filter (*CloudCamAVoxelGrid);

// Executing the transformation
pcl::transformPointCloud (*CloudCamAVoxelGrid, *transformedCloudCamA, transformCamA);

//    (row, column)
viewer.showCloud (transformedCloudCamA, cloudnameA);
//            if(debug == true){
stringstream stream;
stream << "inputCloudCamA.pcd";
string filename = stream.str();
io::savePCDFileASCII(filename, *CloudCamAVoxelGrid);
//            PCL_ERROR("Problem saving %s.\n", filename.c_str());
stream.str(std::string());
stream << "transformedCloudA.pcd";
filename = stream.str();
io::savePCDFileASCII(filename, *transformedCloudCamA);
stream.str(std::string());
stream << "inputCLoudCamA.png";
filename = stream.str();
io::savePNGFile(filename, *cloud, "rgb");
//            cout<< "Saved " << filename << "." << endl;
//            cloud = transformedCloudCamA;
//            }
//            auto endPCgenCamA = chrono::steady_clock::now();
//           cout << "Elapsed Time in milliseconds (camA PointCloud generation): " << chrono::duration_cast<chrono::milliseconds>(endPCgenCamA - startPCgenCamA).count() << "ms" << endl;
}
}

void run ()
{
//        //**********************************
//        start first Xtion
Grabber* interface = new io::OpenNI2Grabber("#1");

boost::function<void (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f =
boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);

interface->registerCallback (f);
interface->start();
//**********************************

//**********************************
//        start second Xtion
pcl::Grabber* interface2 = new io::OpenNI2Grabber("#2");
boost::function<void (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f2 =
boost::bind (&SimpleOpenNIViewer::cloud_cb_2_, this, _1);

interface2->registerCallback (f2);

interface2->start();
//**********************************

while (!viewer.wasStopped())
{
bool showStatistics = true;
bool reset = false;

pcl::io::compression_Profiles_e compressionProfile = pcl::io::MED_RES_ONLINE_COMPRESSION_WITH_COLOR;

PointCloudEncoder = new pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA> (compressionProfile, showStatistics);
PointCloudDecoder = new pcl::io::OctreePointCloudCompression<pcl::PointXYZRGBA> ();

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr concatenatedCloud (new pcl::PointCloud<pcl::PointXYZRGBA> ());
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZRGBA> ());

*concatenatedCloud = *transformedCloudCamA;
*concatenatedCloud += *transformedCloudCamB;

io::savePCDFileASCII("concatenated.pcd", *concatenatedCloud);

pcl::VoxelGrid<pcl::PointXYZRGBA> voxelFilter;
voxelFilter.setInputCloud(concatenatedCloud);
voxelFilter.setLeafSize (0.04f, 0.04f, 0.04f);
voxelFilter.filter (*cloud_filtered);

io::savePCDFileASCII("cloud_filtered.pcd", *cloud_filtered);

// stringstream to store compressed point cloud
std::stringstream compressedData;

// compress point cloud
PointCloudEncoder->encodePointCloud (cloud_filtered, compressedData);

//octomap
//            cout<<"copy data into octomap..."<<endl;
//
//            auto startOctreeGen = chrono::steady_clock::now();
octomap::ColorOcTree tree( 0.04 );
for (float x = -2.5; x <= 2.5; x += 0.08f) {
for (float y = 0; y <= 4; y += 0.08f) {
for (float z = -1.5; z <= 2; z += 0.08f) {
octomap::point3d endpoint(x, y, z);
tree.updateNode(endpoint, false); // integrate 'free' measurement
//                        rayMap.updateNode(endpoint, false);
}
}
}

for (auto p:cloud_filtered->points)
{
tree.updateNode(octomap::point3d(p.x, p.y, p.z), true);
if(rayMapinit == false){
rayMap.updateNode(octomap::point3d(p.x, p.y, p.z), true);
}
}

for (auto p:cloud_filtered->points)
{
tree.integrateNodeColor( p.x, p.y, p.z, p.r, p.g, p.b );
if(rayMapinit == false){
rayMap.integrateNodeColor( p.x, p.y, p.z, p.r, p.g, p.b );
}
}
rayMapinit = true;
//            auto endOctreeGen = chrono::steady_clock::now();
//            cout << "Elapsed Time in milliseconds (Octomap generation): " << chrono::duration_cast<chrono::milliseconds>(endOctreeGen - startOctreeGen).count() << "ms" << endl;

float fixedPos[24][6];
//position 1 left side top 1
fixedPos[0][0] = -860;
fixedPos[0][1] = 1330;
fixedPos[0][2] = -560;
fixedPos[0][3] = 180;
fixedPos[0][4] = 0.0;
fixedPos[0][5] = 90.0;

//position 2 left side bottom
fixedPos[1][0] = -860;
fixedPos[1][1] = 1330;
fixedPos[1][2] = -650;
fixedPos[1][3] = 180;
fixedPos[1][4] = 0.0;
fixedPos[1][5] = 90.0;

//position 3 left side top 2
fixedPos[2][0] = -860;
fixedPos[2][1] = 1330;
fixedPos[2][2] = -560;
fixedPos[2][3] = 180;
fixedPos[2][4] = 0.0;
fixedPos[2][5] = 90.0;

//position 4 right side top 1
fixedPos[3][0] = 600;
fixedPos[3][1] = 1550;
fixedPos[3][2] = -560;
fixedPos[3][3] = 180;
fixedPos[3][4] = 0.0;
fixedPos[3][5] = 90.0;

//position 5 right side bottom
fixedPos[4][0] = 600;
fixedPos[4][1] = 1550;
fixedPos[4][2] = -655;
fixedPos[4][3] = 180;
fixedPos[4][4] = 0.0;
fixedPos[4][5] = 90.0;

//position 6 right side top 2
fixedPos[5][0] = 600;
fixedPos[5][1] = 1550;
fixedPos[5][2] = -560;
fixedPos[5][3] = 180;
fixedPos[5][4] = 0.0;
fixedPos[5][5] = 90.0;

//position 7 left side top 1
fixedPos[6][0] = -860;
fixedPos[6][1] = 1550;
fixedPos[6][2] = -560;
fixedPos[6][3] = 180;
fixedPos[6][4] = 0.0;
fixedPos[6][5] = 90.0;

//position 8 left side bottom
fixedPos[7][0] = -860;
fixedPos[7][1] = 1550;
fixedPos[7][2] = -650;
fixedPos[7][3] = 180;
fixedPos[7][4] = 0.0;
fixedPos[7][5] = 90.0;

//position 9 left side top 2
fixedPos[8][0] = -860;
fixedPos[8][1] = 1550;
fixedPos[8][2] = -560;
fixedPos[8][3] = 180;
fixedPos[8][4] = 0.0;
fixedPos[8][5] = 90.0;

//position 10 right side top 1
fixedPos[9][0] = 600;
fixedPos[9][1] = 1330;
fixedPos[9][2] = -560;
fixedPos[9][3] = 180;
fixedPos[9][4] = 0.0;
fixedPos[9][5] = 90.0;

//position 11 right side bottom
fixedPos[10][0] = 600;
fixedPos[10][1] = 1330;
fixedPos[10][2] = -655;
fixedPos[10][3] = 180;
fixedPos[10][4] = 0.0;
fixedPos[10][5] = 90.0;

//position 12 right side top 2
fixedPos[11][0] = 600;
fixedPos[11][1] = 1330;
fixedPos[11][2] = -560;
fixedPos[11][3] = 180;
fixedPos[11][4] = 0.0;
fixedPos[11][5] = 90.0;

//position 13 right side top 1
fixedPos[12][0] = 600;
fixedPos[12][1] = 1550;
fixedPos[12][2] = -560;
fixedPos[12][3] = 180;
fixedPos[12][4] = 0.0;
fixedPos[12][5] = 90.0;

//position 14 right side bottom
fixedPos[13][0] = 600;
fixedPos[13][1] = 1550;
fixedPos[13][2] = -655;
fixedPos[13][3] = 180;
fixedPos[13][4] = 0.0;
fixedPos[13][5] = 90.0;

//position 15 right side top 2
fixedPos[14][0] = 600;
fixedPos[14][1] = 1550;
fixedPos[14][2] = -560;
fixedPos[14][3] = 180;
fixedPos[14][4] = 0.0;
fixedPos[14][5] = 90.0;

//position 16 left side top 1
fixedPos[15][0] = -860;
fixedPos[15][1] = 1330;
fixedPos[15][2] = -560;
fixedPos[15][3] = 180;
fixedPos[15][4] = 0.0;
fixedPos[15][5] = 90.0;

//position 17 left side bottom
fixedPos[16][0] = -860;
fixedPos[16][1] = 1330;
fixedPos[16][2] = -650;
fixedPos[16][3] = 180;
fixedPos[16][4] = 0.0;
fixedPos[16][5] = 90.0;

//position 18 left side top 2
fixedPos[17][0] = -860;
fixedPos[17][1] = 1330;
fixedPos[17][2] = -560;
fixedPos[17][3] = 180;
fixedPos[17][4] = 0.0;
fixedPos[17][5] = 90.0;

//position 19 right side top 1
fixedPos[18][0] = 600;
fixedPos[18][1] = 1330;
fixedPos[18][2] = -560;
fixedPos[18][3] = 180;
fixedPos[18][4] = 0.0;
fixedPos[18][5] = 90.0;

// position 20 right side bottom
fixedPos[19][0] = 600;
fixedPos[19][1] = 1330;
fixedPos[19][2] = -655;
fixedPos[19][3] = 180;
fixedPos[19][4] = 0.0;
fixedPos[19][5] = 90.0;

//position 21 right side top 2
fixedPos[20][0] = 600;
fixedPos[20][1] = 1330;
fixedPos[20][2] = -560;
fixedPos[20][3] = 180;
fixedPos[20][4] = 0.0;
fixedPos[20][5] = 90.0;

//position 22 left side top 1
fixedPos[21][0] = -860;
fixedPos[21][1] = 1550;
fixedPos[21][2] = -560;
fixedPos[21][3] = 180;
fixedPos[21][4] = 0.0;
fixedPos[21][5] = 90.0;

//position 23 left side bottom
fixedPos[22][0] = -860;
fixedPos[22][1] = 1550;
fixedPos[22][2] = -650;
fixedPos[22][3] = 180;
fixedPos[22][4] = 0.0;
fixedPos[22][5] = 90.0;

//position 24 left side top 2
fixedPos[23][0] = -860;
fixedPos[23][1] = 1550;
fixedPos[23][2] = -560;
fixedPos[23][3] = 180;
fixedPos[23][4] = 0.0;
fixedPos[23][5] = 90.0;

float *currentPosition = 0;
float originPosition[6];
float goalPosition [6];
float avoidPositionStart [6];
float avoidPositionGoal [6];

currentPosition = connectReadCartesian(currentPosition);
// remove tool from map to avoid collision with itself
cout << "***********************************************************************" << endl;
cout << "Delete camera data of robot tool" << endl;
cout << "***********************************************************************" << endl;
for (float x = (currentPosition[0]/1000)-0.10; x <= (currentPosition[0]/1000)+0.10; x += 0.04f) {
for (float y = (currentPosition[1]/1000)-0.04; y <= (currentPosition[1]/1000)+0.1; y += 0.04f) {
for (float z = (currentPosition[2]/1000)-0.04; z <= (currentPosition[2]/1000)+0.1; z += 0.04f) {
octomap::point3d endpoint(x, y, z);
tree.updateNode(endpoint, false); // integrate 'free' measurement
//                        rayMap.updateNode(endpoint, false);
}
}
}
cout << "***********************************************************************" << endl;
cout << "data deleted" << endl;
cout << "***********************************************************************" << endl;

for(int s = 0; s < 6; s++){
goalPosition[s] = fixedPos[positionIndex][s];
avoidPositionGoal[s] = goalPosition[s];
avoidPositionStart[s] =  currentPosition[s];
originPosition[s] = currentPosition[s];
}
if(debug == true){
cout << "***********************************************************************" << endl;
cout << "Initialisation" << endl;
cout << "***********************************************************************" << endl;
printf("avoidPositionStart X pos: %f \n", avoidPositionStart[0]);
printf("avoidPositionStart Y pos: %f \n", avoidPositionStart[1]);
printf("avoidPositionStart Z pos: %f \n", avoidPositionStart[2]);
printf("avoidPositionStart W pos: %f \n", avoidPositionStart[3]);
printf("avoidPositionStart P pos: %f \n", avoidPositionStart[4]);
printf("avoidPositionStart R pos: %f \n", avoidPositionStart[5]);
cout << "***********************************************************************" << endl;
cout << "***********************************************************************" << endl;
printf("avoidPositionGoal X pos: %f \n", avoidPositionGoal[0]);
printf("avoidPositionGoal Y pos: %f \n", avoidPositionGoal[1]);
printf("avoidPositionGoal Z pos: %f \n", avoidPositionGoal[2]);
printf("avoidPositionGoal W pos: %f \n", avoidPositionGoal[3]);
printf("avoidPositionGoal P pos: %f \n", avoidPositionGoal[4]);
printf("avoidPositionGoal R pos: %f \n", avoidPositionGoal[5]);
cout << "***********************************************************************" << endl;
cout << "Initialisation" << endl;
cout << "***********************************************************************" << endl;
}
bool pathFound = false;
int avoidAttemptCnt = 1;

tree.updateInnerOccupancy();
tree.write( "output_file.ot" );

while(pathFound == false && reset == false){
cout << "***********************************************************************" << endl;
cout << "PositionIndex #" << positionIndex << endl;
cout << "***********************************************************************" << endl;
cout << "Attempt #" << avoidAttemptCnt << " to move" << endl;

octomap::point3d rayStart(avoidPositionStart[0]/1000, avoidPositionStart[1]/1000, avoidPositionStart[2]/1000);
octomap::point3d rayDirection((avoidPositionGoal[0]-avoidPositionStart[0])/1000, (avoidPositionGoal[1]-avoidPositionStart[1])/1000, (avoidPositionGoal[2]-avoidPositionStart[2])/1000);
octomap::point3d end;
octomap::point3d rayEnd(avoidPositionGoal[0]/1000, avoidPositionGoal[1]/1000, avoidPositionGoal[2]/1000);
double rayDistance = sqrt(pow((avoidPositionGoal[0]-avoidPositionStart[0])/1000, 2) + pow((avoidPositionGoal[1]-avoidPositionStart[1]), 2)+ pow((avoidPositionGoal[2]-avoidPositionStart[2])/1000, 2));

bool obstacleFound = tree.castRay(rayStart, rayDirection, end, true, rayDistance);

cout << "rayDirection x:" << rayDirection.x() << "   y:" << rayDirection.y() << "   z:" << rayDirection.z() << endl;
std::vector<octomap::point3d> ray;
//                if(debug == true){
rayMap.computeRay(rayStart, rayEnd, ray);
cout << "after computeRay" << endl;
//                cout << "ray.size() ==" << ray.size()<< endl;

// compute Ray for rayMap

for(unsigned int x = 0; x < ray.size(); x++){

rayMap.updateNode( octomap::point3d(ray[x].x(), ray[x].y(), ray[x].z()), true );
rayMap.integrateNodeColor(ray[x].x(), ray[x].y(), ray[x].z(), 255, 0, 0);
//                        cout << "rayCoordinates #" << x <<" :   x:" << ray[x].x() <<"   y:" << ray[x].y()<< "   z:" << ray[x].z() << endl;
}

rayMap.updateInnerOccupancy();
rayMap.write( "rayMap.ot" );
cout << "after rayMap" << endl;
//                }

// Vertical Movement not regarded for collision avoidance
if(positionIndex == 2 || positionIndex == 5 || positionIndex == 8 || positionIndex == 11 || positionIndex == 14 || positionIndex == 17 || positionIndex == 20 || positionIndex == 23){       // maybe directional condition based on rayDirection to check if robot is moving up or down
if(debug == true){do {
cout << "***************************************************" << endl;
cout << "moving to upper position, press any key to continue" << endl;
cout << "***************************************************" << endl;

} while (cin.get() != '\n');
}
connectWriteCartesian(avoidPositionGoal, currentPosition);
positionIndex ++;
pathFound = true;
}
// Vertical Movement not regarded for collision avoidance
else if(positionIndex == 1 || positionIndex == 7 || positionIndex == 13 || positionIndex == 19){  // maybe directional condition based on rayDirection to check if robot is moving up or down
if(debug == true){
do {
cout << "******************************************************" << endl;
cout << "moving to gripping position, press any key to continue" << endl;
cout << "******************************************************" << endl;

} while (cin.get() != '\n');
}
connectWriteCartesian(avoidPositionGoal, currentPosition);
sleep(2);
gripperOn();
positionIndex ++;
pathFound = true;
}
// Vertical Movement not regarded for collision avoidance
else if(positionIndex == 4  || positionIndex == 10 || positionIndex == 16 || positionIndex == 22){  // maybe directional condition based on rayDirection to check if robot is moving up or down
if(debug == true){
do {
cout << "******************************************************" << endl;
cout << "moving to gripping position, press any key to continue" << endl;
cout << "******************************************************" << endl;

} while (cin.get() != '\n');
}
connectWriteCartesian(avoidPositionGoal, currentPosition);
sleep(2);
gripperOff();
positionIndex ++;
pathFound = true;
}

//Raycasting not finding any object in the path
else if( obstacleFound == false){
if(debug == true){
do {
cout << "**************************************************" << endl;
cout << "no obstacle, press any key to move to goalPosition" << endl;
cout << "**************************************************" << endl;

} while (cin.get() != '\n');
}
connectWriteCartesian(avoidPositionGoal, currentPosition);
if(avoidPositionGoal != goalPosition){
cout << "avoidPositionGoal != goalPosition" << endl;
currentPosition = connectReadCartesian(currentPosition);
connectWriteCartesian(goalPosition, currentPosition);
}
avoidAttemptCnt = 1;
positionIndex ++;
pathFound = true;
}

//Raycasting finding any object in the path
else if(obstacleFound == true){
//                    auto startAvoid = chrono::steady_clock::now();
// Attempts to avoid collisions too high!
if(avoidAttemptCnt >= 6){

do {
cout << "*************************************************************" << endl;
cout << "can't find a path, remove object and press any key to move to" << endl;
cout << "       origin position and restart collision detection" << endl;
cout << "*************************************************************" << endl;

} while (cin.get() != '\n');

connectWriteCartesian(originPosition, currentPosition);
//                        currentPosition = connectReadCartesian(currentPosition);
for(int s = 0; s < 6; s++){
avoidPositionGoal[s] = goalPosition[s];
//                            avoidPositionStart[s] =  currentPosition[s];
}
avoidAttemptCnt = 1;
reset = true;
}
// Attempting to avoid collision by moving in z-Direction
else{
if(debug == true){
do {
cout << "***********************************************************************" << endl;
cout << "obstacle found, moving up to avoid collision. Press any key to continue" << endl;
cout << "***********************************************************************" << endl;

} while (cin.get() != '\n');
}
avoidPositionStart[2] += 100;
avoidPositionGoal[2] +=100;
if(debug == true){
cout << "***********************************************************************" << endl;
cout << "After z incrementation" << endl;
cout << "***********************************************************************" << endl;
printf("avoidPositionStart X pos: %f \n", avoidPositionStart[0]);
printf("avoidPositionStart Y pos: %f \n", avoidPositionStart[1]);
printf("avoidPositionStart Z pos: %f \n", avoidPositionStart[2]);
printf("avoidPositionStart W pos: %f \n", avoidPositionStart[3]);
printf("avoidPositionStart P pos: %f \n", avoidPositionStart[4]);
printf("avoidPositionStart R pos: %f \n", avoidPositionStart[5]);
cout << "***********************************************************************" << endl;
cout << "After z incrementation" << endl;
cout << "***********************************************************************" << endl;
}
connectWriteCartesian(avoidPositionStart, currentPosition);
currentPosition = connectReadCartesian(currentPosition);
avoidAttemptCnt ++;
}
//                    auto endAvoid = chrono::steady_clock::now();
//                    cout << "Elapsed Time in milliseconds (avoid obstacle): " << chrono::duration_cast<chrono::milliseconds>(endAvoid - startAvoid).count() << "ms" << endl;
}

// counter for fixed positions reset to start from the beginning.
if(positionIndex >= 24){
positionIndex = 0;
}
if(debug == true){
do {
cout << "*********************************************" << endl;
cout << "checkpoint reached, press any key to continue" << endl;
cout << "*********************************************" << endl;
} while (cin.get() != '\n');
}
}
}
interface->stop ();
interface2->stop ();
}

pcl::visualization::CloudViewer viewer;
const std::string cloudnameA = "cloudA";
const std::string cloudnameB = "cloudB";
};

int main (int argc, char * const argv[])
{
if(argc > 1){
if (argv[1] == std::string("-d")){
cout << "*********************************************" << endl;
cout << "Entering Debug Mode, have fun pushing buttons" << endl;
cout << "*********************************************" << endl;
debug = true;
}
}

// read cloud transformation data from file
ifstream fin ("./../Rob195/cloudA.txt");

if (fin.is_open())
{
for (int row = 0; row < nrows; row++)
for (int col = 0; col < ncols; col++)
{
float item = 0.0;
fin >> item;
transformCamA(row, col) = item;
}
fin.close();
}
ifstream fin2 ("./../Rob195/cloudB.txt");
{
for (int row = 0; row < nrows; row++)
for (int col = 0; col < ncols; col++)
{
float item = 0.0;
fin2 >> item;
transformCamB(row, col) = item;
}
fin2.close();
}

SimpleOpenNIViewer v;
v.run ();

return 0;
}

\end{lstlisting}


\chapter{Technical Background}\label{Chapter:Theory}
This chapter provides a brief technical overview of the models that we will frequently encounter in the rest of the thesis. A fundamental understanding of these systems is essential for appreciating the problems associated with them and the potential solution for overcoming those problems, which will be discussed in the subsequent chapters.

The key concepts of \textbf{attention} and \textbf{pondering}, whose conceptualization lies in the study of human reasoning are presented in this chapter as well. I elaborate on how these concepts have been the crucial first steps towards making deep neural networks think like human beings, thereby making their decision making process more interpretable. These concepts also serve as the backbone for the first major contribution of this thesis, which is presented in chapter \ref{Chapter:proposals}.

This chapter concludes with an overview of formal language theory which is a prerequisite for understanding the theory behind creation of a new dataset - the second major contribution of this thesis which I present in chapter \ref{Chapter:datasets}.

\section{RNN - A non linear dynamical system} \label{RNN}
We frequently encounter data that is temporal in nature. A few examples would  be, audio signals, videos signals, time series of a stock price and natural language. While traditional feed-forward neural networks such as a multi-layer perceptron (MLP) \citep{rosenblatt1962} are excellent at non linear curve fitting and classification tasks, it is unclear as to how they will approach the problem of predicting the value of a temporal signal $T$ at time $t$ given the states $T_{0}, T_{1}....T_{t-1}$ such that the states over time are not i.i.d. This is owing to the fact that a conventional feed-forward network  is acyclic and thus doesn't have any feedback loops rendering it memoryless. Human beings arguably solve such problems by compressing and storing the previous states in a \lq working\rq{} memory,\citep{Miller1956} \lq chunking\rq{} it \citep{neath2013} \citep{craik2000} and predicting the state $T_t$.

A Recurrent neural network (RNN) \citep{Hopfield1982}\citep{Elman1990} overcomes this restriction by having feedback loops which allow information to be carried from the current time step to the next. While the notion of implementing memory via feedback loops might seem daunting at first the architecture is refreshingly simple. In a process known as unrolling a RNN can be seen as a sequence of MLPs (at different time steps) stacked together. More specifically, RNN maintains a memory across time-steps by projecting the information at any given time step, $t$ onto a hidden(latent) state through parameters $\theta$ which are shared across different time-steps. Figure \ref{bck:rnn} shows a rolled and unrolled RNN cell and the equations of an RNN are as follows:

\begin{equation}\label{nn-dynam}
c_t = tanh(Ux_t + \theta c_{t-1}),
\end{equation}

\begin{equation}
y_t = softmax(Vc_t).
\end{equation}

\begin{figure}
	\begin{minipage}[t]{\textwidth}
		\ifpdf
		\includegraphics[width=\linewidth,keepaspectratio=true]{./figs/RNN-unrolled-pdf}
		\else
		\includegraphics[width=\linewidth,keepaspectratio=true]{./figs/RNN-unrolled-eps}
		\fi
		\caption{\small Schematic of a RNN \cite{olah}}
		\label{bck:rnn}
	\end{minipage}
\end{figure}

\section{BPTT and Vanishing Gradients}
The conventional method of training a feedforward neural network is a two step process. The first step is called the \textbf{forward pass} when values fed at input layer pass through the hidden layers, are acted upon by (linear or non-linear) activations and come out at the output layer. In the second step called the \textbf{backward pass}  the error computed at the output layer (from the target output) flows backward through the network i.e. by applying the chain rule of differentiation, the error gradient at output layer, is computed with respect to all possible paths right upto input layer and then aggregated. This method of optimization in neural networks is called \textbf{backpropagation} \citep{Rumelhart1986}.

In the case of an RNN the optimization step i.e. the backward pass over the network weights is not just with regards to the parameters at the final time step but over all the time steps across which the weights (parameters) are shared. This is known as Back Propagation Through Time (BPTT) \citep{Werbos1990} and it gives rise to the problem of vanishing (or exploding) gradients in vanilla RNNs. This concept is better elaborated upon through equation in the following section.

\subsection{BPTT for Vanilla RNN}
\begin{equation}
\mathbf{\mathcal{L}\left(x,y\right)} = - \sum_{t}\left(y_t \log\hat{y_t}\right)
\end{equation}

%\begin{equation}
%\frac{\partial \mathbf{\mathcal{L}}}{\partial \alpha_t} = -\left(y_t-z_t\right)
%\end{equation}
The weight $W_{oh}$ is shared across all time steps. $\therefore$ adding the derivatives across the sequence:

\begin{equation}
\frac{\partial \mathbf{\mathcal{L}}}{\partial W_{oh}} = \sum_{t}\frac{\partial \mathbf{\mathcal{L}}}{\partial \hat{y_t}} \frac{\partial  \hat{y_t}}{\partial W_{oh}}
\end{equation}

%\begin{equation}
%\frac{\partial \mathbf{\mathcal{L}}}{\partial b_z} = \sum_{t}\frac{\partial \mathbf{\mathcal{L}}}{\partial z_t} %\frac{\partial  z_t}{\partial b_z}
%\end{equation}
For time-step t $\rightarrow$ t+1:

\begin{equation}
\frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial W_{hh}} = \frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \hat{y}_{t+1}} \frac{\partial  \hat{y}_{t+1}}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial W_{hh}}
\end{equation}

$W_{hh}$ is shared across time steps, we take the contribution from previous time steps as well, for calculating the gradient at time $t+1$. Summing over the sequence we get:

%\begin{equation}
%\frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial W_{hh}} = \frac{\partial %\mathbf{\mathcal{L}}\left(t+1\right)}{\partial \hat{y}_{t+1}} \frac{\partial  \hat{y}_{t+1}}{\partial \mathbf{h}_{t+1}} %\frac{\partial \mathbf{h}_{t+1}}{\partial \mathbf{h}_t} \frac{\partial \mathbf{h}_t}{\partial W_{hh}}
%\end{equation}

\begin{equation}
\frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial W_{hh}} = \sum_{\tau=1}^{t+1}\frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \hat{y}_{t+1}} \frac{\partial  \hat{y}_{t+1}}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial \mathbf{h}_{\tau}} \frac{\partial \mathbf{h}_{\tau}}{\partial W_{hh}}
\end{equation}

Summing over the whole sequence we get:

\begin{equation} \label{vanishing}
\frac{\partial \mathbf{\mathcal{L}}}{\partial W_{hh}} = \sum_{t}\sum_{\tau=1}^{t+1}\frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \hat{y}_{t+1}} \frac{\partial  \hat{y}_{t+1}}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial \mathbf{h}_{\tau}} \frac{\partial \mathbf{h}_{\tau}}{\partial W_{hh}}
\end{equation}

From equation \ref{vanishing} it is clear than the gradient of a RNN can be expressed as a recursive product of $\frac{\partial h_t}{\partial h_{t-1}}$. \textbf{If this derivative is $\ll 1$ or $\gg 1$, the gradient would vanish or explode respectively} when the network is trained over longer time-steps. In the former case error back-propagated would be too low to change the weights (the training would freeze) while in the latter it would never converge.

Additionally revisiting equation \ref{nn-dynam} and rewriting it as follows:
\begin{equation}\label{ndq}
h^{(t)} = f(x^{(t)}, h^{(t-1)}),
\end{equation}
it is not difficult to see that in the absence of an external input $x^{(t)}$ an RNN induces a a dynamical system. The RNN therefore, can be viewed as a dynamical system with the input as an external force (map) that drives it. A dynamical system can posses a set of points which are invariant under any map. These points are called the \textbf{attractor states} of a dynamical system. These set of points can contain a single point (fixed attractor), a finite set of points (periodic attractor) or an infinite set of points (strange attractor). The type of attractor in a RNN unit depends on the initialization of the weight matrix for the hidden state \citep{Bengio1993}. Now under the application of map (input) if $\frac{\partial h_t}{\partial h_{k}}$ (for a large $t - k$ i.e. long term dependency), go to zero one can argue that the state $h_t$ is in the basin of one of the attractor states. This implies that in order to avoid the vanishing gradient problem a RNN cell must stay close to the \lq boundaries between basins of attraction\rq{} \citep{Pascanu2012}.

Owing to this problem of vanishing (or exploding) gradients a vanilla RNN can't keep track of long term dependencies which is arguably critical for tasks such as speech synthesis, music composition or neural machine translation. The architectural modifications which solved the vanishing gradient problem and are the current de-facto RNN cell(s) are presented in the next section.

\iffalse
\begin{equation}
\frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial W_{hx}} = \frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial W_{hx}}
\end{equation}

\begin{equation}
\begin{split}
\frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial W_{hx}} & = \frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial W_{hx}} + \frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \mathbf{h}_t} \frac{\partial \mathbf{h}_t}{\partial W_{hx}} \\
& = \frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial W_{hx}} + \frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial \mathbf{h}_t}\frac{\partial \mathbf{h}_t}{\partial W_{hx}}
\end{split}\end{equation}

\begin{equation}
\frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial W_{hx}} = \sum_{\tau=1}^{t+1} \frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial \mathbf{h}_{\tau}} \frac{\partial \mathbf{h}_{\tau}}{\partial W_{hx}}
\end{equation}

\begin{equation}
\frac{\partial \mathbf{\mathcal{L}}}{\partial W_{hx}} = \sum_{t} \sum_{\tau=1}^{t+1} \frac{\partial \mathbf{\mathcal{L}}\left(t+1\right)}{\partial \hat{y}_{t+1}} \frac{\partial \hat{y}_{t+1}}{\partial \mathbf{h}_{t+1}} \frac{\partial \mathbf{h}_{t+1}}{\partial \mathbf{h}_{\tau}} \frac{\partial \mathbf{h}_{\tau}}{\partial W_{hx}}
\end{equation}
\fi

\section{Gated RNNs}
The problem of vanishing (or exploding) gradients makes a vanilla RNN unsuitable for long term dependency modeling. However, if for instance, the RNN was to compute an identity function then the gradient computation wouldn't vanish or explode since the Jacobian is simply an identity matrix. Now while an identity initialization of recurrent weights by itself isn't  interesting it brings us to the underlying principle behind gated architectures i.e. the mapping from memory state at one time step to the next is close to identity function.

\begin{figure}[ht]
	\begin{subfigure}[b]{0.5\linewidth}
		\centering
		\ifpdf
		\includegraphics[width=0.95\linewidth]{./figs/Lstm-pdf}
		\else
		\includegraphics[width=0.95\linewidth]{./figs/Lstm-eps}
		\fi
		\caption{LSTM\citep{Zheng2017}}
		\label{lstm}
		\vspace{4ex}
	\end{subfigure}%%
	\begin{subfigure}[b]{0.5\linewidth}
		\centering
		\ifpdf
		\includegraphics[width=0.95\linewidth]{./figs/Gru-pdf}
		\else
		\includegraphics[width=0.95\linewidth]{./figs/Gru-eps}
		\fi
		\caption{GRU \citep{olah}}
		\label{gru}
		\vspace{4ex}
	\end{subfigure}
	\caption{Gated RNNs}
	\label{conf}
\end{figure}

\subsection{LSTM}
Long Short Term Memory (LSTM) introduced by \cite{LSTM} is one of the two most widely used gated RNN architectures in use today. The fact that it has survived all the path-breaking innovations in the field of deep learning for over twenty years to still be the state of the art in sequence modeling speaks volumes about the architecture's ingenuity and strong fundamentals.

The fundamental principle behind the working of a LSTM is, to alter the memory vector only selectively between time steps such that the memory state is preserved over long distances. The architecture is explained as follows:

\begin{equation}
i= \sigma(x_t U^{(i)}, m_{t-1}W^{(i)})
\end{equation}

\begin{equation}
f= \sigma(x_t U^{(f)}, m_{t-1}W^{(f)})
\end{equation}

\begin{equation}
o= \sigma(x_t U^{(o)}, m_{t-1}W^{(o)})
\end{equation}

\begin{equation}
\widetilde {c_t}= \tanh(x_t U^{(g)}, m_{t-1}W^{(g)})
\end{equation}

\begin{equation}
c_t = c_{t-1} \odot f + \widetilde{c_t} \odot i
\end{equation}

\begin{equation}
m_t = tanh(c_t) \odot o
\end{equation}

\begin{itemize}
	\item \textbf{input gate $i^{(t)}$:} The input computes a new cell state based on the current input and the previous hidden state and decides how much of this information to ``let through" via. a sigmoid activation.
	\item \textbf{forget gate $f^{(t)}$:} The forget gate decides what to remember and what to forget for the new memory based on the current input and the previous hidden state. The sigmoid activation acts like a switch where 1 implies remember everything while 0 implies forget everything.
	\item \textbf{output gate $o^{(t)}$:} The output gate then determines determines (via a sigmoid activation) the amount of this internal memory to be exposed to the top layers (and subsequent timesteps) of the network.
	\item \textbf{The input modulation $g^{(t)}$} computed based on the present input and the previous hidden state (which is exposed to the output) yields candidate memory for the cell state via a tanh layer. The hadamard products of input gate and candidate memories is added to the hadamard product of forget gate and previous cell state to yield the new cell state.
	\item \textbf{hidden state $m^{(t)}$:} A hadamard product of the hyperbolic tangent of current cell state and output gate yields the current hidden state.
\end{itemize}

\subsection{GRU}
The Gated Recurrent Unit (GRU) introduced by \cite{GRU} are a new type of gated RNN architecture whose details are as follows:

\begin{equation}
z_t= \sigma(x_t U^{(z)}, m_{t-1}W^{(z)})
\end{equation}

\begin{equation}
r_t= \sigma(x_t U^{(r)}, m_{t-1}W^{(r)})
\end{equation}

\begin{equation}
\widetilde {m_t}= \tanh(x_t U^{(g)},r_t \odot m_{t-1}W^{(g)})
\end{equation}

\begin{equation}
m_t= (1-z_t)m_{t-1} + z_t\widetilde{m_t} %\sigma(x_t U^{(i)} m_{t-1}W^{(i)})
\end{equation}

\begin{itemize}
	\item \textbf{update gate $z_{(t)}$:} The update gate is the filter which decides how much of the activations/memory to be update at any given time step.
	\item \textbf{reset gate $r_{(t)}$:} The reset gate is similar to the forget gate in a LSTM. When its value is close to zero it allows the cell to forget the previously computed state.
	\item \textbf{The input modulation $g_{(t)}$} just as in the case of the LSTM  serves the purpose of yielding candidate memories for the new cell state.
	\item \textbf{hidden state $m_{(t)}$:} The current hidden state is a weighted average of the previous hidden state and the candidate hidden state weighted by ``(1 - update gate)" and the ``(update gate)" respectively.
\end{itemize}

While there are a lot of similarities between a GRU and a LSTM the most striking difference is the lack of an output gate in a GRU. Unlike a LSTM a GRU doesn't control how much of its internal memory to expose to the rest of the units in the network. The GRU therefore has fewer parameters due to the lack of an output gate and is computationally less intensive in comparison to a LSTM.

\section{Seq2Seq Models}\label{background:s2s}
\begin{figure}
	\begin{minipage}[t]{\textwidth}
		\ifpdf
		\includegraphics[width=\linewidth,keepaspectratio=true]{./figs/s2s-pdf}
		\else
		\includegraphics[width=\linewidth,keepaspectratio=true]{./figs/s2s-eps}
		\fi
		\caption{Schematic of a Seq2Seq \citep{manish}}
		\label{bck:s2s}
	\end{minipage}
\end{figure}

Sequence-to-sequence (seq2seq) models introduced by \cite{Sutskever2014}, \cite{GRU} are a class of probabilistic generative models that let us learn the mapping from a variable length input to a variable length output. While initially conceived for machine translation, they have been applied successfully to the tasks of speech recognition, question answering and text summarization \citep{Vinyals2015} \citep{anderson2018bottom} \citep{lu2017knowing}.

Neural networks have been shown to be excellent at learning rich representation from data without the need for extensive feature engineering \citep{Hinton2006}. RNNs are especially adept at learning features and long term dependencies in sequential data (section \ref{RNN}). The simple yet effective idea behind a seq2seq model is learning a fixed size (latent) representation of a variable length input and then generating a variable length output by conditioning it on this latent representation and the previous portion of the output sequence.

\begin{equation} \label{decoder:eqn1}
	h^{(t)} = f(x^{(t)}, h^{(t-1)}, v)
\end{equation}

\begin{equation} \label{decoder:eqn2}
	P(x^{(t+1)}|x^{(t)}, x^{(t-1)},.....,x^{(1)}) = g(x^{(t)}, h^{(t)}, v)
\end{equation}

It can be seen from equation \ref{decoder:eqn2} that the decoder is auto-regressive with the long term temporal dependencies captured in its hidden state. The term $\mathbf{v}$ represents the summary of the entire input state compressed into a fixed length vector (last hidden state of encoder) viz. the \textbf{latent space}. This encoder- decoder network is then jointly trained via. cross entropy loss between the target and the predicted sequence.

\subsection{Se2Seq with Attention}\label{mtv:attn}
It was shown by \cite{Cho2014} that the performance of a basic encoder-decoder model as explained in section \ref{background:s2s} is inversely related to the increase in length of the input sentence. Therefore in lines with concepts of the selective attention and attentional blink in human beings \citep{purves2013principles}, \cite{Bahdanau2014} and later  \cite{Luong2015} showed that soft selection from source states where most relevant information can be assumed to be concentrated during a particular translation step in neural machine translation (NMT) leads to improved performance. In the \cite{Bahdanau2014} framework of attention, the equations \ref{decoder:eqn1} and \ref{decoder:eqn2} are modified as follows:

\begin{equation}\label{attn:eqn1}
h^{(t)} = f(x^{(t)}, h^{(t-1)}, c^{(t)}),
\end{equation}

\begin{equation} \label{attn:eqn2}
P(x^{(t+1)}|x^{(t)}, x^{(t-1)},.....,x^{(1)}) = g(x^{(t)}, h^{(t)}, c^{(t)}).
\end{equation}

Here unlike the traditional seq2seq model the probability of emission of the output at time step $t$ isn't conditioned on a fixed summary representation $\mathbf{v}$ of the input sequence. Rather it is conditioned on a context vector $\mathbf{c^{(t)}}$ which is distinct at each decoding step. The context vector is calculated using a sequence of encoder outputs $(s^1, s^2, ....,s^N)$ to which the the input sequence is mapped such that an encoder output $s^i$ contains a representation of the entire sequence with maximum information pertaining to the $i_{th}$ word in the input sequence. The context vector is then calculated as follows:

\begin{equation}\label{attn:eqn3}
c^{(t)}  = \sum_{j=1}^N \alpha_{tj} s^j,
\end{equation}
where:
\begin{equation}\label{attn:eqn4}
\alpha_{tj} = \text{softmax}(e(h^{(t-1)}, s^j)).
\end{equation}
where $e$ is a alignment/matching/similarity measure between the decoder hidden state $h^{(t-1)}$ i.e. just before the emission of output $x^{(t)}$. The alignment function can be a dot product of the two vectors or a feed-forward network that is jointly trained with the encoder-decoder model. The $\alpha_{tj}$ is an attention vector that weighs the encoder outputs at a given decoding step. \cite{Vaswani2017} view the entire process of attention and context generation as a series of functions applied to the tuple (query, key, value), with the first step being an \textbf{attentive read} step where a scalar matching score between the query and key ($h^{(t-1)}, s^j$) is calculated followed by computation of attention weights $\alpha_{tj}$. Weighted averaged of the \lq values{}\rq\ using the attention weights is then done in the \textbf{aggregation} step.

\section{Pondering}\label{bck:ponder}

%\begin{figure}
%	\begin{minipage}[t]{\textwidth}
%		\ifpdf
%		\includegraphics[width=\linewidth,keepaspectratio=true]{./figs/act-pdf}
%		\else
%		\includegraphics[width=\linewidth,keepaspectratio=true]{./figs/act-eps}
%		\fi
%		\caption{\small Adaptive Computation Time}
%		\label{mtv:ponder}
%	\end{minipage}
%\end{figure}

\begin{figure}[ht]
	\begin{subfigure}[b]{0.5\linewidth}
		\centering
		\includegraphics[width=0.9\linewidth]{./figs/act1-eps}
		\caption{Fixed Computation Time}
		\label{fixed}
		\vspace{4ex}
	\end{subfigure}%%
	\begin{subfigure}[b]{0.5\linewidth}
		\centering
		\includegraphics[width=0.95\linewidth]{./figs/act2-eps}
		\caption{Adpative Computation Time}
		\label{adaptive}
		\vspace{4ex}
	\end{subfigure}
	\caption{\small Schematic of Adaptive Computation Time}
	\label{mtv:ponder}
\end{figure}
The task of positioning a problem and solving belong to different classes of time complexity with the latter requiring more time than the former. \cite{Graves2016} argued that for a given RNN unit it is reasonable to allow for variable computation time for each input in a sequence since some parts of the input might be inherently more complex than the others and thereby require more computational steps. A good example of this would be \textit{spaces between words and ends of sequences}.

Human beings overcome similar obstacles by allocating more time to a difficult problem as compared to a simpler problem. Therefore a naive solution would be to allow a RNN unit to have a large number of hidden state transitions (without penalty on amount of computations performed) before emitting an output on a given input. The network would therefore learn to allocate as much time as possible to minimize its error thereby making it extremely inefficient. \cite{Graves2016} proposed the concept of \textbf{adaptive computation time} to have a trade-off between accuracy and computational efficacy in order to determine the minimum number of state transitions required to solve a problem \footnote{Theoretically this is akin to halting on a given problem or finding the Kolmogorov Complexity of the data, both of which are unsolvable}.

\textbf{A}daptive \textbf{C}omputation \textbf{T}ime (ACT) achieves the above outlined goals by making two simple modifications to a conventional RNN cell, which are presented as follows:

\subparagraph{Sigmoidal Halting Unit} If we revisit the equations of a vanilla RNN from section \ref{RNN}, they can be summarized as:
\begin{equation}
\begin{aligned}
h_t &= f(Ux_t + Wc_{t-1}), \\
y_t &= g(Vc_t).
\end{aligned}
\end{equation}
ACT now allows for \textit{variable state transitions} ($c_t^1, c_t^2,...., c_t^{N(t)}$) and by extension an \textit{intermediate output sequence} ($y_t^1, y_t^2,...., y_t^{N(t)}$) at any given input step \textit{t} as follows:
\begin{equation}
\begin{aligned}
c_t^n &= \begin{cases} f(Ux_t^1 + Wc_{t-1})\ \text{if}\ n = 1 \\ f(Ux_t^n + Wc_t^{n-1})\ \text{if}\ n \neq 1  \end{cases}, \\
y_t^n &= g(Vc_t^n).
\end{aligned}
\end{equation}

A sigmoidal halting unit (with its associated weight matrix $S$) is now added to the network in order to yield a halting probability $p_t^n$ at each state transition as follows:
\begin{equation}
\begin{aligned}
h_t^n &= \sigma(Sc_t^n), \\
p_t^n &= \begin{cases} R(t)\ \text{if}\ n = N(t) \\ h_t^n\ \text{if}\ n \neq N(t)  \end{cases},
\end{aligned}
\end{equation}
where:
\begin{equation}
\begin{aligned}
N(t) &= min\{m : \sum_{n=1}^m h_t^n \geq 1 - \epsilon\}, \\
R(t) &= 1 - \sum_{n=1}^{N(t)-1} h_t^n,
\end{aligned}
\end{equation}
and $\epsilon$ is a small constant.

Each ($n^{th}$) hidden state and output transition at input state \textit{t} are now weighted by the corresponding halting probability $p_t^n$ and summed over all the updates $N(t)$ to yield the final hidden state $c_t$ and output $y_t$ at a given input step. Figure \ref{mtv:ponder} outlines the difference between a standard RNN cell and an ACT RNN cell by showing variable state transitions for input $x$ and $y$ respectively with the corresponding probability associated with each update step. It can be noted that $\sum_{n=1}^{N(t)} p_t^n = 1\ \text{and}\ 0 \leq p_t^n \leq 1\ \forall n$, and therefore, it constitutes a valid probability distribution.

\subparagraph{Ponder Cost} If we don't put any penalty on the number of state transitions then the network would become computationally inefficient and would \lq \textbf{ponder}{}\rq\ for long times even on simple inputs in order to minimize its error. Therefore in order to limit the variable state transitions ACT adds a ponder cost $\mathcal{P}(x)$ to the total loss of the network as follows:
given an input of length $T$ the ponder cost at each time step $t$ is defined as:

\begin{equation}
\rho_t = N(t) +\ R(t)
\end{equation}

\begin{equation}
\begin{aligned}
\mathcal{P}(x) &= \sum_{t=1}^T \rho_t, \\
\widetilde{\mathcal{L}}(x,y) &= \mathcal{L}(x,y) + \tau\mathcal{P}(x),
\end{aligned}
\end{equation}
where $\tau$ is a penalty term hyperparameter (that needs to be tuned) for the ponder loss.

\section{Formal Language Theory}\label{flt}
The field of formal language theory (FLT) concerns itself with the syntactic structure of a formal language (=set of strings) without much emphasis on the semantics. More precisely a formal language $L$ is a set of strings with the constituent units/words/morphemes taken from a finite vocabulary $\Sigma$. It is more apt to define the concept of a formal grammar before proceeding further. A formal grammar $G$ is a quadruple $\langle \Sigma, NT, S, R \rangle$ where $\Sigma$ is a finite vocabulary as previously defined, $NT$ is a finite set of non-terminals, $S$ the start symbol and $R$ the finite set of valid production rules. A production rule can be expressed as $\alpha \rightarrow \beta$ and can be understood as a substitution of $\alpha$ with $\beta$ and $\alpha, \beta$ coming from the following sets for a \textbf{valid} production rule:

\begin{equation}\label{term-nonterm}
	\alpha \in (\Sigma \cup NT)^{*}NT(\Sigma \cup NT)^{*} \qquad \beta \in (\Sigma \cup NT)^{*} \footnote{The \lq *\rq\ denotes Kleene Closure and for a set of symbols say $X$, $X^{*}$ denotes a set of all strings that can be generated using symbols from X, including the empty string $\epsilon$ }.
\end{equation}

From equation \ref{term-nonterm} it is easy to see that the left hand side of the production rule can never be null ($\epsilon$) and must contain at-least one non-terminal. Now a formal language $L(G)$ can be defined as the set of all strings  \textit{generated} by grammar $G$ such that the string consists of morphemes only from $\Sigma$, and has been generated by a finite set of rule ($R$) application after starting from $S$. The \textit{decidability} of a grammar, is the verification (by a Turing machine or another similar computational construct e.g. a finite state automaton (FSA)) of whether a given string has been generated by that grammar or not (the \textit{membership} problem). A grammar is decidable if the membership problem can be solved for all given strings.

\begin{figure}
	\begin{minipage}[t]{0.8\textwidth}
		\ifpdf
		\includegraphics[width=\linewidth,keepaspectratio=true]{./figs/chomsky_h-pdf}
		\else
		\includegraphics[width=\linewidth,keepaspectratio=true]{./figs/chomsky_h-eps}
		\fi
		\caption{\small Chomsky Hierarchy}
		\label{mtv:ch}
	\end{minipage}
\end{figure}

\subsection{Chomsky Hierarchy}\label{flt:ch}
\cite{Chomsky1956} introduced a nested hierarchy for different formal grammars of the form $C_1 \subsetneq C_2 \subsetneq C_3 \subsetneq C_4$ as shown in figure \ref{mtv:ch}. The different classes of grammar are progressively strict subsets of the class just above them in the hierarchy. These classes are not just distinguished by their rules or the languages they generate but also on the computational construct needed to decide the language generated by this grammar. We now take a closer look at the classes in this hierarchy. Please note that for each of these classes the grammar definition is G = $\langle \Sigma, NT, S, R \rangle$.
\subparagraph{Recursively Enumerable} This grammar is characterized by no constraints on the production rules $\alpha \rightarrow \beta$. Therefore any valid grammar is recursively enumerable. The language generated by this grammar is called recursively enumerable language (REL) and is accepted by a Turning Machine.
\subparagraph{Context-Sensitive} This is a grammar in which the left hand side of the production rule i.e. $\alpha$ has the same definition as above (equation \ref{term-nonterm}) but an additional constraint of the form $|\alpha| \leq |\beta|$ is now imposed on the production rules. This is turn leads to $\beta \in (\Sigma \cup NT)^{+}$, i.e. the right hand side of the production rule is now under Kleene Plus closure \footnote{$(\Sigma \cup NT)^{+} = (\Sigma \cup NT)^{*} - \epsilon$}. The non production of $\epsilon$ in context-sensitive grammars poses a problem to the hierarchy because the production of null symbol isn't restricted in its subclasses. While keeping the hierarchy as it is \cite{Chomsky1963} resolved this paradox by defining noncontracting grammmar which is \textit{weakly equivalent}(generates same set of string) to the context sensitive grammar. Noncontracting grammars allow the $S \rightarrow \epsilon$ production. Context sensitive grammars generate context sensitive languages which are accepted by a linear bounded turing machine. While in principle this grammar is decidable, the problem is PSPACE hard and can be so complex, that it is practically intractable \citep{Jager2012}.
\subparagraph{Context-Free} This grammar is described by production rules of the form $A \rightarrow \alpha$ where $A \in NT$ and $\alpha \in (\Sigma \cup NT)^{*}$, such that $|A|=1$.  Context free grammar lead to context free languages (CFL) which are hierarchical in structure, although it is possible that same CFL can be described by different context free grammars, leading to different hierarchical syntactic structures of the language. A CFG is decidable in cubic time of length of string by push down FSA. A push down automaton employs a running stack of symbols to decide its next transition. The stack can also be manipulated as a side effect of the state transition.
\subparagraph{Regular} This grammar is characterized by production rules of the form $A \rightarrow \alpha$ or $A \rightarrow \alpha B$ where $\alpha \in \Sigma^{*}$ and $(A, B) \in NT$. The non terminal in production can therefore be viewed as the next state(s) of a finite state automaton (FSA) while the terminals are the emissions. Regular grammars are decidable in linear time of length of string by an FSA.

\subsection{Subregular Hierarchy}\label{flt:sh}
The simplest class of languages encountered in section \ref{flt:ch} were regular languages that can be described using a FSA. \cite{Jager2012} however argue that the precursor to human language faculty would require lower cognitive capabilities and it stands to reason that even simpler structures can exist in the \lq Regular\rq{} domain. They therefore introduced the concept of subregular languages. If a language can be described by a mechanism even simpler than the FSA then it is a subregular language. While far from the expressive capabilities of regular languages which in turn are the least expressive class in the Chomsky hierarchy, subregular languages provide an excellent benchmark to test basic concept learning and pattern recognition ability of any intelligent system.

\subparagraph{Strictly local languages.} We start with a string $w$ and we are given a lookup table of k-adjacent characters known as \textit{k-factors}, drawn from a particular language. The lookup table therefore serves the role of the language description. A language is \textit{k}-local, if every \textit{k}-factor seen by a \textit{scanner} with a windows of size $k$ sliding over the string $w$, can be found in the aforementioned lookup-table. A $SL_k$ language description, is just the set of k-factors prefixed and suffixed by a start and end symbol, say $\#$. E.g. $SL_2 = \{\#A,AB,BA,B\#\}$

\subparagraph{Locally k-testable languages.} Instead of sliding a scanner over \textit{k}-factors we consider all the \textit{k}-factors to be atomic and build \textit{k}-expression out of them using \textit{propositional logic}. This language description is locally k-testable. As in the case of strictly local languages, scanner of window size $K$ slides over the string and records for every k-factor in vocabulary its occurrence or nonoccurence in the string. The output of this scanner is then fed to a boolean network which verifies the k-expressions. E.g. 2-expression $(\neg \# B) \wedge A$, is a set of strings that doesn't start with B and consists of atleast one A.

\subparagraph{Remarks on Chomsky Hierarchy:}It is easy to see by looking at the production rules of all the grammars in Chomsky Hierarchy that, solving the languages generated by them requires an understanding of these rules. This allows us to create artificial languages such as SCAN \citep{Lake2017} which are context-free, in order to test compositionality in deep neural networks. That said, it is worth noticing that while the grammars in Chomsky Hierarchy are finite, the languages they generate can be infinite. For an infinite language one can argue that a model that can infer the grammar from the given strings is the one that will generalize well to unseen strings. However if the language itself only contains a finite number of strings then although the language itself is compositional, it can be solved also by pure memorization.


\documentclass[11pt,reqno]{amsart}
\usepackage{amsfonts,amssymb,amscd,amsmath,mathrsfs,amsthm}
\usepackage[pdftex]{graphicx}

\setlength{\oddsidemargin}{0.25in}  %please do not change
\setlength{\evensidemargin}{0.25in} %please do not change
\setlength{\marginparwidth}{0in} %please do not change
\setlength{\marginparsep}{0in} %please do not change
\setlength{\marginparpush}{0in} %please do not change
\setlength{\topmargin}{0in} %please do not change

\setlength{\footskip}{.3in} %please do not change
\setlength{\textheight}{8.75in} %please do not change
\setlength{\textwidth}{6in} %please do not change
\setlength{\parskip}{4pt} %please do not change

%
% The following lines set the environments for theorems, propositions, and definitions.
%

\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\newtheorem{prop}{Proposition}

%  Alternatively, can use
%  \newtheorem{thm}{Theorem}[section]
%  to create theorems with numbering depending on the sections.
%
%  \newtheorem{prop}[thm]{Proposition} will generate propositions that use the same counter as theorems.

\theoremstyle{definition}
\newtheorem*{defi}{Definition}

\newcommand{\ab}[2]{\left(\frac{#1}{#2}\right)}

%  Mathematical sets; R, C, ...

\begin{document}

\section{Theorem environments}\label{intro}

\begin{abstract}
This is the abstract for the article.
\end{abstract}

This is an introduction to latex. A file with symbols and an introduction to latex is posted on blackboard. When creating your own latex file for your project, please use the commands before \textbackslash{}begin\{document\}. This will set the margins in such a way that the different files can easily be combined into one file afterwards.

The following are templates for theorems and propositions. Change the newtheorem arguments to create different looks and counters.

\begin{thm}\label{th1} This is a sample theorem.
\end{thm}

\begin{proof} This is an empty proof.
\end{proof}

\begin{defi} This is a definition.
\end{defi}

\begin{prop} This is an example of a proposition.
\end{prop}

\begin{thm}\label{th2} This is the second sample theorem.
\end{thm}

\section{References and counters}

We give a brief example how theorem counters are referred to. We defined theorems \ref{th1}, \ref{th2} in Section \ref{intro}. A counter that is referred to must have been defined with the \textbackslash{}label command at some point in the latex file.

\begin{thm} This is a theorem in the second section. The [section] optional parameter in the environment definition will change the counter so that it depends on the section.
\end{thm}

\end{document}


\documentclass{article}
\usepackage[legalpaper, portrait, margin=1in]{geometry}
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithm}
\usepackage{booktabs}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage[font=small]{caption}
%\usepackage{subcaption}
%\expandafter\def\csname ver@subfig.sty\endcsname{}
\usepackage{tabularx}
\usepackage{subfig}
\usepackage{pgffor}
\usepackage{hyperref}
\usepackage{soul}

% for tables
\newcommand{\centertab}[1]{\multicolumn{1}{|c|}{\textbf{#1} }}
\newcommand{\bigcell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}

\begin{document}
\input{math_definitions}

\section{Time Variable LQR}
\newcommand{\vxt}{\vx_{[t]}}
\newcommand{\vut}{\vu_{[t]}}
\newcommand{\vSt}{\vS_{[t]}}
\newcommand{\vSp}{\vS_{[t+1]}}
\newcommand{\vQt}{\vQ_{[t]}}
\newcommand{\vRt}{\vR_{[t]}}
\newcommand{\vAt}{\vA_{[t]}}
\newcommand{\vBt}{\vB_{[t]}}

\begin{itemize}
  \item System: $\vx_{[t+1]} = \vA_{[t]} \vx_{[t]} + \vB_{[t]} \vu_{[t]}$
  \item Cost:
    \begin{align*}
      J = \sum_{t=1}^T g(\vx_{[t]}, \vu_{[t]})
        = \sum_{t=1}^T \vx_{[t]}^T \vQ_{[t]} \vx_{[t]} + \vu_{[t]}^T \vR_{[t]} \vu_{[t]}
    \end{align*}
  \item Value function:
    \begin{align}
      V(\vx_{[t]}, t) = \min_{\vu_{[t]}}{\sgroup{
          g(\vx_{[t]}, \vu_{[t]})
          + \func{V}{f(\vx_{[t]}, \vu_{[t]}, t) } }} \label{eq:value_function}
    \end{align}
    we assume that the value function takes the following form:
    \begin{align*}
        V(\vx_{[t]}, t) = \vx_{[t]}^T \vS_{[t]} \vx_{[t]}
    \end{align*}
  \item solving Eq. \ref{eq:value_function}:
    \begin{align}
      \vxt^T \vSt \vxt &= \min_{\vut} \vxt^T \vQt \vxt + \vut^T \vRt \vut
          + \func{f}{\vxt, \vut, t}^T \vSp \func{f}{\vxt, \vut, t}
          \label{eq:value_expanded} \\
          &= \min_{\vut} \vxt^T \vQt \vxt + \vut^T \vRt \vut
          + \group{(\vAt \vxt)^T + (\vBt \vut)^T} \vSp \group{\vAt \vxt + \vBt \vut}
           \nonumber
    \end{align}
    dropping time notation, assume everything depends on $t$, while
      $\hat{\vS} = \vSp$

      \renewcommand{\vxt}{\vx}
      \renewcommand{\vut}{\vu}
      \renewcommand{\vSt}{\vS}
      \renewcommand{\vSp}{\hat{\vS}}
      \renewcommand{\vQt}{\vQ}
      \renewcommand{\vRt}{\vR}
      \renewcommand{\vAt}{\vA}
      \renewcommand{\vBt}{\vB}

    \begin{align*}
       Vt =& \vxt^T \vQt \vxt + \vut^T \vRt \vut
             + (\vAt \vxt)^T \vSp (\vAt \vxt)
             + 2 (\vAt \vxt)^T \vSp (\vBt \vut)
             + (\vBt \vut)^T \vSp (\vBt \vut)
    \end{align*}
    solving the minimization problem:
    \begin{align}
      & \dfrac{\partial Vt}{\partial \vut} =
          2 \vRt \vut + 2 \vBt^T \vSp \vAt \vxt + 2 \vBt^T \vSp \vBt \vut = 0 \nonumber \\
      & \underbrace{(\vRt + \vBt^T \vSp \vBt)}_{\vM}\vut =
                        - \underbrace{\vBt^T \vSp \vAt}_{\vC} \vxt  \nonumber \\
      & \vut = - \vM^{-1} \vC \vxt   \label{eq:optimalu}
    \end{align}
    Now we need to solve for $\vSp$ by replacing \ref{eq:optimalu} into
      \ref{eq:value_expanded}:
    \begin{align*}
      \vxt^T \vSt \vxt & =
            \vxt^T \vQt \vxt
          + \vxt^T \vAt^T \vSp \vAt \vxt
          + 2 \vxt^T \underbrace{\group{\vAt^T \vSp \vBt}}_{\vC^T} \vut
          + \vut^T \underbrace{\group{\vRt + \vBt^T \vSp \vBt}}_{\vM} \vut \\
        & =
            \vxt^T \vQt \vxt
          + \vxt^T \vAt^T \vSp \vAt \vxt
          - 2 \vxt^T \vC^T \vM^{-1} \vC \vxt
          + \vxt^T \vC^T \vM^{-T} \vM \vM^{-1} \vC \vxt \\
        & =
            \vxt^T \vQt \vxt
          + \vxt^T \vAt^T \vSp \vAt \vxt
          - \vxt^T \vC^T \vM^{-1} \vC \vxt \\
        & =
            \vxt^T \group{\vQt
                          + \vAt^T \vSp \vAt
                          - \vC^T \vM^{-1} \vC} \vxt
    \end{align*}
    Therefore, we have:
    \begin{align*}
      \vSt & = \vQt + \vAt^T \vSp \vAt
                    - \vC^T \vM^{-1} \vC \\
           & = \vQt + \vAt^T \vSp \vAt
                    - \vAt^T \vSp \vBt \group{\vRt + \vBt^T \vSp \vBt}^{-1} \vBt^T \vSp \vAt
    \end{align*}

\end{itemize}

\end{document}