⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.mxml

📁 遗传算法,Flex做的算法.比较能够说明算法的精确性
💻 MXML
字号:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

<mx:Style source="styles/style.css" />

<mx:Script>
	<![CDATA[
		import mx.managers.CursorManager;
		import mx.formatters.DateFormatter;
		import com.mh.genetics.GeneticAlgorithm;
		
		[Bindable]
		private var algoritm:GeneticAlgorithm;
		
		[Bindable]
		private var isRunning:Boolean = false;
		
		private var df:DateFormatter = new DateFormatter();
		
		
		private function init():void
		{
			loadInstructions();
			algoritm = new GeneticAlgorithm( n_population.value, n_length.value, n_crossover.value, n_mutation.value, n_width.value, n_height.value, 0, n_height.value/2, cb_obstacle.selected );
			df.formatString = "LL:NN:SS";
		}
		
		private function loadInstructions():void
		{
			var request:URLRequest = new URLRequest("instructions.txt");
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,instructionsLoaded);
			
			loader.load(request);
		}
		
		private function instructionsLoaded(e:Event):void
		{
			var loader:URLLoader = e.target as URLLoader;
			if( loader != null )
				t_instructions.htmlText = loader.data;
		}

		private function toggleInstructions():void
		{
			if( currentState == null )
				currentState = "no_instructions";
			else
				currentState = null;
		}
		
		
		private function initialiseAlgorithm():void
		{
			log("Start initializing algorithm");
			
			c_background.width = n_width.value;
			c_background.height = n_height.value;
			
			algoritm = new GeneticAlgorithm( n_population.value, n_length.value, n_crossover.value, n_mutation.value, n_width.value, n_height.value, 0, n_height.value/2,cb_obstacle.selected );
			algoritm.init();
			
			log("Initialization complete");
			
			b_ns.enabled = true;
			b_rf.enabled = true;
			
			visualiseAlgorithm();
		}
		
		
		private function visualiseAlgorithm():void
		{
			log("Start displaying chromosomes");
			
			algoritm.visualise(c_playingField.graphics);
			
			log("Displaying chromosomes complete");
		}
		
		
		private function nextStep( cycles:Number ):void
		{			
			log("Start next cycle ("+algoritm.chromosomes.length+")");
			
			for( var i:int = 0; i<cycles; i++ )	
				algoritm.nextStep();
			
			log("Cycle complete complete ("+algoritm.chromosomes.length+")");
			
			visualiseAlgorithm();
		}

		
		private function log(message:String):void
		{
			message = df.format(new Date())+"> " + message;
			trace(message);
		}
		
	]]>
</mx:Script>


<!-- STATES -->
	<mx:states>
		<mx:State name="no_instructions">
			<mx:SetProperty target="{c_instructions}" name="height" value="28"/>
			<mx:SetProperty target="{b_instructions}" name="label" value="show"/>
		</mx:State>
	</mx:states>
	
	
	
<!-- EFFECTS -->
<mx:Resize id="e_resize" duration="1000" />
<mx:Move id="e_move" duration="1000" /> 


<!-- INTERFACE -->
	<mx:VBox horizontalCenter="0" y="10" verticalGap="10" horizontalAlign="center">
	
		<mx:Panel width="800" height="300" layout="absolute" title="Population" >	
		
			<mx:VBox width="100%" height="100%" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
				<mx:TabNavigator id="tabnavigator2" creationPolicy="all" width="100%" height="100%" >
				
			<!-- POPULATION DATAGIRD -->
					<mx:Canvas label="Individuals" width="100%" height="100%">
						<mx:DataGrid x="0" y="0" width="100%" height="100%" dataProvider="{algoritm.chromosomes}">
							<mx:columns>
								<mx:DataGridColumn headerText="" dataField="generation" width="30" />
								<mx:DataGridColumn headerText="DNA" dataField="dna"/>
								<mx:DataGridColumn headerText="Fitness" dataField="fitness" width="60" />
							</mx:columns>
						</mx:DataGrid>
					</mx:Canvas>
					
		
			<!-- POPULATION VISUALIZATION -->
					<mx:Canvas id="c_visualization" label="Visualization" width="100%" height="100%">
					
						<mx:Canvas id="c_background" width="0" height="0" horizontalCenter="0" verticalCenter="0" borderStyle="none" backgroundColor="#c0c0c0" backgroundAlpha="1">
							
							<mx:Canvas id="c_playingField" width="100%" height="100%" backgroundAlpha="0"	backgroundColor="#ffffff">
							</mx:Canvas>
							
						</mx:Canvas>
						
					</mx:Canvas>
				</mx:TabNavigator>
				
			</mx:VBox>
			
		</mx:Panel>
		
		
	<!-- INFORMATION PANEL -->
		<mx:Canvas id="c_instructions" width="800" height="200" resizeEffect="e_resize">
		
			<mx:Panel title="Instructions" width="800" height="100%" id="panel1">
				<mx:TextArea width="100%" height="100%" borderStyle="none" wordWrap="true" editable="false" id="t_instructions"/>
			
							
				
			</mx:Panel>
			
			<mx:Button top="4" right="10" label="hide" click="toggleInstructions()" id="b_instructions" width="40"/>
		</mx:Canvas>
		
		
		<mx:HBox horizontalGap="10">
		
	<!-- SETTINGS PANEL -->
			<mx:Panel width="395" height="220" layout="absolute" title="Settings" >
				<mx:VBox width="100%" height="100%"  paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10" horizontalAlign="left">
				
					<mx:FormItem label="Width" labelWidth="130" >
						<mx:NumericStepper width="80" id="n_width" value="500" maximum="600" minimum="100" stepSize="5"/>						
					</mx:FormItem>
					
					<mx:FormItem label="Height" labelWidth="130" >
						<mx:NumericStepper width="80" id="n_height" value="150" minimum="20" maximum="200" stepSize="5"/>						
					</mx:FormItem>
					
					<mx:FormItem label="Population" labelWidth="130" >
						<mx:NumericStepper width="80" id="n_population" value="200" minimum="10" maximum="800" stepSize="10"/>						
					</mx:FormItem>
					
					<mx:FormItem label="Chromosome Length" labelWidth="130" >
						<mx:NumericStepper width="80" id="n_length" maximum="1200" minimum="50" value="1000" stepSize="10"/>						
					</mx:FormItem>
					
					<mx:FormItem label="Crossover Chance" labelWidth="130" >
						<mx:NumericStepper width="80" id="n_crossover" value="0.01" minimum="0" maximum="0.2" stepSize="0.005"/>						
					</mx:FormItem>
					
					<mx:FormItem label="Mutation Chance" labelWidth="130" >
						<mx:NumericStepper width="80" id="n_mutation" value="0.005" minimum="0" maximum="0.2" stepSize="0.001"/>						
					</mx:FormItem>
					
				</mx:VBox>
				
				<mx:CheckBox id="cb_obstacle" label="Obstacle" selected="false" bottom="40" height="18" left="245" width="107"/>
				<mx:Button id="b_init" label="Initialise" click="initialiseAlgorithm()" bottom="10" height="22" left="245" width="120"/>
			</mx:Panel>
	
	
	<!-- CONTROLS PANEL -->			
			<mx:Panel width="395" height="220" layout="absolute" title="Controls" >
				<mx:VBox width="100%" height="100%"  paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10" >
				
					<mx:Button id="b_ns" label="Next Step" width="130" click="nextStep(1)" enabled="{algoritm.isInitialised &amp;&amp; !isRunning}" />
					
					<mx:HBox id="b_rf" verticalAlign="middle" enabled="{algoritm.isInitialised &amp;&amp; !isRunning}" >
						<mx:Button label="Run For" width="130" click="{nextStep(ns_cycles.value)}" />
						<mx:NumericStepper value="10" maximum="100" minimum="2" stepSize="1" id="ns_cycles" />
						<mx:Label text="steps" />						
					</mx:HBox>
					
				</mx:VBox>
				
			</mx:Panel>
			
		</mx:HBox>
		
	</mx:VBox>
	
	
	
</mx:Application>

⌨️ 快捷键说明

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