tsgenepool__define.bak

来自「采用蚁群算法解决旅行商问题」· BAK 代码 · 共 53 行

BAK
53
字号
FUNCTION TsGenePool::CreateRandomChromosome
	;function randomly sorts the gene and Returns the new order
	index = Sort(Randomu(seed,self.numGenes))
	xPos = (*self.x)[index]
	yPos = (*self.y)[index]
	names = (*self.names)[index]
	;create the new chromosome from the reorderd positions
	randomChromosome = Obj_New('tsChromosome',
								numGenes=self.numGenes, $
								xPos=xPos,
								yPos=yPos,
								names=names
								)

	Return,randomChromosome
END

;{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|

FUNCTION TsGenePool::Init, numGenes
	;create the gene pool with a set of random positions
	;After they are created the positions never change, just the traversal order.
	self.numGenes = numGenes
	self.x = Ptr_New(Randomu(s,numGenes)*100.)
	self.y = Ptr_New(Randomu(s,numGenes)*100.)
	;when mating two chromosomes we need to be able to identIFy each gene.
	;The next line creates random 10 character strings as the "city names"
	self.names = Ptr_New(String(Byte(Randomu(seed,10,numGenes)*26 + 97.0)))

	Return,1
END
;{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|

PRO TsGenePool::cleanup
	;free the pointers
	IF Ptr_Valid(self.x) 	 THEN Ptr_Free,self.x
	IF Ptr_Valid(self.y) 	 THEN Ptr_Free,self.y
	IF Ptr_Valid(self.names) THEN Ptr_Free,self.names

	Return
END

;{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|

PRO TsGenePool__Define

	void = { TsGenePool				,$
	         numGenes 	: 0L		,$ ;number of genes
	         x 			: Ptr_New()	,$ ;x position
	         y 			: Ptr_New()	,$ ;y position
	         names 		: Ptr_New() } ;unique name for identification
	Return
END

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?