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

📄 crossover_uniform_nasa

📁 在一个任意的天线方向图下
💻
字号:
#!/usr/bin/perl -wuse POSIX;$population_size = 1000;$bit_mutation_probability = 0.01;$nsegments = 5;$nbits = $nsegments * 24;$iterations = 500;# 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'.		for ($n = $nbits - 1; $n >= 0; $n--) {		$population[$i]{'value'}[$n] = floor (0.5 + rand (1));		print STDERR "$population[$i]{'value'}[$n]";	}		print STDERR "\n";}for ($N = 0; $N < $iterations; $N++) {	print STDERR "Iteration: $N\n";# Go through all individuals in the current population. Simulate and calculate MSE.	$str = "";	for ($i = 0; $i < $population_size; $i++) {# Generate NEC files to calculate horizontal and vertical radiation patterns		for ($j = 0; $j < $nbits; $j++) {			$str .= $population[$i]{'value'}[$j];		}		$str .= ' '	}	`bits2nec_nasa_fast.m $str`;	for ($i = 0; $i < $population_size; $i++) {# Generate NEC files to calculate horizontal and vertical radiation patterns# Simulate these using NEC# Extract radiation patterns for calculation of MSE# Calculate MSE (both horizontal and vertical)		$str = sprintf ('nec2++ -i /tmp/horiz_%i.nec ; nec2plot /tmp/horiz_%i.out > /tmp/horiz_%i.dat ; calc_mse.m /tmp/horiz_%i.dat ~/reference/horizontal_reference.dat', $i, $i, $i, $i);		$h_mse = `$str`;		$str = sprintf ('nec2++ -i /tmp/vert_%i.nec ; nec2plot /tmp/vert_%i.out > /tmp/vert_%i.dat ; calc_mse.m /tmp/vert_%i.dat ~/reference/vertical_reference.dat', $i, $i, $i, $i);		$v_mse = `$str`;# 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'};		}	}	#	foreach (@intermediate) {#		@code = @$_;		#		foreach (@code) {#			print $_#		}		#		print "\n";#	}	$average_mse /= $population_size;	for ($i = 0, $best_code = ""; $i < $nbits; $i++) {		$best_code .= $sorted[0]{'value'}[$i];	}	print $N . "\t" . $average_mse . "\t" . $sorted[0]{'mse'} . "\t" . $sorted[$population_size - 1]{'mse'} . "\t" .  $best_code . "\n";	#	print STDERR "Intermediate population is now $new_pop_size\n";		$i = 0;		foreach (@intermediate) {				@partner1 = @$_;		@partner2 = @{$intermediate[floor (rand ($new_pop_size))]};#		foreach (@partner1) {#			print $_#		}		#		print "\n";#		foreach (@partner2) {#			print $_#		}		#		print "\n";# Now we have two individuals selected from this list; $_ and $partner. We# need to combine these using a crossover method# Generate random number		for ($j = $nbits - 1; $j >= 0; $j--) {			$selector = floor (0.5 + rand (1));			$new_population[$i]{'value'}[$j] = ($selector & $partner1[$j]) + (~$selector & $partner2[$j]);		}		$i++;	}	#	print "New population:\n\n";	#	foreach (@new_population) {#		@code = @{${$_}{'value'}};#		foreach (@code) {#			print $_;#		}#		print "\n";#	}# Now randomly mutate bits in individuals	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 < $nbits; $i++) {			if (rand (1) < $bit_mutation_probability) {				 $new_population[$n]{'value'}[$i] = 1 - $new_population[$n]{'value'}[$i];			}		}	}#	print "After mutation\n";#	foreach (@new_population) {#		@code = @{${$_}{'value'}};#		foreach (@code) {#			print $_;#		}#		print "\n";#	}		@population = @new_population;	$population_size = $new_pop_size;}

⌨️ 快捷键说明

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