Информация об изменениях

Сообщение Re[5]: [забыл математику] Оптимизация алгоритма от 16.09.2022 17:59

Изменено 16.09.2022 19:13 xma

Re[5]: [забыл математику] Оптимизация алгоритма
Здравствуйте, Real 3L0, Вы писали:

R3>Забей. Вон рабочий вариант: http://rsdn.org/forum/dotnet/8360983.1
Автор: Xander Zerge
Дата: 14.09.22


при marketBuyPrice = 0 неправильно работает .. (но возможно конечно у тебя всегда marketBuyPrice > 0 по ТЗ)

R3>Забей. Вон рабочий вариант: http://rsdn.org/forum/dotnet/8360983.1
Автор: Xander Zerge
Дата: 14.09.22


не рабочий, погонял random и вуаля

marketBuyPrice = 6624.17, overwriteMinStep = 66.241699999999999999999999999
ret = 6624.1700000000000000000000000
ret_opt_Serge_Novikoff = 6690.4116999999999999999999999


  вот ещё

marketBuyPrice = 7169.31, overwriteMinStep = 71.693099999999999999999999999
ret = 7169.3100000000000000000000000
ret_opt_Serge_Novikoff = 7241.0030999999999999999999999


marketBuyPrice = 3349.01, overwriteMinStep = 33.490099999999999999999999999
ret_original = 3349.0100000000000000000000000
ret_opt_Serge_Novikoff = 3382.5000999999999999999999999


marketBuyPrice = 5063.61, overwriteMinStep = 50.636099999999999999999999998
ret_original = 5063.6100000000000000000000000
ret_opt_Serge_Novikoff = 5114.2460999999999999999999998


marketBuyPrice = 4461.52, overwriteMinStep = 44.615199999999999999999999999
ret_original = 4461.5200000000000000000000000
ret_opt_Serge_Novikoff = 4506.1351999999999999999999999



можешь погонять тут random, запуская код

  код с random
https://dotnetfiddle.net/

using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        
        /*
        decimal marketBuyPrice = 6624.17m, overwriteMinStep = 66.241699999999999999999999999m;
        var ret_original = test_original(marketBuyPrice, overwriteMinStep);
        var ret_opt_Serge_Novikoff = test_optimal_Serge_Novikoff(marketBuyPrice, overwriteMinStep);
        
        Console.WriteLine("marketBuyPrice = " + marketBuyPrice + ", overwriteMinStep = " + overwriteMinStep);
        Console.WriteLine("ret_original = " + ret_original);
        Console.WriteLine("ret_opt_Serge_Novikoff = " + ret_opt_Serge_Novikoff);
        
        bool flag = (ret_original == ret_opt_Serge_Novikoff);
        */
        
        ///*
        Random rand = new Random();
        
        bool flag = true;
        
        for (int i=0; i<10000; i++) {
            
            decimal marketBuyPrice = rand.Next(0, 1000000) / 100.0m; 
            decimal overwriteMinStep = rand.Next(1, 10000000) / 100.0m; 
            
            if (marketBuyPrice / overwriteMinStep > 100)
                overwriteMinStep *= marketBuyPrice / (overwriteMinStep * 100.0m);
            
            var ret_original = test_original(marketBuyPrice, overwriteMinStep);
            var ret_opt_Serge_Novikoff = test_optimal_Serge_Novikoff(marketBuyPrice, overwriteMinStep);
            
            if ( ret_original != ret_opt_Serge_Novikoff ) {
                
                Console.WriteLine("");
                Console.WriteLine("marketBuyPrice = " + marketBuyPrice + ", overwriteMinStep = " + overwriteMinStep);
                
                Console.WriteLine("ret_original = " + ret_original);
                Console.WriteLine("ret_opt_Serge_Novikoff = " + ret_opt_Serge_Novikoff);
                
                flag = false;
            }
        }
        //*/
        
        Console.WriteLine("flag = " + flag);
        
    }
    
    public static decimal test_original(decimal marketBuyPrice, decimal overwriteMinStep)
    {        
        decimal mediumPrice2 = 0;

        do
        {
            mediumPrice2 += overwriteMinStep;
        } while (mediumPrice2 < marketBuyPrice);
        
        return mediumPrice2;
    }
        
    public static decimal test_optimal_Serge_Novikoff(decimal marketBuyPrice, decimal minStep)
    {        
        var imin = (int)(marketBuyPrice / minStep);
        
        if (minStep * imin < marketBuyPrice)
            ++imin;

        decimal mediumPrice2 = minStep * imin;
            
        return mediumPrice2;
    }
}



  код Serge Novikoff c добавление оригинала для проверки
https://dotnetfiddle.net/

на этих значениях — не совпадает результат, (с остальными вышеперечисленными ошибочными (найденными рандомом), аналогично)

using System;
using System.Collections.Generic;
using System.Linq;
                
public class Program
{
    public static void Main()
    {
        decimal marketBuyPrice = 6624.17m, overwriteMinStep = 66.241699999999999999999999999m;
        
        var ret_original = test_original(marketBuyPrice, overwriteMinStep);
        Console.WriteLine("ret_original = " + ret_original);
        
        var ret = PricesForBuy(marketBuyPrice, marketBuyPrice + overwriteMinStep * 10.2m, overwriteMinStep);
        Console.WriteLine("ret_opt_Serge_Novikoff = " + string.Join(" ", ret));
    }
    
    public static decimal test_original(decimal marketBuyPrice, decimal overwriteMinStep)
    {        
        decimal mediumPrice2 = 0;

        do
        {
            mediumPrice2 += overwriteMinStep;
        } while (mediumPrice2 < marketBuyPrice);
        
        return mediumPrice2;
    }
    
    public static List<decimal> PricesForBuy(decimal marketBuyPrice, decimal marketSellPrice, decimal minStep)
    {
        var imax = (int)(marketSellPrice / minStep);

        var imin = (int)(marketBuyPrice / minStep);
        
        //Console.WriteLine("(marketBuyPrice / minStep) = " + (marketBuyPrice / minStep));
        //Console.WriteLine("imin = " + imin);
        
        if (minStep * imin < marketBuyPrice)
            ++imin;
        
        return MakePriceRange_V3(imin, imax, minStep);
    }
    
    private static List<decimal> MakePriceRange_V1(int min, int max, decimal step)
    {
        var ret = new List<decimal>(max - min + 1);
        for (var i = min; i <= max; ++i)
            ret.Add(step * i);
        return ret;
    }
    
    private static List<decimal> MakePriceRange_V2(int min, int max, decimal step)
    {
        return new List<decimal>(Enumerable.Range(min, max - min + 1).Select(v => step * v));
    }

    private static List<decimal> MakePriceRange_V3(int min, int max, decimal step)
    {
        var ret = new List<decimal>(max - min + 1);
        var price = step * min;
        for (var i = min; i <= max; ++i, price += step)
            ret.Add(price);
        return ret;
    }
}


P.S.:

но возможно у тебя значения ограниченной точности конечно в реальных задач, и поэтому таких проблем не возникает ..
Re[5]: [забыл математику] Оптимизация алгоритма
Здравствуйте, Real 3L0, Вы писали:

R3>Забей. Вон рабочий вариант: http://rsdn.org/forum/dotnet/8360983.1
Автор: Xander Zerge
Дата: 14.09.22


при marketBuyPrice = 0 неправильно работает .. (но возможно конечно у тебя всегда marketBuyPrice > 0 по ТЗ)

R3>Забей. Вон рабочий вариант: http://rsdn.org/forum/dotnet/8360983.1
Автор: Xander Zerge
Дата: 14.09.22


не рабочий, погонял random и вуаля

marketBuyPrice = 6624.17, overwriteMinStep = 66.241699999999999999999999999
ret = 6624.1700000000000000000000000
ret_opt_Serge_Novikoff = 6690.4116999999999999999999999


  вот ещё

marketBuyPrice = 7169.31, overwriteMinStep = 71.693099999999999999999999999
ret = 7169.3100000000000000000000000
ret_opt_Serge_Novikoff = 7241.0030999999999999999999999


marketBuyPrice = 3349.01, overwriteMinStep = 33.490099999999999999999999999
ret_original = 3349.0100000000000000000000000
ret_opt_Serge_Novikoff = 3382.5000999999999999999999999


marketBuyPrice = 5063.61, overwriteMinStep = 50.636099999999999999999999998
ret_original = 5063.6100000000000000000000000
ret_opt_Serge_Novikoff = 5114.2460999999999999999999998


marketBuyPrice = 4461.52, overwriteMinStep = 44.615199999999999999999999999
ret_original = 4461.5200000000000000000000000
ret_opt_Serge_Novikoff = 4506.1351999999999999999999999



можешь погонять тут random, запуская код

  код с random
https://dotnetfiddle.net/

using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        
        /*
        decimal marketBuyPrice = 6624.17m, overwriteMinStep = 66.241699999999999999999999999m;
        var ret_original = test_original(marketBuyPrice, overwriteMinStep);
        var ret_opt_Serge_Novikoff = test_optimal_Serge_Novikoff(marketBuyPrice, overwriteMinStep);
        
        Console.WriteLine("marketBuyPrice = " + marketBuyPrice + ", overwriteMinStep = " + overwriteMinStep);
        Console.WriteLine("ret_original = " + ret_original);
        Console.WriteLine("ret_opt_Serge_Novikoff = " + ret_opt_Serge_Novikoff);
        
        bool flag = (ret_original == ret_opt_Serge_Novikoff);
        */
        
        ///*
        Random rand = new Random();
        
        bool flag = true;
        
        for (int i=0; i<10000; i++) {
            
            decimal marketBuyPrice = rand.Next(0, 1000000) / 100.0m; 
            decimal overwriteMinStep = rand.Next(1, 10000000) / 100.0m; 
            
            if (marketBuyPrice / overwriteMinStep > 100)
                overwriteMinStep *= marketBuyPrice / (overwriteMinStep * 100.0m);
            
            var ret_original = test_original(marketBuyPrice, overwriteMinStep);
            var ret_opt_Serge_Novikoff = test_optimal_Serge_Novikoff(marketBuyPrice, overwriteMinStep);
            
            if ( ret_original != ret_opt_Serge_Novikoff ) {
                
                Console.WriteLine("");
                Console.WriteLine("marketBuyPrice = " + marketBuyPrice + ", overwriteMinStep = " + overwriteMinStep);
                
                Console.WriteLine("ret_original = " + ret_original);
                Console.WriteLine("ret_opt_Serge_Novikoff = " + ret_opt_Serge_Novikoff);
                
                flag = false;
            }
        }
        //*/
        
        Console.WriteLine("flag = " + flag);
        
    }
    
    public static decimal test_original(decimal marketBuyPrice, decimal overwriteMinStep)
    {        
        decimal mediumPrice2 = 0;

        do
        {
            mediumPrice2 += overwriteMinStep;
        } while (mediumPrice2 < marketBuyPrice);
        
        return mediumPrice2;
    }
        
    public static decimal test_optimal_Serge_Novikoff(decimal marketBuyPrice, decimal minStep)
    {        
        var imin = (int)(marketBuyPrice / minStep);
        
        if (minStep * imin < marketBuyPrice)
            ++imin;

        decimal mediumPrice2 = minStep * imin;
            
        return mediumPrice2;
    }
}



  код Serge Novikoff c добавление оригинала для проверки
https://dotnetfiddle.net/

на этих значениях — не совпадает результат, (с остальными вышеперечисленными ошибочными (найденными рандомом), аналогично)

using System;
using System.Collections.Generic;
using System.Linq;
                
public class Program
{
    public static void Main()
    {
        decimal marketBuyPrice = 6624.17m, overwriteMinStep = 66.241699999999999999999999999m;
        
        var ret_original = test_original(marketBuyPrice, overwriteMinStep);
        Console.WriteLine("ret_original = " + ret_original);
        
        var ret = PricesForBuy(marketBuyPrice, marketBuyPrice + overwriteMinStep * 10.2m, overwriteMinStep);
        Console.WriteLine("ret_opt_Serge_Novikoff = " + string.Join(" ", ret));
    }
    
    public static decimal test_original(decimal marketBuyPrice, decimal overwriteMinStep)
    {        
        decimal mediumPrice2 = 0;

        do
        {
            mediumPrice2 += overwriteMinStep;
        } while (mediumPrice2 < marketBuyPrice);
        
        return mediumPrice2;
    }
    
    public static List<decimal> PricesForBuy(decimal marketBuyPrice, decimal marketSellPrice, decimal minStep)
    {
        var imax = (int)(marketSellPrice / minStep);

        var imin = (int)(marketBuyPrice / minStep);
        
        //Console.WriteLine("(marketBuyPrice / minStep) = " + (marketBuyPrice / minStep));
        //Console.WriteLine("imin = " + imin);
        
        if (minStep * imin < marketBuyPrice)
            ++imin;
        
        return MakePriceRange_V3(imin, imax, minStep);
    }
    
    private static List<decimal> MakePriceRange_V1(int min, int max, decimal step)
    {
        var ret = new List<decimal>(max - min + 1);
        for (var i = min; i <= max; ++i)
            ret.Add(step * i);
        return ret;
    }
    
    private static List<decimal> MakePriceRange_V2(int min, int max, decimal step)
    {
        return new List<decimal>(Enumerable.Range(min, max - min + 1).Select(v => step * v));
    }

    private static List<decimal> MakePriceRange_V3(int min, int max, decimal step)
    {
        var ret = new List<decimal>(max - min + 1);
        var price = step * min;
        for (var i = min; i <= max; ++i, price += step)
            ret.Add(price);
        return ret;
    }
}