Variable and function description


 
Thread Tools Search this Thread
Top Forums Programming Variable and function description
# 1  
Old 06-16-2011
Variable and function description

I have a C++ project and want to describe the variables and functions in the code.
A small example is shown below. I am wondering where to best describe the variables and functions. I have made a UML diagram in the beginning.

There are several options

1. In the UML diagram in the beginning.
2. Just after showing the UML diagram.
3. In the declaration of the variables and functions.
4. In the implementation for functions

Any suggestions please, as I'd like a coherent way of doing this, which will help the programmer use the class in the easiest way possible.




Code:
class Darwin
//
//    Genetic algorithm based on Darwinian evolution
//
//  +-Module---------------------------------------------------------+
//  |                             ObjFunc                            |
//  |-Attribute------------------------------------------------------|
//  |                                                                |
//  |  # _ <<const>> KPop: short = 10                                |
//  |  # _ <<const>> DefSel: short = 0                               |
//  |  # _ <<const>> DefBaseCO: short = 2                            |
//  |  # _ <<const>> DefCO: short = 3                                |
//  |  # _ <<const>> DefLambdaC: REAL = 1.0                          |
//  |  # _ <<const>> DefLambdaS: short = 0                           |
//  |  # _ <<const>> DefLambdaM: REAL = 0.1                          |
//  |                                                                |
//  |  # NPop: int                                                   |
//  |  # MaxIter: int                                                |
//  |  # Sel: short                                                  |
//  |  # LambdaS: short                                              |
//  |  # BaseCO: short                                               |
//  |  # CO: short                                                   |
//  |  # LambdaC: REAL                                               |
//  |  # LambdaM: REAL                                               |
//  |                                                                |
//  |-Operations-----------------------------------------------------|
//  |                                                                |
//  |  # <<const>> CrossOver(P1: const Model&, P2: const Model&,     |
//  |      C1: Model&, C2: Model&): void                             |
//  |  # Mutate(mod: Model&, mis: REAL&): void                       |
//  |  # SearchBest(): void                                          |
//  |  + Darwin(ObjFunc *base)                                       |
//  |  + SetStd(std: const bool): void                               |
//  |  + SetBaseCO(baseco: const short): void                        |
//  |  + SetNPop(npop: const int): void                              |
//  |  + SetMaxIter(maxiter: const int): void                        |
//  |  + SetElitism(elitism: const bool): void                       |
//  |                                                                |
//  +----------------------------------------------------------------+

//  DefSel      Sets the default selection method.
//  DefBaseCO   Sets the default chromosome representation.
//  DefLambdaC  Sets the default crossover probability.
//  DefCO       Sets the default crossover method.
//  DefLambdaS  Sets the selection probability.
//  DefLambdaM  Sets the default mutation probability.
//  DefElitism  Sets the default elitism flag.

//  RMS         RMS Misfit Value of the Population
//  Tol         Minimum tolerance.
//  MedVal      Medium value on population parameters

  int  NPop;                 // Number of individuals in the population
  int  MaxIter;              // Number of generation to perform
  short  Sel;
  short  LambdaS;
  short  BaseCO;
  short  CO;
  REAL  LambdaC;
  REAL  LambdaM;
  REAL  MedVal;
  REAL  RMS;
  REAL  Tol;
  bool  Std;                 // True for Standard Darwin, False for Genitor
  bool  Mini;                // True in Minimization, False in Maximization
  bool  Elitism;             // True if Elitism is to be performed
  Matrix<REAL>  Pop;         // Population of sound speed models
  Vector<REAL>  PopVal;      // Misfit value of each individual

#ifndef DARWIN_H
#define DARWIN_H

#include "dynstr.h"
#include "random.h"
#include "verbose.h"
#include "vector.h"
#include "matrix.h"
#include "optimize.h"

// ******************************************************************
// ***** CLASS: Darwin **********************************************
// ******************************************************************

class Darwin : public Optimization {

protected:

  static const short  KPop = 10;          // Population factor
  static const short  DefSel = 0;
  static const short  DefBaseCO = 2;
  static const short  DefCO = 3;
  static const REAL  DefLambdaC = 1.0;
  static const short  DefLambdaS = 0;
  static const REAL  DefLambdaM = 0.1;
  static const REAL  KExpand = 1.5;
  static const bool  DefStd = false;
  static const bool  DefElitism = true;
  static const unsigned long BMax = 0xFFFFFFFF;

  int  NPop;                 // Number of individuals in the population
  int  MaxIter;              // Number of generation to perform
  short  Sel;
  short  LambdaS;
  short  BaseCO;
  short  CO;
  REAL  LambdaC;
  REAL  LambdaM;
  REAL  MedVal;
  REAL  RMS;
  REAL  Tol;
  bool  Std;                 // True for Standard Darwin, False for Genitor
  bool  Mini;                // True in Minimization, False in Maximization
  bool  Elitism;             // True if Elitism is to be performed
  Matrix<REAL>  Pop;         // Population of sound speed models
  Vector<REAL>  PopVal;      // Misfit value of each individual

  // ------------------------------------------------------------------
  // Creat Children by Combining Two Parents (CrossOver)
  void
  CrossOver(
    const Model&  P1,
    const Model&  P2,
    Model&  C1,
    Model&  C2 ) const;

  // ------------------------------------------------------------------
  // Mutates an individual by altering one or more genes in a chromosome
  void
  Mutate(
    Model&  mod,
    REAL&  mis );

  REAL
  GetPhi(
    const REAL&  value ) const;

  bool
  IsValid(
    const REAL&  v ) const;

  void
  SearchBest();

  bool
  IsBetter(
    const REAL&  v1,
    const REAL&  v2 ) const;

  REAL
  Subst(
    REAL  dm ) const;

  // ------------------------------------------------------------------
  // Population Statistics at each Generation
  void
  Statistics();

public:

  // Initialization List
  Darwin(ObjFunc *base) :
      Std(DefStd),
      Sel(DefSel),
      BaseCO(DefBaseCO),
      CO(DefCO),
      LambdaS(DefLambdaS),
      LambdaC(DefLambdaC),
      LambdaM(DefLambdaM),
      Elitism(DefElitism),
      MaxIter(0),
      Tol(0.0),
      Optimization(base) {

      NPop = KPop * NPar;         // Sets the population size.

  }

  // ------------------------------------------------------------------
  // Set whether to use a Standard Darwin or a Genitor Genetic Algorithm
  void
  SetStd(
    const bool  std );

  // ------------------------------------------------------------------
  // Set the Chromosome Representation (2 for binary coding)
  void
  SetBaseCO(
    const short  baseco );

  // ------------------------------------------------------------------
  // Set the number of individuals (Population of sound speed models)
  void
  SetNPop(
    const int  npop );

// ------------------------------------------------------------------
// Set the maximum number of generations
  void
  SetMaxIter(
    const int  maxiter );

  // ------------------------------------------------------------------
  // True if Elitism is allowed. Select an individual with a bias towards
  // the better chromosomes.
  void
  SetElitism(
    const bool  elitism );

  // ---------------------------------------------------------------------
  // Set the parent selection method (Random or Roulette)
  void
  SetSel(
    const short  sel );

  // ---------------------------------------------------------------------
  // Set the selection probability (Choose a chromosome from the current
  // generation's population for inclusion in the next generation's population.
  void
  SetLambdaS(
    const short  lambdas );

  // ---------------------------------------------------------------------
  // Set the Crossover Method to Use
  void
  SetCO(
    const short  co );

  // ---------------------------------------------------------------------
  // Set the Crossover Probability
  void
  SetLambdaC(
    const REAL  lambdac );

  // ---------------------------------------------------------------------
  // Set the mutation probability (an arbitrary bit is changed)
  void
  SetLambdaM(
    const REAL  lambdam );

  // ---------------------------------------------------------------------
  // Set the Minimum Tolerance
  void
  SetTol(
    const REAL  tol );

  // ---------------------------------------------------------------------
  // Performs minimization using a genetic algorithm
  void
  Minimize(
    const Verbose  Vrb );

  // ---------------------------------------------------------------------
  // Performs maximization using a genetic algorithm
  void
  Maximize(
    const Verbose  Vrb );

  // ---------------------------------------------------------------------
  // Performs a Darwinian genetic algorithm (standard)
  void
  Standard(
    const Verbose  Vrb );

  // ---------------------------------------------------------------------
  // Performs a Genitor genetic algorithm
  void
  Genitor(
    const Verbose  Vrb );

  // ---------------------------------------------------------------------
  // Saves the current population models and fitness values in a backup file
  void
  Backup() const;

  // ---------------------------------------------------------------------
  // Restores population models and misfit values by reading a backup file
  void
  Restore(
    istream&  is );

};

// ***********************************************************************
// Implementation: Darwin
// ***********************************************************************

// Public Member Functions

void
Darwin::SetStd(
  const bool  std ) {

  Std = std;

}

//////////////////////////////////////////////////////////////////////////

void
Darwin::SetSel(
  const short  sel ) {

  Sel = sel;

}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from one function to another function?

updateEnvironmentField() { linewithoutquotes=`echo $LINE | tr -d '"'` b() } I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script (1 Reply)
Discussion started by: pottic
1 Replies

2. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

3. Programming

Tags for variable and function with the same name

I ran into a slight problem with vi jumping to a variable definition and not function when both have the same name. Does anybody know if I can influence ctags to give preference to function over the variable? Details of my setup: in $MYLIB/myedi.h I have a struct with short procname in... (1 Reply)
Discussion started by: migurus
1 Replies

4. Shell Programming and Scripting

function for variable files

hi all this is my function #! /bin/sh awk '/ Type/ { print "m;" $4 } /IDf/ {print $3 } /IuSac/ { print $3 } /IuSac/ { print $1 }' / IBM.txt |tr '\n' ';'| perl -pi -e 's/;m//g'|cut -d ";" -f 2-5 ' >> 2m 1) i wanna make it to save output of awk in a file named by date in order not to... (3 Replies)
Discussion started by: teefa
3 Replies

5. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

6. UNIX for Dummies Questions & Answers

function not see variable in script

Hi Forum Can anyone tell me whats wrong with my script. What i want to do read in values from a input file using a while loop then taking that input from the file into a function that i created. Every time i execute the script it goes through the while loop but the function doesn't see the... (10 Replies)
Discussion started by: ShinTec
10 Replies

7. Shell Programming and Scripting

Variable value not retaining outside function

Hi All, As per my understanding, value of variable is retained outside function. But the value of array myarrayDriver is not retained outside function. Could you please tell the reason for the same.. code: readingConfigFile() { search_keyword="$1" i=0 for pointer in $(cat... (7 Replies)
Discussion started by: ajincoep
7 Replies

8. UNIX for Dummies Questions & Answers

Calling a function through a variable

Hey folks, I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected. #!/bin/ksh . ./functions.sh STRING=function_1 FUNCTION="$STRING" RETURN=eval $FUNCTION echo "value of $FUNCTION function is: $RETURN" All i'm... (5 Replies)
Discussion started by: Irrational
5 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

10. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies
Login or Register to Ask a Question