Linq version of the simulated annealing algorithm 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 =&gt; R.Next(-10, 10))
                  let iterations = Enumerable.Range(0, 100)

                  let Schedule = (Func&lt;int, float&gt;)
                                 (X =&gt; 4 + (float)Math.Sin(X))


                  from iteration in iterations
                      let temperature = Schedule(iteration)
                      let state = Enumerable.Range(0, 10).Select(i =&gt; R.Next(-10, 10))
                      let DeltaE = state.Sum() - initial.Sum()

                      where DeltaE &gt; 0 ||
                            Math.Pow(Math.E, DeltaE / temperature) &gt; R.NextDouble()
                      select state.ToList()
              ).OrderBy(S =&gt; S.Sum()).First();
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].