Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace l2 { public static class Util { private static Random rnd = new Random(); public static int GetRandom02() { return rnd.Next(0,2); } public static double GetRandom02d() { return rnd.NextDouble(); } public static int GetRandomCustom(int val) { return rnd.Next(0, val); } public static char rev(char har) { char h = '0'; if (har == h) return '1'; else return '0'; } public static string ReplaceAt(this string input, int index, char newChar) { char[] chars = input.ToCharArray(); chars[index] = newChar; return new string(chars); } } class Program { public static string Reverse(string s) { char[] charArray = s.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } public static String genbin(double dlugosc) { String liczbastr = ""; for (int i = 0; i < dlugosc; i++) { liczbastr = liczbastr.Insert(0, Util.GetRandom02().ToString()); } return liczbastr; } public static int bintodec(String bin) { int dec = 0; dec = Convert.ToInt32(bin, 2); return dec; } public static double doprzedzialu(int liczba, double a, double b, double m) { double wynik = 0; wynik = a + (b - a) * liczba / (Math.Pow(2, m) - 1); return wynik; } public static double rastrigin(double x, double A, double omega, int n) { double wynik = 0; wynik = A * n + Math.Pow(x, 2) - 10 * Math.Cos(omega * x); return wynik; } static void Main(string[] args) { bool pierwszy = true; double A = 10; double omega = 20 * 3.14; int n = 3; int d = 6; double a = -1.0; double b = 1.0; double dziedzm = b - a; double ilosckom = (b - a) * Math.Pow(10, d); int m = 0; double dlugwekbin = Math.Pow(2, m); while (dlugwekbin <= ilosckom) { m++; dlugwekbin = Math.Pow(2, m); } int il = 10; List<String> lancuchy = new List<String>(); for (int i = 0; i < il; i++) { lancuchy.Add(genbin(m)); } List<String> koncowe = new List<String>(); for (int k = 0; k < 10; k++) { //CAŁKOWITE ZASTĘPOWANIE if(pierwszy != true) lancuchy = koncowe; //CZĘŚCIOWE ZASTĘPOWANIE /*if(pierwszy != true) { List<String> temp = lancuchy; lancuchy = new List<String>(); for (int i = 0; i < 5; i++) lancuchy.Add(koncowe[i]); for (int i = 0; i < 5; i++) lancuchy.Add(temp[i]); } */ //LOSOWE ZASTĘPOWANIE /*if (pierwszy != true) { List<String> temp = lancuchy.Concat(koncowe).ToList(); lancuchy = new List<String>(); for (int i = 0; i < 10; i++) lancuchy.Add(temp[Util.GetRandomCustom(temp.Count)]); } */ List<double> odwzorowane = new List<double>(); for (int i = 0; i < il; i++) { odwzorowane.Add(doprzedzialu(bintodec(lancuchy[i]), a, b, m)); } List<double> rastriginlista = new List<double>(); for (int i = 0; i < il; i++) { rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n)); } Console.WriteLine("Dziedzina zmienności = " + dziedzm + " Il kombinacji = " + ilosckom + " m = " + m + " dł wektora bin = " + dlugwekbin); Console.WriteLine(); Console.WriteLine("Wektory binarne:"); for (int i = 0; i < il; i++) Console.WriteLine(lancuchy[i]); Console.WriteLine(); Console.WriteLine("Odwzorowane lancuchy binarne na liczbę X z zakresu [" + a + "," + b + "]:"); for (int i = 0; i < il; i++) Console.WriteLine(odwzorowane[i]); Console.WriteLine(); Console.WriteLine("Wartości funkcji Rastrigina:"); for (int i = 0; i < il; i++) Console.WriteLine(rastriginlista[i]); Console.WriteLine(); Console.WriteLine("RULETKA:"); Console.WriteLine(); double F = 0; for (int i = 0; i < il; i++) F += rastriginlista[i]; //Console.WriteLine("Całkowite dopasowanie F = " + F); List<double> prawdopodobienstwa_wyboru = new List<double>(); for (int i = 0; i < il; i++) prawdopodobienstwa_wyboru.Add(rastriginlista[i] / F); /*Console.WriteLine("Prawdopodobienstwa wyboru:"); for (int i = 0; i < il; i++) Console.WriteLine(prawdopodobienstwa_wyboru[i]);*/ List<double> dystrybuanty = new List<double>(); for (int i = 0; i < il; i++) { double dystr = 0; for (int j = 0; j <= i; j++) dystr += prawdopodobienstwa_wyboru[j]; dystrybuanty.Add(dystr); } /*Console.WriteLine("Dystrybuanty:"); for (int i = 0; i < il; i++) Console.WriteLine(dystrybuanty[i]);*/ List<String> ruletka = new List<string>(); for (int i = 0; i < il; i++) { double r = Util.GetRandom02d(); for (int j = 0; j < il; j++) { if (r < dystrybuanty[j]) { //Console.WriteLine("Wybrany osobnik z indeksem " + j + ": " + lancuchy[j]); ruletka.Add(lancuchy[j]); break; } } } for (int i = 0; i < il; i++) Console.WriteLine(ruletka[i]); Console.WriteLine(); Console.WriteLine("MUTACJA:"); Console.WriteLine(); double pm = 0.1; List<String> lancuchymut = new List<String>(); for (int i = 0; i < ruletka.Count; i++) { string st = ""; for (int j = 0; j < m; j++) { double rn = Util.GetRandom02d(); if (rn < pm) { if (ruletka[i][j] == '0') st = st.Insert(0, "1"); else if (ruletka[i][j] == '1') st = st.Insert(0, "0"); } else { st = st.Insert(0, ruletka[i][j].ToString()); } } lancuchymut.Add(Reverse(st)); } /* Console.WriteLine("PO MUTACJI:"); for (int i = 0; i < il; i++) Console.WriteLine(lancuchymut[i]);*/ Console.WriteLine(); Console.WriteLine("INWERSJA:"); Console.WriteLine(); List<String> inw = new List<String>(); double pi = 0.1; for (int i = 0; i < ruletka.Count; i++) { double rnd = Util.GetRandom02d(); if(rnd < pi) { int p1 = Util.GetRandomCustom(m - 1); int p2 = Util.GetRandomCustom(m - 1); int pocz; int kon; if (p1 < p2) { pocz = p1; kon = p2; } else { pocz = p2; kon = p1; } string lancuch = ruletka[i]; for(int j = pocz + 1; j < kon; j++) { if (lancuch[j] == '0') lancuch = lancuch.ReplaceAt(j, '1'); else if (lancuch[j] == '1') lancuch = lancuch.ReplaceAt(j, '0'); } inw.Add(lancuch); } } /* for (int i = 0; i < inw.Count; i++) Console.WriteLine(inw[i]);*/ Console.WriteLine(); Console.WriteLine("KRZYŻOWANIE JEDNOPUNKTOWE:"); Console.WriteLine(); List<String> rulkopia = ruletka; double pk = 0.6; List<String> ldokrzyz = new List<String>(); for (int i = il - 1; i >= 0; i--) { double r = Util.GetRandom02d(); if (r < pk) { ldokrzyz.Add(rulkopia[i]); rulkopia.RemoveAt(i); } } if (ldokrzyz.Count % 2 != 0) { int rnd = Util.GetRandomCustom(rulkopia.Count); ldokrzyz.Add(rulkopia[rnd]); rulkopia.RemoveAt(rnd); } /*for (int i = 0; i < ldokrzyz.Count; i++) Console.WriteLine(ldokrzyz[i]);*/ List<String> lkrzyzfin = new List<String>(); int ile = ldokrzyz.Count / 2; for (int i = 0; i < ile; i++) { List<String> lkrzyztemp = new List<String>(); int los = Util.GetRandomCustom(ldokrzyz.Count); lkrzyztemp.Add(ldokrzyz[los]); ldokrzyz.RemoveAt(los); los = Util.GetRandomCustom(ldokrzyz.Count); lkrzyztemp.Add(ldokrzyz[los]); ldokrzyz.RemoveAt(los); int punkt = Util.GetRandomCustom(m - 1); string t1 = lkrzyztemp[0]; string t2 = lkrzyztemp[1]; for (int j = punkt + 1; j < m; j++) { lkrzyztemp[0] = lkrzyztemp[0].ReplaceAt(j, t2[j]); lkrzyztemp[1] = lkrzyztemp[1].ReplaceAt(j, t1[j]); } lkrzyzfin.Add(lkrzyztemp[0]); lkrzyzfin.Add(lkrzyztemp[1]); } List<String> wszystkie = new List<string>(); wszystkie = lancuchymut.Concat(inw).Concat(rulkopia).ToList(); odwzorowane = new List<double>(); for (int i = 0; i < wszystkie.Count; i++) { odwzorowane.Add(doprzedzialu(bintodec(wszystkie[i]), a, b, m)); } rastriginlista = new List<double>(); for (int i = 0; i < odwzorowane.Count; i++) { rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n)); } List<Tuple<double, string>> polaczone = new List<Tuple<double, string>>(); for (int i = 0; i < wszystkie.Count; i++) polaczone.Add(new Tuple<double, String>(rastriginlista[i], wszystkie[i])); polaczone.Sort(); polaczone.Reverse(); Console.WriteLine("WSZYSTKIE:"); for (int i = 0; i < polaczone.Count; i++) Console.WriteLine(polaczone[i]); koncowe = new List<String>(); for(int i = 0; i < 10; i++) { koncowe.Add(polaczone[i].Item2); } odwzorowane = new List<double>(); for (int i = 0; i < koncowe.Count; i++) { odwzorowane.Add(doprzedzialu(bintodec(koncowe[i]), a, b, m)); } rastriginlista = new List<double>(); for (int i = 0; i < odwzorowane.Count; i++) { rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n)); } rastriginlista.Sort(); polaczone = new List<Tuple<double, string>>(); for (int i = 0; i < koncowe.Count; i++) polaczone.Add(new Tuple<double, String>(rastriginlista[i], koncowe[i])); polaczone.Sort(); polaczone.Reverse(); pierwszy = false; Console.WriteLine("WYNIK:"); for (int i = 0; i < polaczone.Count; i++) Console.WriteLine(polaczone[i]); } Console.ReadKey(); } } }
Optional Paste Settings
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
glossary padding (...
CSS | 1 min ago
OSRS worlds jan 25...
Java | 6 min ago
FDP Ejercicio
Racket | 15 min ago
glossary padding v...
CSS | 23 min ago
List values of man...
ABAP | 28 min ago
Arrays) Hangman
Java | 30 min ago
Arrays) Parallel A...
Java | 31 min ago
Arrays) Initialise...
Java | 32 min ago
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!