GAの実行
Namespace: fvalgcliAssembly: fvalgcli (in fvalgcli.dll) Version: 3.1.0.0 (3.1.0.11)
Syntax
C# |
---|
public static int fnFIE_ga_execute( FHANDLE hga, int pick_num, IntPtr chrom, DOUBLE_PTR fitness, ref int generate ) |
Visual Basic |
---|
Public Shared Function fnFIE_ga_execute ( hga As FHANDLE, pick_num As Integer, chrom As IntPtr, fitness As DOUBLE_PTR, ByRef generate As Integer ) As Integer |
Parameters
- hga
- Type: fvalgcli..::..FHANDLE
GAのハンドル
- pick_num
- Type: System..::..Int32
出力する染色体の個数(1以上)
- chrom
- Type: System..::..IntPtr
適応度の大きい順に並んだ染色体の配列です。 「一遺伝子あたりのバイト数×一染色体あたりの遺伝子数× pick_num 」分の領域が必要です。 遺伝子の型により、以下の様に必要なサイズが変わります。- 整数型( F_GA_INT_TYPE_GENE ) sizeof(INT) × 一染色体あたりの遺伝子数× pick_num
- 浮動小数点型( F_GA_FLOAT_TYPE_GENE ) sizeof(DOUBLE) × 一染色体あたりの遺伝子数× pick_num
- 順序型( F_GA_ORDER_TYPE_GENE ) sizeof(INT) × 一染色体あたりの遺伝子数× pick_num
- fitness
- Type: fvalgcli..::..DOUBLE_PTR
抽出した各染色体の適用度の配列。 pick_num で指定した個数分の領域が必要です。
- generate
- Type: System..::..Int32%
世代交代を行った回数
Return Value
Type: Int32以下のエラーコードを返します。
エラーコード:
f_err | 内容 |
---|---|
F_ERR_NONE | 正常終了 |
F_ERR_INVALID_OBJECT | 不正なオブジェクトが渡された |
F_ERR_INVALID_PARAM | 不正なパラメータが渡された |
F_ERR_NOMEMORY | メモリ不足エラー |
F_ERR_NO_LICENCE | ライセンスエラー、または未初期化エラー |
Remarks
Examples
C# | Copy |
---|---|
// $Revision: 1.1 $ using System; using System.Collections.Generic; using System.Text; using fvalgcli; namespace TC { public partial class FIE { [FvPluginExecute] public void fnFIE_ga_open() { int status = (int)fvalgcli.f_err.F_ERR_NONE; FHANDLE hGa = FHANDLE.Zero; // Descriptor of GA F_GA_RANGE_PTR taGeneRange = F_GA_RANGE_PTR.Zero; // Range of value for every gene INT_PTR iaChrom = INT_PTR.Zero; // Essence of chromosome DOUBLE_PTR daFitness = DOUBLE_PTR.Zero; // Fitness PNT_T_PTR taCoord = PNT_T_PTR.Zero; // Set of coordinates (positions of cities) double dExpArrage = 0.75; // Expected fitness value (>0.75) const int GENE_N = 20; // Maximal number of genes const int PICK_N = 3; // Number of chrosomoses picked out try { // allocate memory taGeneRange = F_GA_RANGE_PTR.alloc(GENE_N); iaChrom = INT_PTR.alloc(PICK_N * GENE_N); daFitness = DOUBLE_PTR.alloc(PICK_N); taCoord = PNT_T_PTR.alloc(GENE_N); /***** Parameters of GA function *****/ int iGeneNum = GENE_N; int iMaxAlterCnt = 1000; f_ga_type tGeneType = f_ga_type.F_GA_ORDER_TYPE; for (int i = 0; i < iGeneNum; i++) { F_GA_VALUE ga_min = F_GA_VALUE.init(0); F_GA_VALUE ga_max = F_GA_VALUE.init(0); taGeneRange[i] = F_GA_RANGE.init(ga_min, ga_max); } /* Cost function */ F_GA_FUNC fpCostFunc = cost_func; // Cost function /* Set the fixed value */ int iChromNum = 200; /* Number of chromosomes */ f_ga_select tSelect = f_ga_select.F_GA_PROPORT_SELECT; /* Method of seletion */ f_ga_cross tCrossover = f_ga_cross.F_GA_PARTIAL_CROSS; /* Method of crossover */ double dCrossRate = 0.9; /* Rate of crossover */ double dMutatRate = 0.05; /* Rate of mutation */ f_ga_alter tAlternate = f_ga_alter.F_GA_ELITE_ALTERNATE; /* Method of alternation */ f_ga_end tEndCond = f_ga_end.F_GA_NO_CONDITION_END; /* End condition */ double dThresh = 0.001; /* Threshold of end condition */ /* Create the coordinates of positions */ for (int i = 0; i < iGeneNum; i++) taCoord[i] = PNT_T.init(20*i+5, i); // generate table of distance generate_table( iGeneNum, taCoord ); /****** GA Test begin ******/ /* GA open */ hGa = api.fnFIE_ga_open( tGeneType, iGeneNum, iChromNum, taGeneRange, tSelect, tCrossover, dCrossRate, dMutatRate, tAlternate, fpCostFunc, tEndCond, dThresh, iMaxAlterCnt ); Assert.IsTrue(hGa != FHANDLE.Zero, "エラーが発生しました。"); m_dMaxFitness = 0.0; /* GA execute */ int iRet = 0; status = api.fnFIE_ga_execute(hGa, PICK_N, iaChrom, daFitness, ref iRet); Assert.IsTrue(status == (int)f_err.F_ERR_NONE, "エラーが発生しました。({0})", (f_err)status); for ( int i = 0; i < PICK_N; i++ ) { if ( daFitness[i] < dExpArrage ) { Assert.Fail("エラーが発生しました。(daFitness[{0}]={1})", i, daFitness[i]); } } } finally { hGa.Dispose(); taGeneRange.Dispose(); iaChrom.Dispose(); daFitness.Dispose(); taCoord.Dispose(); } } /// <summary> /// Cost function of genetic algorithm /// </summary> /// <param name="tGeneType">Type of gene</param> /// <param name="iGeneNum">Number of gene</param> /// <param name="vpChrom">Descriptor of chromosome</param> /// <returns> /// </returns> private static double cost_func(f_ga_type tGeneType, int iGeneNum, IntPtr vpChrom) { double dFitness, dMax, dLength; int iLength = 0; INT_PTR iaChrom = vpChrom; for (int i = 0; i < iGeneNum; i++) iLength += m_waLength[iaChrom[i],iaChrom[(i+1)%iGeneNum]]; dLength = (double)iLength; dMax = (double)(iGeneNum * 500); if( dMax < dLength ) dFitness = 0.0; else dFitness = 1.0 - dLength / dMax; if( m_dMaxFitness < dFitness ) m_dMaxFitness = dFitness; return dFitness; } /// <summary> /// Table of distance (distance of cities) /// </summary> private static int[,] m_waLength = new int[20, 20]; /// <summary> /// Maximal value of fitness /// </summary> private static double m_dMaxFitness = 0.0; /// <summary> /// Generate the distance table /// </summary> /// <param name="iDataNum">Input data num</param> /// <param name="taCoord">Coordinate of point</param> private static void generate_table(int iDataNum, PNT_T_PTR taCoord) { for( int i = 0; i < iDataNum; i++ ) for( int j = 0; j < iDataNum; j++ ) m_waLength[i, j] = calc_length(taCoord[i].x, taCoord[i].y, taCoord[j].x, taCoord[j].y); } /// <summary> /// Calculate the diatance of two points /// </summary> /// <param name="iXs">X-coordinate of start point</param> /// <param name="iYs">Y-coordinate of start point</param> /// <param name="iXe">X-coordinate of end point</param> /// <param name="iYe">Y-coordinate of end point</param> /// <returns> /// </returns> private static int calc_length(int iXs, int iYs, int iXe, int iYe) { int iXd = iXe - iXs + 1; int iYd = iYe - iYs + 1; return ((int)System.Math.Sqrt((double)(iXd * iXd + iYd * iYd))); } } } |
Visual Basic | Copy |
---|---|
' $Revision: 1.1 $ Imports System.Collections.Generic Imports System.Text Imports fvalgcli Public Partial Class FIE <FvPluginExecute> _ Public Sub fnFIE_ga_open() Dim status As Integer = CInt(fvalgcli.f_err.F_ERR_NONE) Dim hGa As FHANDLE = FHANDLE.Zero ' Descriptor of GA Dim taGeneRange As F_GA_RANGE_PTR = F_GA_RANGE_PTR.Zero ' Range of value for every gene Dim iaChrom As INT_PTR = INT_PTR.Zero ' Essence of chromosome Dim daFitness As DOUBLE_PTR = DOUBLE_PTR.Zero ' Fitness Dim taCoord As PNT_T_PTR = PNT_T_PTR.Zero ' Set of coordinates (positions of cities) Dim dExpArrage As Double = 0.75 ' Expected fitness value (>0.75) Const GENE_N As Integer = 20 ' Maximal number of genes Const PICK_N As Integer = 3 ' Number of chrosomoses picked out Try ' allocate memory taGeneRange = F_GA_RANGE_PTR.alloc(GENE_N) iaChrom = INT_PTR.alloc(PICK_N * GENE_N) daFitness = DOUBLE_PTR.alloc(PICK_N) taCoord = PNT_T_PTR.alloc(GENE_N) '**** Parameters of GA function **** Dim iGeneNum As Integer = GENE_N Dim iMaxAlterCnt As Integer = 1000 Dim tGeneType As f_ga_type = f_ga_type.F_GA_ORDER_TYPE For i As Integer = 0 To iGeneNum - 1 Dim ga_min As F_GA_VALUE = F_GA_VALUE.init(0) Dim ga_max As F_GA_VALUE = F_GA_VALUE.init(0) taGeneRange(i) = F_GA_RANGE.init(ga_min, ga_max) Next ' Cost function Dim fpCostFunc As F_GA_FUNC = AddressOf cost_func ' Cost function ' Set the fixed value Dim iChromNum As Integer = 200 ' Number of chromosomes Dim tSelect As f_ga_select = f_ga_select.F_GA_PROPORT_SELECT ' Method of seletion Dim tCrossover As f_ga_cross = f_ga_cross.F_GA_PARTIAL_CROSS ' Method of crossover Dim dCrossRate As Double = 0.9 ' Rate of crossover Dim dMutatRate As Double = 0.05 ' Rate of mutation Dim tAlternate As f_ga_alter = f_ga_alter.F_GA_ELITE_ALTERNATE ' Method of alternation Dim tEndCond As f_ga_end = f_ga_end.F_GA_NO_CONDITION_END ' End condition Dim dThresh As Double = 0.001 ' Threshold of end condition ' Create the coordinates of positions For i As Integer = 0 To iGeneNum - 1 taCoord(i) = PNT_T.init(20 * i + 5, i) Next ' generate table of distance generate_table(iGeneNum, taCoord) '***** GA Test begin ***** ' GA open hGa = api.fnFIE_ga_open(tGeneType, iGeneNum, iChromNum, taGeneRange, tSelect, tCrossover, _ dCrossRate, dMutatRate, tAlternate, fpCostFunc, tEndCond, dThresh, _ iMaxAlterCnt) Assert.IsTrue(hGa <> FHANDLE.Zero, "エラーが発生しました。") m_dMaxFitness = 0.0 ' GA execute Dim iRet As Integer = 0 status = api.fnFIE_ga_execute(hGa, PICK_N, iaChrom, daFitness, iRet) Assert.IsTrue(status = CInt(f_err.F_ERR_NONE), "エラーが発生しました。({0})", CType(status, f_err)) For i As Integer = 0 To PICK_N - 1 If daFitness(i) < dExpArrage Then Assert.Fail("エラーが発生しました。(daFitness[{0}]={1})", i, daFitness(i)) End If Next Finally hGa.Dispose() taGeneRange.Dispose() iaChrom.Dispose() daFitness.Dispose() taCoord.Dispose() End Try End Sub ''' <summary> ''' Cost function of genetic algorithm ''' </summary> ''' <param name="tGeneType">Type of gene</param> ''' <param name="iGeneNum">Number of gene</param> ''' <param name="vpChrom">Descriptor of chromosome</param> ''' <returns> ''' </returns> Private Shared Function cost_func(tGeneType As f_ga_type, iGeneNum As Integer, vpChrom As IntPtr) As Double Dim dFitness As Double, dMax As Double, dLength As Double Dim iLength As Integer = 0 Dim iaChrom As INT_PTR = vpChrom For i As Integer = 0 To iGeneNum - 1 iLength += m_waLength(iaChrom(i), iaChrom((i + 1) Mod iGeneNum)) Next dLength = CDbl(iLength) dMax = CDbl(iGeneNum * 500) If dMax < dLength Then dFitness = 0.0 Else dFitness = 1.0 - dLength / dMax End If If m_dMaxFitness < dFitness Then m_dMaxFitness = dFitness End If Return dFitness End Function ''' <summary> ''' Table of distance (distance of cities) ''' </summary> Private Shared m_waLength As Integer(,) = New Integer(19, 19) {} ''' <summary> ''' Maximal value of fitness ''' </summary> Private Shared m_dMaxFitness As Double = 0.0 ''' <summary> ''' Generate the distance table ''' </summary> ''' <param name="iDataNum">Input data num</param> ''' <param name="taCoord">Coordinate of point</param> Private Shared Sub generate_table(iDataNum As Integer, taCoord As PNT_T_PTR) For i As Integer = 0 To iDataNum - 1 For j As Integer = 0 To iDataNum - 1 m_waLength(i, j) = calc_length(taCoord(i).x, taCoord(i).y, taCoord(j).x, taCoord(j).y) Next Next End Sub ''' <summary> ''' Calculate the diatance of two points ''' </summary> ''' <param name="iXs">X-coordinate of start point</param> ''' <param name="iYs">Y-coordinate of start point</param> ''' <param name="iXe">X-coordinate of end point</param> ''' <param name="iYe">Y-coordinate of end point</param> ''' <returns> ''' </returns> Private Shared Function calc_length(iXs As Integer, iYs As Integer, iXe As Integer, iYe As Integer) As Integer Dim iXd As Integer = iXe - iXs + 1 Dim iYd As Integer = iYe - iYs + 1 Return CInt(Math.Truncate(System.Math.Sqrt(CDbl(iXd * iXd + iYd * iYd)))) End Function End Class |