-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cb1877
commit f557217
Showing
6 changed files
with
118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Created by Romain Reuillon on 19/04/16. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package fr.geocites.micmac | ||
|
||
import fr.geocites.micmac.context._ | ||
|
||
import scalaz._ | ||
import Scalaz._ | ||
|
||
object observable { | ||
|
||
def totalI(state: MicMacState) = | ||
state.flyingPlanes.map(_.sir.i).sum + | ||
state.network.airports.map(_.sir.i).sum | ||
|
||
def updateMaxStepI[M[_]: Monad](implicit obs: Observable[M], step: Step[M]) = Kleisli[M, MicMacState, MicMacState] { state: MicMacState => | ||
val totalIValue = totalI(state) | ||
|
||
def update(step: Long) = | ||
obs.maxIStep.modify { | ||
case Some(current) => | ||
if (totalIValue > current.value) Some(MaxIStep(step, totalIValue)) else Some(current) | ||
case None => Some(MaxIStep(step, totalIValue)) | ||
} | ||
|
||
for { | ||
s <- step.step.get | ||
_ <- update(s) | ||
} yield state | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Created by Romain Reuillon on 19/04/16. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package fr.geocites.micmac | ||
|
||
import fr.geocites.micmac.context.MicMacState | ||
|
||
import scalaz._ | ||
import Scalaz._ | ||
|
||
import observable._ | ||
|
||
object stop { | ||
|
||
type StopCondition[M[_]] = Kleisli[M, MicMacState, Boolean] | ||
|
||
def endOfEpidemy[M[_]: Monad](implicit observable: Observable[M], step: Step[M]): StopCondition[M] = Kleisli { state => | ||
def finished(maxIStep: Option[MaxIStep], step: Long) = | ||
maxIStep match { | ||
case Some(maxIStep) => | ||
if(step >= maxIStep.step + 1) totalI(state) < 1.0 | ||
else false | ||
case None => false | ||
} | ||
|
||
for { | ||
s <- step.step.get | ||
maxIStep <- observable.maxIStep.get | ||
} yield finished(maxIStep, s) | ||
} | ||
|
||
def stopAfter[M[_]: Monad](s: Long)(implicit step: Step[M]): StopCondition[M] = Kleisli { state => | ||
step.step.get.map { _ >= s } | ||
} | ||
|
||
def or[M[_]: Monad](s1: StopCondition[M], s2: StopCondition[M]) = | ||
for { | ||
c1 <- s1 | ||
c2 <- s2 | ||
} yield c1 || c2 | ||
|
||
} |