Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) LogisticGrowthSimulator

 

Logistic Growth Simulator shows population sizes in time for the logistic growth equation. On the press of a button, it also shows a Feigenbaum diagram.

 

Technical facts

 

 

 

 

 

 

./ToolLogisticGrowthSimulator/ProjectLogisticGrowth.cpp

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("UnitFormScatterplot.cpp", FormScatterplot);
USEFORM("UnitFormMain.cpp", FormMain);
USEFORM("UnitFormLineChart.cpp", FormLineChart);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        try
        {
                 Application->Initialize();
                 Application->Title = "Logistic Growth Simulation";
                 Application->CreateForm(__classid(TFormMain), &FormMain);
                 Application->Run();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }
        catch (...)
        {
                 try
                 {
                         throw Exception("");
                 }
                 catch (Exception &exception)
                 {
                         Application->ShowException(&exception);
                 }
        }
        return 0;
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitFormChart.h

 

//---------------------------------------------------------------------------

#ifndef UnitFormChartH
#define UnitFormChartH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Chart.hpp>
#include <ExtCtrls.hpp>
#include <Series.hpp>
#include <TeEngine.hpp>
#include <TeeProcs.hpp>
//---------------------------------------------------------------------------
class TFormLineChart : public TForm
{
__published: // IDE-managed Components
        TChart *mChart;
        TFastLineSeries *Series1;
private: // User declarations
public: // User declarations
        __fastcall TFormLineChart(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormLineChart *FormLineChart;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitFormChart.cpp

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "UnitFormChart.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormLineChart *FormLineChart;
//---------------------------------------------------------------------------
__fastcall TFormLineChart::TFormLineChart(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitFormLineChart.h

 

//---------------------------------------------------------------------------
/*
  Logistic Growth Simulator, logistic growth simulation
  Copyright (C) 2009  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormLineChartH
#define UnitFormLineChartH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Chart.hpp>
#include <ExtCtrls.hpp>
#include <Series.hpp>
#include <TeEngine.hpp>
#include <TeeProcs.hpp>
//---------------------------------------------------------------------------
class TFormLineChart : public TForm
{
__published: // IDE-managed Components
        TChart *mChart;
        TFastLineSeries *Series1;
private: // User declarations
public: // User declarations
        __fastcall TFormLineChart(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormLineChart *FormLineChart;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitFormLineChart.cpp

 

//---------------------------------------------------------------------------
/*
  Logistic Growth Simulator, logistic growth simulation
  Copyright (C) 2009  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitFormLineChart.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormLineChart *FormLineChart;
//---------------------------------------------------------------------------
__fastcall TFormLineChart::TFormLineChart(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitFormMain.h

 

//---------------------------------------------------------------------------
/*
  Logistic Growth Simulator, logistic growth simulation
  Copyright (C) 2009  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormMainH
#define UnitFormMainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Grids.hpp>
#include <ValEdit.hpp>
#include <Chart.hpp>
#include <Series.hpp>
#include <TeEngine.hpp>
#include <TeeProcs.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
#include <memory>
#include "UnitSimulation.h"
class TFormLineChart;
class TFormScatterplot;
//---------------------------------------------------------------------------
class TFormMain : public TForm
{
__published: // IDE-managed Components
        TTrackBar *TrackBar1;
        TValueListEditor *Editor1;
        TButton *ButtonCreateFeigen;
        TButton *ButtonAbout;
        void __fastcall TrackBar1Change(TObject *Sender);
        void __fastcall FormCreate(TObject *Sender);
        void __fastcall ButtonCreateFeigenClick(TObject *Sender);
        void __fastcall ButtonAboutClick(TObject *Sender);
private: // User declarations
        std::auto_ptr<Simulation> mSimulation;
        std::auto_ptr<TFormLineChart> mChartBegin;
        std::auto_ptr<TFormLineChart> mChartEnd;
        std::auto_ptr<TFormLineChart> mChartFeigenNvalues;
        std::auto_ptr<TFormScatterplot> mChartFeigenValues;
public: // User declarations
        __fastcall TFormMain(TComponent* Owner);
        Parameters GetParams() const;
        double GetR() const;
        double GetK() const;
        double GetXzero() const;
        int GetT() const;
};
//---------------------------------------------------------------------------
extern PACKAGE TFormMain *FormMain;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitFormMain.cpp

 

//---------------------------------------------------------------------------
/*
  Logistic Growth Simulator, logistic growth simulation
  Copyright (C) 2009  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "UnitFormMain.h"
//---------------------------------------------------------------------------
#include <map>
#include <algorithm>
#include "UnitFormLineChart.h"
#include "UnitFormScatterplot.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
  : TForm(Owner),
  mChartBegin(new TFormLineChart(0)),
  mChartEnd(new TFormLineChart(0)),
  mChartFeigenNvalues(new TFormLineChart(0)),
  mChartFeigenValues(new TFormScatterplot(0))
{

}
//---------------------------------------------------------------------------
Parameters TFormMain::GetParams() const
{
  Parameters params;
  params.mR = this->GetR();
  params.mK = this->GetK();
  return params;
}
//---------------------------------------------------------------------------
double TFormMain::GetR() const
{
  return static_cast<double>(TrackBar1->Position) / 1000.0;
}
//---------------------------------------------------------------------------
double TFormMain::GetK() const
{
  return 1.0;
}
//---------------------------------------------------------------------------
double TFormMain::GetXzero() const
{
  return 0.1;
}
//---------------------------------------------------------------------------
int TFormMain::GetT() const
{
  return 10000;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::TrackBar1Change(TObject *Sender)
{
  const double r = this->GetR();

  Editor1->Cells[1][1] = r;
  const Parameters params = GetParams();
  const double xZero = this->GetXzero();
  mSimulation.reset(new Simulation(xZero,params));
  const int maxTime = this->GetT();
  mSimulation->Run(maxTime);
  std::vector<double> popSizes = mSimulation->GetPopSizes();
  mChartBegin->mChart->Series[0]->Clear();
  mChartBegin->mChart->Series[0]->AddArray(&popSizes[0],1000-1);
  mChartBegin->mChart->Refresh();
  mChartEnd->mChart->Series[0]->Clear();
  mChartEnd->mChart->Series[0]->AddArray(&popSizes[9000],1000-1);
  mChartEnd->mChart->Refresh();

  std::map<double,char> histogram;
  typedef std::vector<double>::const_iterator Iter;
  for (Iter i = &popSizes[9000]; i!=popSizes.end(); ++i)
  {
    histogram[*i] = '0';
  }
  const int nEquil = histogram.size();
  Editor1->Cells[1][2] = nEquil;

  Refresh();

}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
  const int maxx = 800;
  const int maxy = 600;
  //This -> Topleft
  this->Left   = 0;
  this->Top    = 0;
  this->Width  = maxx / 2;
  this->Height = maxy / 2;

  //Begin -> TopRight
  mChartBegin->Caption = "Begin";
  mChartBegin->Left = maxx / 2;
  mChartBegin->Width = maxx /2 ;
  mChartBegin->Top = 0;
  mChartBegin->Height = maxy / 2;
  mChartBegin->Show();

  //End -> BottomLeft
  mChartEnd->Caption = "End";
  mChartEnd->Left = 0;
  mChartEnd->Width = maxx / 2;
  mChartEnd->Height = maxy / 2;
  mChartEnd->Top = maxy / 2;
  mChartEnd->Show();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonCreateFeigenClick(TObject *Sender)
{
  const int maxx = 800;
  const int maxy = 600;

  //FeigenValues -> BottomRight
  mChartFeigenValues->Caption = "Feigen";
  mChartFeigenValues->Left = 0;
  mChartFeigenValues->Width = maxx / 2;
  mChartFeigenValues->Top = maxy / 2;
  mChartFeigenValues->Height = maxy / 2;

  //Feigen -> BottomRight
  mChartFeigenNvalues->Caption = "Feigen";
  mChartFeigenNvalues->Left = maxx / 2;
  mChartFeigenNvalues->Width = maxx / 2;
  mChartFeigenNvalues->Top = maxy / 2;
  mChartFeigenNvalues->Height = maxy / 2;

  //Calculate the feigenbaum
  {
    for (int i=1; i<4000; ++i)
    {
      Parameters params;
      params.mR = static_cast<double>(i) / 1000.0;
      params.mK = 1.0;
      Simulation sim(0.1,params);
      sim.Run(10000);
      std::vector<double> popSizes = sim.GetPopSizes();
      //How many equilibria does it have?
      std::map<double,char> histogram;
      typedef std::vector<double>::const_iterator Iter;
      for (Iter i = &popSizes[9000]; i!=popSizes.end(); ++i)
      {
        histogram[*i] = '0';
      }

      typedef std::map<double,char>::const_iterator IterMap;
      for (IterMap i = histogram.begin(); i!=histogram.end(); ++i)
      {
        mChartFeigenValues->mChart->Series[0]->AddXY(params.mR, (*i).first);
      }

      const int nEquil = histogram.size();
      mChartFeigenNvalues->mChart->Series[0]->AddXY(params.mR,nEquil);


    }


    Refresh();

  }

  mChartFeigenValues->Show();
  mChartFeigenNvalues->Show();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonAboutClick(TObject *Sender)
{
  ShowMessage(
    "  Logistic Growth Simulator, logistic growth simulation                  \n"
    "  Copyright (C) 2009  Richel Bilderbeek                                  \n"
    "                                                                         \n"
    "  This program is free software: you can redistribute it and/or modify   \n"
    "  it under the terms of the GNU General Public License as published by   \n"
    "  the Free Software Foundation, either version 3 of the License, or      \n"
    "  (at your option) any later version.                                    \n"
    "                                                                         \n"
    "  This program is distributed in the hope that it will be useful,        \n"
    "  but WITHOUT ANY WARRANTY; without even the implied warranty of         \n"
    "  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          \n"
    "  GNU General Public License for more details.                           \n"
    "  You should have received a copy of the GNU General Public License      \n"
    "  along with this program.  If not, see <http://www.gnu.org/licenses/>.  ");


}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitFormScatterplot.h

 

//---------------------------------------------------------------------------
/*
  Logistic Growth Simulator, logistic growth simulation
  Copyright (C) 2009  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormScatterplotH
#define UnitFormScatterplotH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Chart.hpp>
#include <ExtCtrls.hpp>
#include <Series.hpp>
#include <TeEngine.hpp>
#include <TeeProcs.hpp>
//---------------------------------------------------------------------------
class TFormScatterplot : public TForm
{
__published: // IDE-managed Components
        TChart *mChart;
        TPointSeries *Series1;
private: // User declarations
public: // User declarations
        __fastcall TFormScatterplot(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormScatterplot *FormScatterplot;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitFormScatterplot.cpp

 

//---------------------------------------------------------------------------
/*
  Logistic Growth Simulator, logistic growth simulation
  Copyright (C) 2009  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitFormScatterplot.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormScatterplot *FormScatterplot;
//---------------------------------------------------------------------------
__fastcall TFormScatterplot::TFormScatterplot(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitSimulation.h

 

//---------------------------------------------------------------------------
/*
  Logistic Growth Simulator, logistic growth simulation
  Copyright (C) 2009  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitSimulationH
#define UnitSimulationH
//---------------------------------------------------------------------------
#include <vector>
//---------------------------------------------------------------------------
struct Parameters
{
  double mR; //Intrinsic growth rate
  double mK; //Carrying capacity
};
//---------------------------------------------------------------------------
struct Simulation
{
  Simulation(
    const double& initPopSize,
    const Parameters& params);

  const Parameters mParams;

  void Run(const int& nGenerations);

  const std::vector<double>& GetPopSizes() const { return mPopSizes; }

  private:
  std::vector<double> mPopSizes;

  void NextGeneration();
};

#endif

 

 

 

 

 

./ToolLogisticGrowthSimulator/UnitSimulation.cpp

 

//---------------------------------------------------------------------------
/*
  Logistic Growth Simulator, logistic growth simulation
  Copyright (C) 2009  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#pragma hdrstop

#include "UnitSimulation.h"

//---------------------------------------------------------------------------
Simulation::Simulation(
  const double& initPopSize,
  const Parameters& params)
  : mParams(params)
{
  mPopSizes.push_back(initPopSize);
}
//---------------------------------------------------------------------------
void Simulation::Run(const int& nGenerations)
{
  while (static_cast<int>(mPopSizes.size()) < nGenerations) NextGeneration();
}
//---------------------------------------------------------------------------
void Simulation::NextGeneration()
{
  const double popNow = mPopSizes.back();
  const double popNext = mParams.mR * popNow * (mParams.mK - popNow);
  mPopSizes.push_back(popNext);
}
//---------------------------------------------------------------------------
#pragma package(smart_init)

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml