Example #1
0
File: Lab2.cs Project: MatyasovM/pi
        static double FindRandomNeighborhood(double theKey)
        {
            int    aCodeIndex  = myKeys.IndexOf(theKey);
            var    aCode       = myValues[aCodeIndex];
            string aStringCode = General.BitSetAsString(aCode);

            List <double> aRes = new List <double>();

            for (int i = 0; i < aStringCode.Length; i++)
            {
                aStringCode = InvertBit(aStringCode, i);

                int         anIntegerValue = Convert.ToInt32(aStringCode, 2);
                BitVector32 aCurBitVector  = new BitVector32(anIntegerValue);

                int anIndex = myValues.IndexOf(aCurBitVector);
                if (anIndex >= 0)
                {
                    aRes.Add(myKeys[anIndex]);
                }
                aStringCode = InvertBit(aStringCode, i);
            }

            Random aRand        = new Random();
            int    aRandomIndex = aRand.Next(0, aRes.Count);

            return(aRes[aRandomIndex]);
        }
Example #2
0
File: Lab2.cs Project: MatyasovM/pi
        static void Algorithm()
        {
            var    maxS = myValues[4];
            double max  = myKeys[4];

            int N = 100;

            for (int i = 0; i < N; i++)
            {
                Console.WriteLine($"Step # {i + 1}, current max = {max}, current maxS = {General.BitSetAsString (maxS)}");
                var aNeighborhood = FindRandomNeighborhood(max);
                Console.WriteLine($"The best code in neighborhood: {aNeighborhood}");
                if (aNeighborhood > max)
                {
                    maxS = myValues [myKeys.IndexOf(aNeighborhood)];
                    max  = aNeighborhood;
                    Console.WriteLine($"New maximum: {max}");
                }

                Console.Write(Environment.NewLine);
            }

            Console.WriteLine($"max : {max}");
            Console.WriteLine($"maxS : {General.BitSetAsString (maxS)}");
        }
Example #3
0
        static void Algorithm()
        {
            var    maxS = myValues[4];
            double max  = myKeys[4];

            int N = 10;

            for (int i = 0; i < N; i++)
            {
                Console.WriteLine($"Step # {i + 1}, current max = {max}, current maxS = {General.BitSetAsString (maxS)}");
                var aNeighborhood = FindNeighborhood(max);
                PrintNeighborhood(aNeighborhood);
                if (aNeighborhood.Count != 0)
                {
                    var aMax = aNeighborhood.Max();
                    Console.WriteLine($"The best code in neighborhood: {aMax}");
                    if (aMax > max)
                    {
                        maxS = myValues [myKeys.IndexOf(aMax)];
                        max  = aMax;
                        Console.WriteLine($"New maximum: {max}");
                    }
                    else
                    {
                        break;
                    }
                }

                Console.Write(Environment.NewLine);
            }

            Console.WriteLine($"max : {max}");
            Console.WriteLine($"maxS : {General.BitSetAsString (maxS)}");
        }
Example #4
0
File: Lab2.cs Project: MatyasovM/pi
 static void PrintNeighborhood(List <double> theSource)
 {
     Console.WriteLine("Current Neighborhood:");
     foreach (var aVal in theSource)
     {
         int aKeyIndex    = myKeys.IndexOf(aVal);
         var aCurrentCode = myValues [aKeyIndex];
         Console.WriteLine("\t" + General.BitSetAsString(aCurrentCode));
     }
 }