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;
}
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)