Lars Relund Nielsen

112 posts

Accessing an array created in R from C++

Assume that you have created a 3-dim array in R containing vectors of various size: > a<-array(list(),c(2,2,2))   # array where each element contain a numeric vector > a[[1,1,1]]<-rnorm(1) > a[[1,1,2]]<-rnorm(3) > a[[2,1,1]]<-rnorm(2) > a[[2,1,2]]<-rnorm(3) > a[[1,2,1]]<-rnorm(4) > a[[1,2,2]]<-rnorm(3) > a[[2,2,1]]<-rnorm(5) > a[[2,2,2]]<-rnorm(1) > d = dim(a) > d [1] 2 2 2 > i<-1; j<-1; k<-2 > q<-i + j*d[1] + k*(d[1]*d[2]) – (d[1] + d[1]*d[2]) > a[[i,j,k]]      # access an element [1] -0.2370040 -0.6009635  3.0550405 > a[[q]]          # access the same element using a single index [1] -0.2370040 -0.6009635  3.0550405 Note you can access the array in two ways. Next you want to copy the array to a vector on the C++ side. For instance in a package you may need to do some operations in C++ to speed up time. On the C++ side you can create a function:

EURO Summer Institute 2009 (ESI 2009)

Aim of ESI 2009 is to bring together young scientists with academic experts on OR methods besides the development of applications for Agriculture, Forestry and related industries of the primary sector. Anders Ringgaard Kristensen and I will the first day talk about Markov decision processes (MDPs) for sequential decision problems in agriculture and forestry. First the theory of MDPs will be presented and next I will give an introduction to my new MDP package in R. The files needed for the exercises can be found here.

Optimering af foderplanlægning med NorFor Plan

NorFor Plan, der er et system til rationsoptimering, inddrager ikke de økonomiske sammenhænge på den enkelte bedrift og dermed optimering af fodringsøkonomien i den enkelte besætning. Økonomisk optimering er vigtig i en tid, hvor der er stigende fokusering på effektivisering i malkekvægsbesætninger og større udsving i priser på foder og mælk. I 2008 har jeg været involveret i et udredningsarbejde, som skulle opstille en anbefaling for udvikling af et beslutningsstøtteværktøj til økonomisk foderoptimering i den enkelte besætning baseret på NorFor Plan. Resultatet af arbejdet er en intern rapport.

Highlighting R code in LaTeX using SweaveListingUtils

It is well known that you can use Sweave to integrate R code into a LaTeX document. However, how do we highlight R code in LaTeX? LaTeX got its own powerful package listings that can highlight source code from various languages. Moreover, the package SweaveListingUtils provides utilities for defining R as a listings “language”. To highlight your R code in your Sweave document you have to include the following code chunk:

Using an array for storing an object in R

Often you need fast access/lookup in R. For instance assume that you have the following data frame/matrix dat >head(dat) x y z [1,] 1 1 1 0.46 -0.89 0.88 -0.21 0.46 0.93 1.06 [2,] 1 1 2 0.72 -0.40 1.71 -0.52 0.95 NA NA [3,] 1 1 3 -0.49 -0.42 -0.26 -0.06 1.07 1.48 1.08 [4,] 1 1 4 -1.55 -0.90 0.15 -0.60 1.86 -1.15 NA [5,] 1 1 5 -0.46 -1.54 -0.40 NA NA NA NA [6,] 1 1 6 0.14 1.13 -2.42 0.86 0.13 -1.60 0.62 x, y and z represent an unique index and the rest of the columns represent a vector. The vectors may be of various length. To find the vector corresponding to index (x,y,z) you can do a normal search:

Kick-off meeting – The Intelligent Farrowing Pen

Today I am participating in a kick-off meeting of the project ‘The intelligent Farrowing Pen’. The four-year project is supported by Højteknologifonden, The Faculty of Agricultural Sciences, Skov A/S and Danish Pig Production. The goal is to develop a system whereby the condition and activity of the animals will be registered. For example, previous studies have shown that the sow is very active in the 24 hours prior to farrowing. She stands up and lies down often and spends time building a nest. This knowledge can be used to predict when she will farrow within a narrow margin. When the system predicts when the sow is expected to farrow, a message is given to the farmer and to a climate control system that ensures that there is more heat in the pen. Not only does this save piglets. It could perhaps also save heating bills in the farrowing barn, which […]

Dag om anvendelser af OR

Den 25 november 2008 afholder DORS en dag om anvendelser af OR på Handelshøjskolen i Århus, hvor jeg deltager med en præsentation. Sted: R –Bygningen, ASB – det gamle Musik Konservatorie Program 9:30 – 10:00 Ankomst, kaffe og rundstykker 10:00 – 10:15 Velkomst ved DORS formand Julie Groth 10:15 – 11:00 Niels Christian Petersen, Institut for Sundhedstjenesteforskning, SDU: “Håndtering af forskelle i omgivelserne i Data Envelopment Analysis” 11:00 – 11:45 Johan Kjeldgaard‐Pedersen, PA Consulting: “OR IRL” 11:45 – 12:15 Lars Relund, Research Centre Foulum: “En Markov beslutningsproces anvendt til beregning af optimale udskiftningsstrategier for malkekøer” 12:15 – 13:00 Frokost 13:00 – 14:00 Arne Andersson, Trade Extensions: About OR in Game Theory 14:00 – 14:30 Jørgen Bang‐Jensen, imada, SDU: ”Eksempler på praktiske anvendelser af kombinatorisk optimering” 14:30 – 15:00 Jakob Skov, B&O: ” Skedulering af et Anodiseringsanlæg” 15:00 – 15:30 Kaffe og kage 15:30 – 16:00 Rune Larsen, imada, SDU: ”Optimaring […]

OR50 conference

The 9.-11. September I have been participating in OR50 – The UK OR Society’s 50’th conference in York.  The conference was held at the University of York’s beautiful lakeside campus. I gave a talk in the agricultural stream about ‘Optimal replacement policies for dairy cows based on daily yield measurements’.