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

📄 crossover_2p

📁 在一个任意的天线方向图下
💻
字号:
#!/usr/bin/perl -wuse POSIX;$population_size = 20;$bit_mutation_probability = 0.01;# Generate an initial population of random individuals. Each individual is# represented by a 16-bit number.for ($i = 0; $i < $population_size; $i++) {# Generate a random number between 0 and 65535 (2^16) Store the individuals# into an array (population) of hashes. The hash element that we initialise# is called 'value'.	$population[$i]{'value'} = int (rand (65536));}$N = 0;while (1) {	print STDERR "Iteration: $N\n";# Go through all individuals in the current population. Simulate and calculate MSE.	for ($i = 0; $i < $population_size; $i++) {# Determine dimensions of left and right dipole segments		$right = $population[$i]{'value'} & 0x00FF;		$left = $population[$i]{'value'} >> 8;#		print STDERR "Dimensions: $left, $right\n";# Generate NEC files to calculate horizontal and vertical radiation patterns		`bits2nec.m horiz $left $right > /tmp/horiz.nec`;		`bits2nec.m vert $left $right > /tmp/vert.nec`;# Simulate these using NEC		`nec2++ -i /tmp/horiz.nec`;		`nec2++ -i /tmp/vert.nec`;# Extract radiation patterns for calculation of MSE		`nec2plot /tmp/horiz.out > /tmp/horiz.dat`;		`nec2plot /tmp/vert.out > /tmp/vert.dat`;		# Calculate MSE (both horizontal and vertical)		$h_mse = `calc_mse.m /tmp/horiz.dat ~/reference/horizontal_reference.dat`;		$v_mse = `calc_mse.m /tmp/vert.dat ~/reference/vertical_reference.dat`;# Remove trailing newlines				chomp $h_mse;		chomp $v_mse;		#		print STDERR "Mean squared errors (h, v) = $h_mse, $v_mse\n";# Store result in $population[$i]{'mse'}		$population[$i]{'mse'} = $h_mse + $v_mse;		print STDERR "Evaluated " . ($i + 1) . " of $population_size\n";	}	@sorted = sort { ${$a}{'mse'} <=> ${$b}{'mse'}} @population;		$rank = 1;	$new_pop_size = 0;		print STDERR "Current lowest MSE = $sorted[0]{'mse'}\n";		$average_mse = 0;		foreach (@sorted) {		${$_}{'rank'} = $rank++;		${$_}{'rank fitness'} = 2 * ($population_size - ${$_}{'rank'}) / ($population_size - 1);		${$_}{'copies'} = floor (${$_}{'rank fitness'} + rand (1));#		print STDERR "${$_}{'mse'}, copies = ${$_}{'copies'}\n";		$average_mse += ${$_}{'mse'};				for ($j = 0; $j < ${$_}{'copies'}; $j++) {			$intermediate[$new_pop_size++] = ${$_}{'value'};		}	}	$average_mse /= $population_size;	print $N . "\t" . $average_mse . "\t" . $sorted[0]{'mse'} . "\t" . $sorted[$population_size - 1]{'mse'} . "\n";	#	print STDERR "Intermediate population is now $new_pop_size\n";		$i = 0;		foreach (@intermediate) {				$partner = $intermediate[floor (rand ($new_pop_size))];		print STDERR "$_ paired with $partner\n";		# So now we have two individuals selected from this list; $_ and $partner.# We need to combine these using a crossover method		$crossover_point_2 = 1 + floor (rand (15));		$crossover_point_1 = floor (rand ($crossover_point_2 - 1));		$mask_1 = 0xFFFF >> $crossover_point_1;		$mask_2 = 0xFFFF >> $crossover_point_2;		$front = $_ & ~$mask_1;		$back = $_ & $mask_2;		$new_population[$i]{'value'} = $front + $back + ($partner & ($mask_1 & ~$mask_2));		print STDERR "Crossover at $crossover_point_1, $crossover_point_2\n";				print STDERR "Child = $new_population[$i]{'value'}\n";		$i++;	}	for ($n = 0; $n < $new_pop_size; $n++) {		print STDERR ("Before: $new_population[$n]{'value'}, After: ");# For each bit, there is a certain probability that the bit will be inverted		for ($i = 0; $i < 16; $i++) {			if (rand (1) < $bit_mutation_probability) {				$new_population[$n]{'value'} ^= (1 << $i);			}		}		print STDERR ("$new_population[$n]{'value'}\n");	}	@population = @new_population;	$population_size = $new_pop_size;	$N++;}

⌨️ 快捷键说明

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