Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 8069cf7b4b583f414d2b309341277d1de58012a8
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Wed Jan 15 09:28:57 2020 +0200

    Corrections to version and help information

commit 7a18eda5c5c3c145e01281b5184fd02870768bd0
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Wed Jan 15 09:20:54 2020 +0200

    Comment out current prose. Will need to review and rewrite.

commit ab785144a3dba25d3220527ce95622a9dc5ed0ed
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Wed Jan 15 09:15:50 2020 +0200

    Rework the RVar section and planning content for the next pages

commit 1393c5be76bf4a1c821f4cf25216bdea96cd26de
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Sun Jan 5 12:31:48 2020 +0200

    Small prose updates

commit f1e70c01fe5b4c3f25a801717a4940a4969629a1
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Thu Jan 2 10:30:43 2020 +0200

    Expand on the introduction docs

commit 64f67d2f53abbe59c4ce9d7ae10e194e1c6f3d71
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Wed Jan 1 21:03:55 2020 +0200

    Docusaurus upgrade

commit ca3dbef8635e9918af90b642f52443bbd4fec711
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Wed Jan 1 18:23:33 2020 +0200

    Hide links to blog

commit f5108939c80425776f2a9b0f7cf4ecb17f746050
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Wed Jan 1 18:21:29 2020 +0200

    Clean up docusaurus configuration

commit cc7bb68ba214ef4d212bcb46a58fbc8dfc63d98d
Author: Gary Pamparà <gpampara@gmail.com>
Date:   Wed Jan 1 14:45:57 2020 +0200

    Rename the community tutorial to the "basic tutorial"
  • Loading branch information
gpampara committed Jan 15, 2020
1 parent c3979aa commit 17bddf5
Show file tree
Hide file tree
Showing 50 changed files with 506 additions and 374 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ lazy val core = project

val mdocVariableMap =
Map(
"CILIB_VERSION" -> "2.0"
"CILIB_VERSION" -> "2.0.1"
)
val mdocInFile = new java.io.File("docs")
val mdocOutFile = new java.io.File("website/docs")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: constraint
title: Constraints
---

<!--
As we begin to explore more of the core concepts of CILib we start to see how they are utilized by other concepts.
With this in mind, you might see types specific to CILib you haven't come across yet.
Dont worry, these will be explained in their appropriate sections later in the book.
Expand Down Expand Up @@ -191,3 +192,4 @@ All `Constraint` classes make use of two parameters
This defines a constraint context that can used on `NonEmptyLists` through the companion object.
</div>
-->
File renamed without changes.
2 changes: 2 additions & 0 deletions docs/community-tutorial/de.md → docs/basics/de.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: de
title: DE
---

<!--
In this chapter we will be exploring the differential evolution package of CILib.
What's great about algorithms in CILib is their flexibility.
The DE show cases this wonderfully.
Expand Down Expand Up @@ -137,3 +138,4 @@ def currentToBest[S, A: Numeric: Equal](
y: Int Refined Positive,
z: (Double, Position[A]) => RVar[NonEmptyList[Boolean]])
```
-->
File renamed without changes.
2 changes: 2 additions & 0 deletions docs/community-tutorial/entity.md → docs/basics/entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: entity
title: Entity
---

<!--
By now you should have enough some experience with `Entity`.
If not, refer to the "A Quick Look at Entity" section in the `Lenses` chapter.
Thus we can say the following about `Entity`.
Expand Down Expand Up @@ -124,3 +125,4 @@ That being
Now, if you are asking "What are those?"
Fear not as we find out in the next chapter of "A Guide to CILib"!
-->
2 changes: 2 additions & 0 deletions docs/community-tutorial/eval.md → docs/basics/eval.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: eval
title: Eval
---

<!--
In the last chapter we saw how to test if a set of numbers satisfies a `Constraint` or a violates `Constraints`.
We now are going to look at how to unconstrained or constrained evaluations in CILib.
`Eval` useful in the sense that it provides a platform to determine whether a set of values are a feasible or
Expand Down Expand Up @@ -217,3 +218,4 @@ what the flow of the program logic.
</div>
In the following chapter we will look at the concept that we briefly touched upon, `Fit`.
-->
3 changes: 2 additions & 1 deletion docs/community-tutorial/fit.md → docs/basics/fit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: fit
title: Fitness
---

<!--
After the last chapter you're now somewhat familiar with `Feasible` and `Infeasible`.
`CILib` offers this and more for dealing with the fitness of solutions.
These concepts will be the main focus of this chapter,
Expand Down Expand Up @@ -453,3 +453,4 @@ They key take away points from this chapter are that
- Using the optimization scheme, we can begin to compare solutions (preferably `Feasible`).
</div>
-->
2 changes: 2 additions & 0 deletions docs/community-tutorial/ga.md → docs/basics/ga.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: ga
title: GA
---

<!--
The genetic algorithm library offers two things:
* A specialized `Entity` type
Expand Down Expand Up @@ -46,3 +47,4 @@ mutation: List[Individual[S]] => RVar[List[Individual[S]]] //Function used to mu
It's worth mentioning that all algorithms will have the same return type.
This is so that they may be plugged into an iterator and subsequently an `Runner`.
But for documentation purposes it will be stated at every algorithm.
-->
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
45 changes: 45 additions & 0 deletions docs/basics/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
id: basic-tutorial
title: Preface
hide_title: true
---

# Preface

## About the basic tutorial

This basic tutorial is aimed at walking you through the different parts of CILib (Computational Intelligence Library).
CILib is written in Scala and familiarity with the language is recommended.
Scala is a JVM language which allows for the expression and usage of more advanced type system capabilities, which Java, nor the other JVM languages are able to provide.

[Essential Scala](1) and [Advanced Scala With Cats](2) are freely available, online Scala resources.
It should be noted that these resources recommend practices that are not always followed within CIlib.
The core focus in CIlib development is to *always* prefer a functional approach for implementation and avoid the use of object-orientation as much as possible.
The benefits obtained through this style of code far outweigh any perceived overhead!

> #### Compiler verified code samples
> Throughout the tutorial you will see a lot of theory along side practical examples represented through blocks of code.
> These blocks of code are verified during the documentation processing and will always be up to date with the referenced version of CIlib.

## Knowledge requirements

The code samples that follow will reference various functional programmming abstractions.
Firstly, let's state outright that these concepts are **not scary**, contrary the general misinformation that exists within the programming world.
The abstractions may be unfamiliar to you, which is fair, but this is nothing more than an oppurtunity to learn general programming language theory which is true regardless of the programming language being used :smile:.

Functional programming structures (such as Functor, Applicative and Monad) allow us to be very expressive by clearly restricting what we can and cannot be done with a given structure.
Furthermore, these structures also predefine behaviour that is very useful and enables better composition.
For example, knowing that a given structure has an instance of `Functor` available will immediately inform us that this structure, regardless of what it is, can accpet a transformation function to transform the internal values.

We won't be focusing on this too much and it's not really all that important to use CIlib.
If you do, however, struggle with to follow the examples, please feel free to contact the developers online in order to aid you in understanding and to provide some references to resources that are helpful.
Your questions may also result in updates to this tutorial, which you may also provide yourself by submitting a pull request :tada:.


## Categories of data-structures

In the sections that follow, different data structures will be discussed.
The first few data-structures (`RVar`, `Step` and `StepS`) are the main structures within CIlib and are the building blocks which allow for the expression of algorithms.
Additional data structures follow are important structures in their own right, but are suplimental when compared to the three core data strucutres.
The additional structures are closely related to algorithms, but are concerned with algorithm specifics instead of providing a computational platform.
File renamed without changes.
2 changes: 2 additions & 0 deletions docs/community-tutorial/lenses.md → docs/basics/lenses.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: lenses
title: Lenses
---

<!--
Great! You just learnt about creating positions in a search space.
But you might be asking yourself "How can we modify our set positions to explore the search space?"
And this is exactly what we intend to learn in the coming chapter.
Expand Down Expand Up @@ -387,3 +388,4 @@ In the next chapter we will test our newly acquired skills as we begin to put ev
<div class="callout callout-info">
Optics in CILib use `Lens` and `Prism` that allow us *zoom* in on instances to retrieve information or create new instances.
</div>
-->
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: objective
title: Objective
---

<!--
Computational intelligence research requires an objective.
Either it's a single objective or a collection of objectives.
CILib offers just this.
Expand Down Expand Up @@ -33,3 +34,4 @@ That's it.
If the purpose of `Objective` is still a bit unclear
then don't worry as we are going to be seeing it used a lot in examples in the coming chapters.
Often we'll see `Objectives` returned and we will need to figure out what we do with the information for each case.
-->
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ id: position
title: Position
---

Content currently being developed

<!--
:::caution
Candidate solution vectors within a search space are the basic pieces of information that computational algorithms maintain and,
includes feature vectors that represent training patterns in a neural network.
Expand Down Expand Up @@ -358,3 +361,4 @@ result.eval(rng)
These points can then be evaluated which will yield a solution.
A solution is a point with a fitness evaluation.
:::
-->
2 changes: 2 additions & 0 deletions docs/community-tutorial/pso.md → docs/basics/pso.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ id: pso
title: PSO
---

<!--
The particle swarm optimization library of CILib is one of the most extensive libraries.
While offering numerous pso components and algorithms it's best to begin with the basics of the library and then progressively work from there.
Expand Down Expand Up @@ -548,3 +549,4 @@ crossoverPSO[S](
guide: Guide[S,Double]
)(implicit M: HasMemory[S,Double])
```
-->
File renamed without changes.
124 changes: 124 additions & 0 deletions docs/basics/rvar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
id: rvar
title: RVar
hide_title: true
---

# RVar

```scala mdoc:invisible
import cilib._
```

`RVar` (the contraction of *random variable*) is the backbone data structure of CILib.

`RVar` allows for the sequenced composition of actions involving randomness, but more importantly tracks the effect of applying randomness to the computed value (which the `RVar` represents).
Tracking the effect of randomness is very important, particularly within Computational Intelligence algorithms, in order to allow for the duplication of the computational process.
In other words, even if a computation uses randomness in order to calculate a value, the resulting value will always be the same provided that the provided inputs are unchanged.
This **purity** (the same inputs producing the same output) allows allows a stochastic process (one which involves randomness) to be represented as a _purely deterministic_ and _declarative_ computation.
The stochastic nature of the data structure is, however, only relevant when an `RVar` instance is evaluated to determine the computed value.

> ##### RVar
>
> An instance of `RVar` represents a computation that when evaluated, produces a value with randomness applied.
## The Basics of RVar

Instances of `RVar` cannot be created without making use of the predefined functions, homed within the `RVar` companion object.
These functions allow for the creation of basic types with applied randomness and include:

- Integers (`Ints` and `Long`)
- Floating point numbers (`Float` and `Double`)
- Booleans (`True`/`False` values)
- Constant values where the randomness is not necessary - useful when a constant value needs to be used within a calculation of `RVar`s.

Other values are built up using these base instances quite easily.
Additionally, functions that operate on the types mentioned above are available.
These operations are common operations which would be tedious for the user to maintain and have been included as part of the library.
It is recommended to look at the [documentation for `RVar`]() to become familiar with the available combinator functions.

## Some example usage

To make the statements in the previous section a little more concrete, let's examine some usage examples.
Assume that you need to create a list of random values.
Now, because we are using a statically-typed language, we cannot mix and match different `RVar` instances without actually losing information about the list contents.

Losing information implies that the type which may represent all the types within the list becomes more and more general.
The more general the type becomes, less of the known specific information for the underlying type remains.
For example, in Java a list containing several different types within it will be represented as a `List<Object>`.
The contents of the list are known to be of type `Object`, but not much can be achieved with this knowledge.
`java.lang.Object` does not allow for many methods which means that mon many relevant operations are possible.
Such situations tend to force the use of reflection, which produces fragile and unverifiable programs.

For this reason, the `RVar` companion object allows us to create lists of values using the already defined instances:

```scala mdoc
val listOfInts = RVar.ints(12)
```

So `listOfInts` is a `List[Int]` of 12 elements that are all randomly generated but contained within a `RVar` computation.
It is important to observe that the value of `listOfInt` is _not actually_ 12 random integers, but is rather a computation which when evaluated will produce a list of randomised integers.
Instances of `RVar` may be combined to produce new `RVar` values though the predefined combinator functions and the _for-comprehension_ of Scala.
[//]: # ( The combination of `RVar` values into new values uses the available **Functor, Applicative and Monad** instances for `RVar`.)

Let's now take `listOfInts` and do something with the internal `List[Int]`.

```scala mdoc
val doubledListOfInts =
for {
list <- listOfInts
} yield list.map(x => x * 2)
```

The value `doubledListOfInts` extracted the list contained within the `listOfInts` and then yielded a result which multiplied each of the elements in the list by 2.
The same operation may also be achieved using the `map` method on `RVar`:

```scala mdoc
val doubledListOfIntsAlternative =
listOfInts.map(_.map(x => x * 2))
```

The result of `doubledListOfInts` and `doubledListOfIntsAlternative` is a `RVar` value, nothing else.
The actual value of the list is _still not known_ when the values are transformed.
Essentially, the `RVar` computation became a richer computation which would first generate the randomised values and then apply a transformation on these values to ultimately produce the final result.
That's quite convenient.
We can now create new `RVar`s using existing `RVar` values and use any method to create these values that is the most comfortable.

Now that we can create these `RVar` instances, we would need to evaluate them in order to produce the presented random value.


## Evaluating RVar instances

It should be stressed that the internal value of `RVar` should not be a concern to the user.
Even though we are playing with these instances now, it should be noted that the library will produce values that should only be evaluated at the "end of the world".
The "end of the world" means that the `RVar`s are evaluated at the entry point to your program (often in the `main` function).
Up until that evaluation point, the `RVar` instances describe the computation including randomness and are often referred to as sub-programs of the main program.

Now, to evaulate a `RVar` instance, the `RVar` needs to be _evaluated_.
This evaluation requires a `RNG` value.
`RNG` is an instance of a pseudo-random number generator (PRNG).
The CMWC generator is recommended due to the good performance properties and overall power of the generator itself.
`RNG` creation is similar to that of `RVar`, in that the `RNG` companion object's functions should be used to create the values.
Pseudo-random number generators require a starting point, or a _seed_ value.
This is the single value that initializes the internal state of the generator, and is the single value required to produce the same result from the same `RVar` program.
The ability to reproduce values, even though randomness is involved, is the most crucial feature of the library.
Reproduction allows others to obtain the same results for a given experiment / algorithm execution, which in turn allows for simplified error correction and validation of results.

Let's create a `RNG` value and evaluate one of the previously created `RVar` instances:

```scala mdoc
// Our seed is 1234
val rng = RNG.init(1234)

val result = doubledListOfInts.run(rng)
```

The value of `result` is interesting.
It is a tuple of two values, a `RNG` as well as a `List[Int]` of evaluated randomised integers.
The `RNG` part of the result is the new state of the `RNG`, after the complete evaluation of the `RVar` computation.
By returning the updated `RNG` state, the state may be threaded into a subsequent `RVar` computation.

Provided the same `RNG` state is used and the same sequence of `RVar` instances are evaluated, the exact same solution is produced.
It might seem that such threading could be error prone.
The `RNG` threading **is** error prone and also the reason why `RVar` is responsible for such threading, instead of the user.
`RVar` is also a stack-safe computation, meaning that the composition of `RVar` instances will not result in a stack-overflow error which is common in highly recursive logic.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions docs/community-tutorial/step.md → docs/basics/step.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ id: step
title: Step
---

Content currently under development

<!--
What is this mysterious data type called `Step`? Well, it actually
represents a *step* in an algorithm. Nothing mysterious at all. How
does this happen? `Step` is a monad transformer and because monad
Expand Down Expand Up @@ -318,3 +321,4 @@ We also learnt some valuable skills in this chapter such as:
But what if I told you `Step` has a sibling?
*gasp*.
In the next chapter we are going to look at `StepS` which represents a step with a state.
-->
4 changes: 4 additions & 0 deletions docs/community-tutorial/stepS.md → docs/basics/stepS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ id: step-with-state
title: StepS
---

Content currently under development

<!--
At this point, we now understand what `Step` is.
`StepS` is very similar but now we are creating a step with a state.
Expand Down Expand Up @@ -217,3 +220,4 @@ The type of the initial state value is determined by the second type parameter o
</div>
In the next chapter we will gain some practical experience with `Step` as well as a bit of `StepS`
-->
File renamed without changes.
Loading

0 comments on commit 17bddf5

Please sign in to comment.