Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4233668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:14:57+00:00 2026-05-21T02:14:57+00:00

I have a c# function that i have as part of a bigger algo

  • 0

I have a c# function that i have as part of a bigger algo that im designing however that function is acting weird. It is calculating different results for fitness on multiple runs with the same arguments. I do not see where the problem is in the function. Any enlightened suggestion would be welcome.

private readonly Dictionary<int,decimal>_cache = new Dictionary<int, decimal>(); // lookup cache
    private void CalculateFitness(TimeTable timeTable)
    {                      
        const int points = 1;            

        var exams = timeTable.Exams.ToList();
        var combinations = exams.Select(x => exams.Where(y => exams.IndexOf(y) > exams.IndexOf(x))
                                                          .Select(z => new List<Exam> { x, z }))
                                                          .SelectMany(x => x);


        var clash = combinations.Where(touple => touple[0].Period.Id == touple[1].Period.Id && touple[0].Date == touple[1].Date && touple[0].Students.Intersect(touple[1].Students).Any());
        var clCount = clash.Sum(touple => touple[0].Students.Intersect(touple[1].Students).Count());            



        var score = clCount == 0 ? timeTable.Exams.Count : timeTable.Exams.Count - clCount;


        if (_cache.ContainsKey(score))
        {
            timeTable.Fitness = _cache[score];
        }
        else
        {
            timeTable.Fitness = Math.Abs(decimal.Divide(score, (timeTable.Exams.Count * points))); // Calculate Fitness 
            _cache.Add(score, timeTable.Fitness);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:14:58+00:00Added an answer on May 21, 2026 at 2:14 am

    It is returning different results on
    multiple runs for the same arguments

    That means your function is not side effect free – you are changing or using global state somewhere. Find and eliminate those and the function should always return the expected value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following part of a function that is driving me crazy. As
Let's say I have a program(C++, for example) that allocates multiple objects, never bigger
I have a function that fills a part of the bitmap created by imagecreatetruecolor($h,$w)
I have an third part dll that have a function that returns a string.
I have a custom function defined that extracts part of an address from a
I have a function that generates text that fits the lower part of a
Good Day, I have written the following function that is part of some calculations:
Inside my Controller i have function that runs after user clicks on item, which
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.