13.3.12. Macro “samplingConstrLHSLinear.C

13.3.12.1. Objective

This macro shows the usage of the TConstrLHS class when one wants to create a constrained LHS with three linear constraints. In order to illustate the concept, it is applied on three input variables drawn from uniform distribution with well-thought boundaries (as the concept of the LHS is to have nicely distributed marginals).

13.3.12.2. Macro Uranie

#include "ConstrFunctions.C"

void nicegrid(TDataServer *tds, TPad *Can)
{
    int ns=tds->getNPatterns(), nx=tds->getNAttributes();
    tds->drawPairs();
    TH1F *h[nx];
    TAttribute *att=NULL;
    gStyle->SetOptStat(0);
    for(int iatt=0; iatt<nx; iatt++)
    {
        Can->GetPad(iatt*(nx+1)+1)->cd();
        att=tds->getAttribute(iatt);
        h[iatt] = new TH1F(Form("%s_histo",att->GetName()), Form("%s;x_%i",att->GetName(), iatt), ns, att->getLowerBound(), att->getUpperBound());
        tds->Draw(Form("%s>>%s_histo", att->GetName(), att->GetName()),"","goff");
        h[iatt]->Draw();
    }
}

void samplingConstrLHSLinear(const string& figure="figure.png", const string& style="", int seed=0)
{
    // Canvas to produce the 2x1 plot to compare LHS designs
    TCanvas *Can = new TCanvas("Can","Can", 10, 32, 1400, 1000);
    TPad *apad = new TPad("apad", "apad", 0, 0.03, 1, 1);
    apad->Draw();
    apad->cd();

    int ns = 250, nx = 3; // Size of the samples to be produced
    // Create dataserver and define the attributes
    TDataServer *tds = new TDataServer("tds","pouet");
    for(int iatt=0; iatt<nx; iatt++)
    {
        tds->addAttribute( new TUniformDistribution(Form("x_%i",iatt),0,iatt+1) );
        tds->getAttribute(Form("x_%i",iatt))->delShare();
    }

    // Generate the constr lhs
    TConstrLHS *constrlhs = new TConstrLHS(tds, ns);
    vector<int> inputs = {1,0,2,1};
    constrlhs->addConstraint(Linear, 2, inputs.size(), &inputs[0]);
    constrlhs->generateSample();

    // Do the plot
    nicegrid(tds, apad);
}

The very beginning of these macros is the nicegrid method which is here only to show the nice marginal distributions and the scatter plots. One can clearly skip this part to focus on the rest in the main function.

The macro very much looks like any other design-of-experiments generating macro above: the dataserver is created along with the canvas object and the problem is defined along with the input variables and the number of locations to be produced. Once done, then the TConstrLHS instance is created with the four following lines:

    // Generate the constr lhs
    TConstrLHS *constrlhs = new TConstrLHS(tds, ns);
    vector<int> inputs = {1,0,2,1};
    constrlhs->addConstraint(Linear, 2, inputs.size(), &inputs[0]);
    constrlhs->generateSample();

The constructor is pretty obvious, as it takes only the dataserver object and the number of locations. Once created the main method to be called is the addConstraint function which has been largely discussed in TConstrLHS example. The first argument of this method is the pointer to the C++ function which has been included in our macro through the very first line:

#include "ConstrFunctions.C"

which contains the Linear function. The rest of the argument are the number of constraints, the size of the list of parameters and its content. Finally the nicegrid method is called to produce the nice plot shown in Figure 13.15

13.3.12.3. Graph

../../_images/samplingConstrLHSLinear.png

Figure 13.15 Graph of the macro “samplingConstrLHSLinear.C”