knitr::opts_chunk$set(echo = TRUE)
suppressMessages(library("findWeb"))

An example

Some general remark: The algorithms used relies heavily on optimization. Therefore it is not guranteed that the algorithms will find always a errorfree solution (even when you can spot the errorfree solution).

The Leisepark in Berlin

Part of the package is a data set about portals in the Leisepark in Berlin, Germany (Ingress intel map).

  library("findWeb")
  data(leisepark)
  head(leisepark)

Step 1: Compute xy portal positions from latitude annd longitude and a web

Convert the latitudes and longitudes to xy-coordinates

  xy <- ll2xy(leisepark$lon, leisepark$lat)
  head(xy)

Next, we will create as target linking structure a fishbone (or herringbone) linking structure.

  g <- fishbone(8)
  plot(g)

The fishbone(8) structure consists of nine portals, twentyone links and 19 fields. It was one of the popular linking structures since the EXO5 Controller event in October 2017 since it is simple. And in any case no Softbank Ultra Link is necessary to build it.

Step 2: Create an initial link structure

Then we have to create a starting link structure.

  g0 <- web(g, xy)
  plot(g0)

There are two requirements for a perfect link structure:

The second condition means that for the fishbone(n) the largest field should contain at least n-2 portals. The error value used is the number of link intersections plus number of portals for each field missing to the target link structure.

Step 3: Optimize the link structure by program

The next step is an optimization of the start link structure. The optimizeWeb generates two (at start and end) or even more plots.

Note: the random seed needs to be fixed since the portal positions are generated randomly and the optimization algorithm uses a random search.

  set.seed(1)
  g1 <- optimizeWeb(g0)

In this case the optmization algorithm has found a solution with no errors. It is not guranteed that the program will find a link structure with no errors!

Since the a random search algorithm is used you may repeat the search several times and may become a different solution.

  set.seed(0)
  g11 <- optimizeWeb(g0)

Step 4: Optimize the link structure by hand

However, the solution the program found may not be optimal: the outer portals are far away from each other.

  g2 <- swap(g1, 42, 16)
  g2 <- swapi(g1)  # swap 42-16
  plot(g2)
  plot(g2)

What can I do if the program does not find a solution and neither me, too?

There are two parameters you may modify

  set.seed(0)
  g12 <- optimizeWeb(g0, maxit=1000000) # default: 100000
  g02 <- web(g, xy, rot=1024) # default: 256
  plot(g02)

In general the optimization algorithm will honor it if you have more portals available than your target structure requires.

Creating a link plan

As the optimization algorithm the link plan created by the software should only be starting point for a real linking plan.

For a single agent (n=1)

Once you are created a target link structure you need to know which portals to visit and how to link such that you get the maximum number of fields.

We follow the half-plan approach as shown in the video of Michael Hartley: A Fully General Ingress Maxfields Algorithm. The screen shot shows an agent who has a general walking direction east south east (= -22.5 degree or approx -0.0175 radian).

We use the first linking structure which as been created for the Leisepark

  g2 <- linkPlan1(g1)
  # agent comes from -1.877423 radian or -107 degree or south south west
  plot(g2)

The agent starts at portal blue 9, walks to portal blue 8, ... until finally he ends up at portal blue 2, following the pink line.

You may visually check the path and the link plan by (press enter after each agent move)

  getPlan(g2, quiet=TRUE)

A parameter "wait"" determines how the plan is visualised. A negative value (default) means continue after keypress, a positive value continues after wait seconds and a zero does not plot the plan at all.

Now lets head the agent mainly in direction east, coming from the west (= -180 degree = -3.1415 radian)

  g2 <- linkPlan1(g1, dir=-pi)
  # agent comes from direction west and walks to the east
  plot(g2)

First create your link plan again:

 g3 <- linkPlan1(g1)
 plot(g3)
 ```

View the pathes, the links and the keys (and softbank ultralinks) are required to build the structure:

```r
 getPlan(g3, wait=0)
 getPlan(g3, names=leisepark$shortname, wait=0)

For more than one agent (n>=1)

For creating a link plan for several agents we follow a different approach. We do it the japanese way starting from the center and building the fields to the outer.

 

set.seed(0)
n <- 17
x <- cbind(rnorm(n), rnorm(n))
i <- which.min(rowSums(x^2))
r <- max(rowSums(x^2))
x <- scale(x, center=x[i,], scale=FALSE)
plot(x, pch=19, asp=TRUE, axes=FALSE, xlab="", ylab="", col=1+((1:n)==i))
box()
a <- atan2(x[,2], x[,1])
qa <- quantile(a, c(0.5-1/3, 0.5, 0.5+1/3))
for(i in 1:3) lines(c(0, r*cos(qa[i])), c(0, r*sin(qa[i])), col="red")

From a starting portal (red) we decompose the area in n sectors (here n=3 sectors). Each of the n agents goes to the portals in his sector and links back to all the portals all the agents have visited already.

NOTE: Is is not clear if this approach leads always to the maximum field number!

A cobweb in the Leisepark

As a first step we will create a cobweb with three portals at each leg

g <- cobweb(3)
plot(g)

Then find a solution at the Leisepark

g0 <- web(g, xy)
set.seed(1)
g1 <- optimizeWeb(g0)
plot(g1)

Finally we create a link plan for three agents

g2 <- linkPlanN(g1, agents=3)
plot(g2)

and look for the linking and the keys

getPlan(g2, wait=1) 

Of course, a single agent could have done it alone ;)

g4 <- linkPlan1(g1, direction=-pi)
getPlan(g4, wait=1) 

Helpful informations

Plotting

The plot command offers additional parameters which are set on TRUE by default.

plot(g4, blue=FALSE) # do not plot the blue numbers
plot(g4, black=FALSE) # do not plot the black numbers
plot(g4, links=FALSE) # do not plot the links
plot(g4, pathes=FALSE) # do not plot the agent pathes

Using the forward pipe operator

To simplify the typing you may use the forward pipe operator %>% of the library magrittr. Instead of writing

g0 <- fishbone(8)
g1 <- web(g0, xy)
g2 <- optimizeWeb(g1)
g3 <- linkPlan1(g2)
plot(g3, blue=FALSE)

you may use

library("magrittr") # for %>% operator
fishbone(8) %>% web(xy) %>% optimizeWeb() %>% linkPlan1() %>% plot(blue=FALSE)

Or alternatively

g <- fishbone(8) %>% web(xy) %>% optimizeWeb() %>% linkPlan1()
plot(g, blue=FALSE)

Thanks

Finally, a thanks to Stefan Dirsch for testing the R package, Michael Hartley for his videos and the authors of R package FNN: Fast Nearest Neighbor Search Algorithms and Applications ;)

Enjoy planing and linking, agents!



SigbertIngress/findWeb documentation built on May 26, 2019, 4:38 p.m.