Click here to Skip to main content
15,890,947 members
Home / Discussions / C#
   

C#

 
AnswerRe: C#: How to compare variables by value and reference type Pin
Richard MacCutchan8-May-17 23:25
mveRichard MacCutchan8-May-17 23:25 
AnswerMessage Closed Pin
8-May-17 23:27
harold aptroot8-May-17 23:27 
QuestionRedundent code in a class Pin
hussain.rao158-May-17 20:29
hussain.rao158-May-17 20:29 
AnswerRe: Redundent code in a class Pin
Richard MacCutchan8-May-17 21:06
mveRichard MacCutchan8-May-17 21:06 
GeneralRe: Redundent code in a class Pin
hussain.rao1511-May-17 23:56
hussain.rao1511-May-17 23:56 
GeneralRe: Redundent code in a class Pin
Richard MacCutchan12-May-17 0:24
mveRichard MacCutchan12-May-17 0:24 
AnswerRe: Redundent code in a class Pin
Gerry Schmitz9-May-17 6:17
mveGerry Schmitz9-May-17 6:17 
QuestionCan you speed up the calculation of programs using software methods and how much? Pin
Member 127091098-May-17 1:21
Member 127091098-May-17 1:21 
Below is a program that selects the coefficients and corresponds to the result. After that sorting is performed for the statement.
Initially the program calculated the coefficient from 10%.
I tried to change the accuracy by 2%. The result turned out to be bad - the time for selecting the coefficients was greatly increased.
The question is:
Can other methods be used to speed up the finding of the result. And how fast can the program work?
Maybe someone already faced a similar problem?
Below is the code of the program

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Flerov.PortfolioOptimizer
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var  dlg = new OpenFileDialog();

            if (dlg.ShowDialog() != DialogResult.OK)
                return;

            var input = File.ReadAllLines(dlg.FileName).Skip(2).Select(line =>
                {
                    var res = line.Split(';');
                    var date = DateTime.ParseExact(res[0], "dd.MM.yyyy", (IFormatProvider)CultureInfo.InvariantCulture);
                    //var date = DateTime.ParseExact(res[0] + " " + res[1], "dd.MM.yyyy 00:00", (IFormatProvider)CultureInfo.InvariantCulture);
                    //  "dd.MM.yyyy", (IFormatProvider)CultureInfo.InvariantCulture)
                    var data = res.Skip(1).Select(decimal.Parse).ToArray();
                    return new Tuple<DateTime, decimal[]>(date, data);
                }).ToArray();

            var count = input.First().Item2.Length;

            var allKkoefs = new List<decimal>();
 //           for (decimal i = 0; i <= 1; i+=0.1m) 
            for (decimal i = 0; i <= 1; i += 0.02m)
                {
                    allKkoefs.Add(i);
            }

            var listResult = new List<Tuple<decimal[], decimal>>();

            var permutation = allKkoefs.Permutations(count).Select(p => p.ToArray()).Where(p => p.Sum() == 1).ToArray();

            foreach (var k in permutation)
            {
                var koefs = k.ToArray();

                var sum = input.Select(p => p.Item2.Zip(koefs, (arg1, arg2) => arg1*arg2).Sum()).ToArray();
                var avg = sum.Average();
                var std = sum.StandardDeviation(avg);
                if (std == 0) std = 1;

                var res = (decimal) (avg/std);

                listResult.Add(new Tuple<decimal[], decimal>(koefs, res));
            }

            var best = listResult.OrderByDescending(p => p.Item2).Take(30).ToArray();

            var header = "value;";
            for (int i = 1; i <= count; i++)
            {
                header += "koef" + i + ";";
            }

            File.WriteAllLines(dlg.FileName + ".result.csv", new[]{header});
            File.AppendAllLines(dlg.FileName + ".result.csv", best.Select(i => string.Format("{0};{1}", i.Item2,
                i.Item1.Aggregate("", (s, arg2) => s+arg2+";"))));
        }
    }

    static class Helper
    {
        public static decimal StandardDeviation(this ICollection<decimal> num, decimal? avg)
        {
            if (num.Count == 1)
                return num.First();

            if (avg == null)
                avg = num.Average();

            var sumOfSqrs = num.Sum(t => Math.Pow((double)(t - avg.Value), 2));

            var n = num.Count;
            return (decimal)Math.Sqrt(sumOfSqrs / (n - 1));
        }

        public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> pool, int selectionSize)
        {
            var idxs = new int[selectionSize];
            var pl = pool.ToArray();
            var length = pl.Length;

            do
            {
                yield return GetItems(pl, idxs, selectionSize);
                GetNextPermutation(idxs, selectionSize, length);
            } while (idxs[0] < length);
        }

        private static void GetNextPermutation(IList<int> idxs, int selectionSize, int poolSize)
        {
            var position = selectionSize - 1;

            idxs[position]++;
            while (idxs[position] == poolSize && position > 0)
            {
                idxs[position - 1]++;
                for (var i = position; i < selectionSize; i++)
                    idxs[i] = 0;
                position--;
            }
        }

        private static IEnumerable<T> GetItems<T>(IList<T> items, IList<int> idxs, int length)
        {
            for (var i = 0; i < length; i++)
                yield return items[idxs[i]];
        }
    }
}

QuestionQuestiongame over the net - client crash Pin
Member 129316127-May-17 7:52
Member 129316127-May-17 7:52 
AnswerRe: Questiongame over the net - client crash Pin
Richard Andrew x647-May-17 11:32
professionalRichard Andrew x647-May-17 11:32 
AnswerRe: Questiongame over the net - client crash Pin
OriginalGriff7-May-17 21:41
mveOriginalGriff7-May-17 21:41 
QuestionCreate Dynamic buttons Pin
Member 131834647-May-17 0:31
Member 131834647-May-17 0:31 
QuestionRe: Create Dynamic buttons Pin
CHill607-May-17 0:33
mveCHill607-May-17 0:33 
AnswerRe: Create Dynamic buttons Pin
User 41802547-May-17 8:49
User 41802547-May-17 8:49 
GeneralRe: Create Dynamic buttons Pin
Member 131834648-May-17 1:04
Member 131834648-May-17 1:04 
SuggestionRe: Create Dynamic buttons Pin
Richard Deeming8-May-17 1:54
mveRichard Deeming8-May-17 1:54 
QuestionHow to Do Implementation of this questions Please Pin
Member 131757266-May-17 11:08
Member 131757266-May-17 11:08 
AnswerRe: How to Do Implementation of this questions Please Pin
Mycroft Holmes6-May-17 14:00
professionalMycroft Holmes6-May-17 14:00 
AnswerRe: How to Do Implementation of this questions Please Pin
Pete O'Hanlon6-May-17 22:00
mvePete O'Hanlon6-May-17 22:00 
Questionanother way Type 'short is short-shrifted, but not short-shifted Pin
BillWoodruff5-May-17 1:10
professionalBillWoodruff5-May-17 1:10 
AnswerRe: another way Type 'short is short-shrifted, but not short-shifted Pin
Jochen Arndt5-May-17 1:32
professionalJochen Arndt5-May-17 1:32 
AnswerRe: another way Type 'short is short-shrifted, but not short-shifted Pin
Pete O'Hanlon5-May-17 1:35
mvePete O'Hanlon5-May-17 1:35 
AnswerRe: another way Type 'short is short-shrifted, but not short-shifted Pin
harold aptroot5-May-17 3:05
harold aptroot5-May-17 3:05 
GeneralRe: another way Type 'short is short-shrifted, but not short-shifted Pin
BillWoodruff5-May-17 4:54
professionalBillWoodruff5-May-17 4:54 
GeneralRe: another way Type 'short is short-shrifted, but not short-shifted Pin
harold aptroot5-May-17 5:19
harold aptroot5-May-17 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.