Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell: Abstracting a Genetic Algorithm
    primarykey
    data
    text
    <p>I'm new to the world of Haskell programming and I'm cutting my teeth on a simple genetic algorithm for finding good solutions to the Travelling Salesman problem. I am representing the solutions as permutations on Integers and so I have this type synonym</p> <pre><code>type Genome = [Int] </code></pre> <p>The algorithm itself is a set of functions which operate on the solutions:</p> <pre><code>mutation :: Genome -&gt; Genome selectParents :: [Genome] -&gt; [Genome] -&gt; [Genome] crossover :: Genome -&gt; Genome -&gt; (Genome, Genome) selectSurvivors :: [Genome] -&gt; [Genome] -&gt; [Genome] </code></pre> <p>I'm not sure how much of my code is relevant to my question so please ask if more details are needed. One thing that might be worth mentioning is that the type signatures above are actually simplified, I am in fact using the State monad to carry around an <code>StdGen</code> so all of these functions actually return stateful computations.</p> <p>There are several things which I would like to do with this but can't quite get my head around. I want to make it possible to choose different representations for the solutions, it seems to me that this would be a natural place to use a type class, so that <code>Genome</code> would be the type class and <code>[Int]</code> a specific instance of this <code>Genome</code>. </p> <p>Now, I want to be able to experiment with the implementations, and I want to be able to use the code in other projects. Using a type class like this would require that every new algorithm I create would require me to create another instance of <code>Genome</code>, is this a good way to go about creating a library?</p> <p>One bonus question, just a thing that's been bothering me, is there any way to create something like a type synonym for a function so that if I'm writing a function which takes functions as arguments I can write the synonym rather than the whole type signature of the function i.e so that something like the following would work.</p> <pre><code>type someFunc = [Int] -&gt; [Int] -&gt; Int someOtherFunc :: someFunc -&gt; [Int] -&gt; Int </code></pre> <p>Right, hopefully that's a lucid enough explanation of the problem, feel like I've missed the really obvious answer but it hasn't jumped out at me. Cheers</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload