7
\$\begingroup\$

I decided to try and implement (a version of) the simulated annealing algorithm using just LINQ, just to see if I could.

I'd love it if anybody could see any ways to improve it, or give advice on any cool tricks for doing this kind of thing.

var result = (from res in Enumerable.Range(0, 1)
 let R = new Random()
 let initial = Enumerable.Range(0, 10).Select(i => R.Next(-10, 10))
 let iterations = Enumerable.Range(0, 100)
 let Schedule = (Func<int, float>)
 (X => 4 + (float)Math.Sin(X))
 from iteration in iterations
 let temperature = Schedule(iteration)
 let state = Enumerable.Range(0, 10).Select(i => R.Next(-10, 10))
 let DeltaE = state.Sum() - initial.Sum()
 where DeltaE > 0 ||
 Math.Pow(Math.E, DeltaE / temperature) > R.NextDouble()
 select state.ToList()
 ).OrderBy(S => S.Sum()).First();
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 29, 2011 at 3:15
\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

You have a lot of small errors here.

  • What is the purpose of from res in Enumerable.Range(0, 1)? It looks like you did it to force some local variables into 'let' queries, which makes no sense to me.
  • Instead of using OrderBy(X).First() you should use MinBy(X) (write it yourself if it doesn't exist). There's a significant performance difference.
  • The value of initial changes every time it is enumerated. Confusing. (You can cache the results with ToArray or ToList to prevent re-enumerating from running more Random.Next calls)
  • Ensure new Random() is only performed once (do it outside the query). The default seed is the current time, which is highly correlated between calls. You're likely to end up with two instances of Random with the exact same seed.
  • Random.Next(-10, 10) returns values in [-10, +9]. You probably wanted [-10, +10].
palacsint
30.3k9 gold badges82 silver badges157 bronze badges
answered Nov 1, 2011 at 16:11
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.