13.8.3. Macro “reoptimizeHollowBarCodevizir.C

13.8.3.1. Objective

The objective of the macro is to optimize the section and distortion of the hollow bar defined in Sizing of a hollow bar example problem using the evolutionary solvers. This can be done with different solvers, the one chosen here being the TVizirGenetic one.

13.8.3.2. Macro Uranie

    using namespace URANIE::DataServer;
    using namespace URANIE::Relauncher;
    using namespace URANIE::Reoptimizer;

    // variables
    TAttribute x("x", 0.0, 1.0),
      y("y", 0.0, 1.0),
      thick("thick"), // thickness
      sect("sect"), // section of the pipe
      dist("dist"); // distortion

    // Creating the TCodeEval, dumping output of the dummy python in an output file
    string python_exec = "python3";
    if(string(gSystem->GetBuildArch()) == "win64")
        python_exec.pop_back();
    TCodeEval code((python_exec + " bar.py > bartoto.dat").data());

    // Pass the python script itself as a input file. x and y will be modified in bar.py directly
    TKeyScript inputfile("bar.py");
    inputfile.addInput(&x,"x");
    inputfile.addInput(&y,"y");
    code.addInputFile(&inputfile);

    // precise the name of the output file in which to read the three output variables
    TFlatResult outputfile("bartoto.dat");
    outputfile.addOutput(&thick);
    outputfile.addOutput(&sect);
    outputfile.addOutput(&dist);
    code.addOutputFile(&outputfile);

    // Create a runner
    TSequentialRun runner(&code);
    runner.startSlave(); // Usual Relauncher construction

    if(runner.onMaster())
    {
        // Create the TDS
        TDataServer tds("vizirDemo", "Param de l'opt vizir pour la barre");
        tds.addAttribute(&x);
        tds.addAttribute(&y);

        // create the vizir genetic solver
        TVizirGenetic solv;
        // Set the size of the population to 150, and a maximum number of evaluation at 15000
        solv.setSize(200,15000);

        // Create the multi-objective constrained optimizer
        TVizir2 opt(&tds, &runner, &solv);

        // add the objective
        opt.addObjective(&sect); // minimizing the section
        opt.addObjective(&dist); // minimizing the distortion

        // and the constrains
        TGreaterFit positiv(0.4);
        opt.addConstraint(&thick,&positiv);  //on thickness (thick > 0.4)

        opt.solverLoop(); // running the optimization

        // Stop the slave processes
        runner.stopSlave();

        TCanvas *fig1 = new TCanvas("fig1","Pareto Zone",5,64,1270,667);
        int phi=12;  int theta=30;
        TPad *pad1 = new TPad("pad1","",0,0.03,1,1);
        TPad *pad2 = new TPad("pad2","",0,0.03,1,1);
        pad2->SetFillStyle(4000); //will be transparent

        pad1->Draw(); pad1->Divide(2,1); pad1->cd(1); gPad->SetPhi(phi); gPad->SetTheta(theta);

        gStyle->SetLabelSize(0.03); gStyle->SetLabelSize(0.03,"Y"); gStyle->SetLabelSize(0.03,"Z");

        tds.getTuple()->Draw("sect:y:x");
        //Get the TH3 to change Z axis color
        TH3F *htemp = (TH3F*)gPad->GetPrimitive("htemp");
        htemp->SetTitle("");
        htemp->GetZaxis()->SetLabelColor(2); htemp->GetZaxis()->SetAxisColor(2); htemp->GetZaxis()->SetTitleColor(2);

        fig1->cd();
        pad2->Draw();
        pad2->Divide(2,1);
        pad2->cd(1); gPad->SetFillStyle(4000); gPad->SetPhi(phi); gPad->SetTheta(theta);
        tds.getTuple()->SetMarkerColor(4);
        tds.getTuple()->Draw("dist:y:x");
        htemp = (TH3F*)gPad->GetPrimitive("htemp");
        htemp->SetTitle("");
        htemp->GetZaxis()->SetLabelColor(4); htemp->GetZaxis()->SetAxisColor(4); htemp->GetZaxis()->SetTitleColor(4);
        htemp->GetZaxis()->SetTickSize( -1*htemp->GetZaxis()->GetTickLength() );
        htemp->GetZaxis()->SetLabelOffset( -15*htemp->GetZaxis()->GetLabelOffset() );
        htemp->GetZaxis()->LabelsOption("d");
        htemp->GetZaxis()->SetTitleOffset( -1.5*htemp->GetZaxis()->GetTitleOffset() );
        htemp->GetZaxis()->RotateTitle( );

        pad2->cd(2);
        tds.getTuple()->SetMarkerColor(2);
        tds.draw("dist:sect");
    }

The variables are defined as follow:

    // variables
    TAttribute x("x", 0.0, 1.0),
      y("y", 0.0, 1.0),
      thick("thick"), // thickness
      sect("sect"), // section of the pipe
      dist("dist"); // distortion

where the first two are the input ones while the last ones are computed using the provided code (as explained in Sizing of a hollow bar example problem). This code is configured through these lines

    // Creating the TCodeEval, dumping output of the dummy python in an output file
    TCodeEval code((python_exec + " bar.py > bartoto.dat").data());

    // Pass the python script itself as a input file. x and y will be modified in bar.py directly
    TKeyScript inputfile("bar.py");
    inputfile.addInput(&x,"x");
    inputfile.addInput(&y,"y");
    code.addInputFile(&inputfile);

    // precise the name of the output file in which to read the three output variables
    TFlatResult outputfile("bartoto.dat");
    outputfile.addOutput(&thick);
    outputfile.addOutput(&sect);
    outputfile.addOutput(&dist);
    code.addOutputFile(&outputfile);

The usual Relauncher construction is followed, using a TSequentialRun runner and the solver is chosen in these lines

        // create the vizir genetic solver
        TVizirGenetic solv;
        // Set the size of the population to 150, and a maximum number of evaluation at 15000
        solv.setSize(200,15000);

Combining the runner, solver and dataserver, the master object is created and the objective and constraint are defined. This is done in:

        // Create the multi-objective constrained optimizer
        TVizir2 opt(&tds, &runner, &solv);

        // add the objective
        opt.addObjective(&sect); // minimizing the section
        opt.addObjective(&dist); // minimizing the distortion

        // and the constrains
        TGreaterFit positiv(0.4);
        opt.addConstraint(&thick,&positiv);  //on thickness (thick > 0.4)

Finally the optimisation is launched and the rest of code is providing the graphical result shown in next section.

13.8.3.3. Graph

../../_images/reoptimizeHollowBarCodeVizir.png

Figure 13.56 Graph of the macro “reoptimizeHollowBarCodeVizir.C”