-
Notifications
You must be signed in to change notification settings - Fork 3
Container #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JusteRaimbault
wants to merge
9
commits into
ISCPIF:master
Choose a base branch
from
JusteRaimbault:container
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Container #1
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fd9e6f5
branching
JusteRaimbault e27434f
Containers_implementation
JusteRaimbault de40424
Merge remote-tracking branch 'upstream/master' into container
JusteRaimbault bc40a5c
reorganisation
JusteRaimbault c8ddb70
modifs
JusteRaimbault 7c4bc4e
merged upstream
JusteRaimbault f931581
Merge branch 'master' of github.com:ISCPIF/spacematters into container
JusteRaimbault 818d89a
Implemented slow indicators through fast convolution
JusteRaimbault 9023119
minor changes
JusteRaimbault File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
model/src/main/scala/fr/iscpif/schelling/quantity/initial/Container.scala
This file contains hidden or 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,44 @@ | ||
| /* | ||
| * Copyright (C) 2015 Romain Reuillon | ||
| * | ||
| * 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.iscpif.schelling.quantity.initial | ||
|
|
||
| import fr.iscpif.schelling.quantity._ | ||
|
|
||
| import scala.util.Random | ||
|
|
||
| trait Container { | ||
|
|
||
| def size : Int | ||
|
|
||
| var values : Seq[Seq[Cell]] | ||
|
|
||
| def container(implicit rng: Random): Seq[Seq[Cell]] | ||
|
|
||
| def printContainer: Unit = { | ||
| values.foreach( | ||
| (row : Seq[Cell]) => { | ||
| row.foreach( | ||
| (c : Cell) => {print(c.capacity);print(" | ")} | ||
| ) | ||
| println() | ||
| } | ||
| ) | ||
|
|
||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better build a string here and return it than directly printing it. |
||
|
|
||
|
|
||
| } | ||
48 changes: 48 additions & 0 deletions
48
model/src/main/scala/fr/iscpif/schelling/quantity/initial/ExpMixtureContainer.scala
This file contains hidden or 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,48 @@ | ||
| package fr.iscpif.schelling.quantity.initial | ||
|
|
||
| import fr.iscpif.schelling.quantity.Cell | ||
|
|
||
| import scala.util.Random | ||
|
|
||
| trait ExpMixtureContainer <: Container { | ||
|
|
||
| /** maximal capacity C_m */ | ||
| def maxCapacity : Int | ||
|
|
||
| /** Size of exponential kernels, of the form C_m*exp(-||x-x_0||/r_0) */ | ||
| def kernelRadius : Double | ||
|
|
||
| /** Number of exponential kernels */ | ||
| def centersNumber : Int | ||
|
|
||
| var values : Seq[Seq[Cell]] = null | ||
|
|
||
| def container(implicit rng: Random) = { | ||
| val arrayVals = Array.fill[Cell](size, size) { | ||
| new Cell(0, 0, 0) | ||
| } | ||
|
|
||
| // generate random center positions | ||
| val centers = Array.fill[Int](centersNumber, 2) { | ||
| rng.nextInt(size) | ||
| } | ||
|
|
||
| for (i <- 0 to size - 1; j <- 0 to size - 1) { | ||
| for (c <- 0 to centersNumber - 1) { | ||
| arrayVals(i)(j).capacity = arrayVals(i)(j).capacity + maxCapacity * math.exp(-math.sqrt(math.pow((i - centers(c)(0)), 2) + math.pow((j - centers(c)(1)), 2)) / kernelRadius) | ||
| //println(i + " " + j + " : " + arrayVals(i)(j).capacity) | ||
| } | ||
| } | ||
|
|
||
| //println(arrayVals.toSeq) | ||
| //val a = arrayVals.map({a:Array[Cell]=>a.toSeq}) | ||
| //for(k <- 0 to a.length - 1){println(a(k))} | ||
| //println(arrayVals.map({a:Array[Cell]=>a.toSeq}).toSeq) | ||
| //values = arrayVals.map({ a: Array[Cell] => a.toSeq }).toSeq | ||
|
|
||
| values = Seq.tabulate(size,size){(i:Int,j:Int)=>Cell(arrayVals(i)(j).capacity,0,0) } | ||
|
|
||
| values | ||
| } | ||
|
|
||
| } |
114 changes: 114 additions & 0 deletions
114
model/src/main/scala/fr/iscpif/schelling/quantity/initial/PrefAttDiffusionContainer.scala
This file contains hidden or 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,114 @@ | ||
| package fr.iscpif.schelling.quantity.initial | ||
|
|
||
| import fr.iscpif.schelling.quantity.Cell | ||
|
|
||
| import scala.util.Random | ||
|
|
||
| trait PrefAttDiffusionContainer <: Container { | ||
|
|
||
| /** sum of all capacities */ | ||
| def totalCapacity : Double | ||
|
|
||
| /** diffusion parameters */ | ||
| def diffusion : Double | ||
| def diffusionSteps : Int | ||
|
|
||
| /** Growth rate */ | ||
| def growthRate : Int | ||
|
|
||
| /** Preferential attachment parameter */ | ||
| def alphaAtt : Double | ||
|
|
||
|
|
||
|
|
||
|
|
||
| var values : Seq[Seq[Cell]] = null | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /** | ||
| * | ||
| * @param rng | ||
| * @return | ||
| */ | ||
| def container(implicit rng: Random) = { | ||
| var arrayVals = Array.fill[Cell](size, size){new Cell(0,0,0)} | ||
| var population :Double = 0 | ||
|
|
||
| while(population < totalCapacity) { | ||
|
|
||
| // add new population following pref att rule | ||
| if(population==0){ | ||
| //choose random patch | ||
| for(_<- 1 to growthRate){val i = rng.nextInt(size);val j = rng.nextInt(size) ; arrayVals(i)(j).capacity = arrayVals(i)(j).capacity + 1 } | ||
| } else{ | ||
| val oldPop = arrayVals.clone() | ||
| val ptot = oldPop.flatten.map((c:Cell)=>math.pow(c.capacity / population , alphaAtt)).sum | ||
|
|
||
| /* | ||
| val drawings = Array.fill[Double](growthRate){rng.nextDouble()} | ||
| Sorting.quickSort(drawings) | ||
| var s = 0.0; var i =0;var j =0;var k=0; | ||
| oldPop.flatten.foreach( | ||
| (c:Cell)=>{ | ||
| s=s+(math.pow(c.capacity/population,alphaAtt)/ptot) | ||
| while(s<drawings(k)){arrayVals(i)(j).capacity = arrayVals(i)(j).capacity + 1;k=k+1;} | ||
| j=j+1;if(j==size){j=0;i=i+1}; | ||
| } | ||
| ) | ||
| */ | ||
|
|
||
| for(_<- 1 to growthRate){ | ||
| var s = 0.0 ; val r = rng.nextDouble() ; var i =0;var j =0 | ||
| while(s<r){s=s+(math.pow(oldPop(i)(j).capacity/population,alphaAtt)/ptot);j=j+1;if(j==size){j=0;i=i+1}} | ||
| if(j==0){j=size-1;i = i - 1}else{j=j-1}; | ||
| arrayVals(i)(j).capacity = arrayVals(i)(j).capacity + 1 | ||
| } | ||
| } | ||
|
|
||
| // diffuse | ||
| for (_ <- 1 to diffusionSteps) { | ||
| arrayVals = diffuse(arrayVals, diffusion) | ||
| } | ||
|
|
||
| // update total population | ||
| population = arrayVals.flatten.map(_.capacity).sum | ||
|
|
||
| //println("population : "+population) | ||
| } | ||
|
|
||
| values = Seq.tabulate(size,size){(i:Int,j:Int)=>Cell(arrayVals(i)(j).capacity,0,0) } | ||
| values | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Diffuse to neighbors proportion alpha of capacities | ||
| * @param a | ||
| */ | ||
| def diffuse(a : Array[Array[Cell]],alpha : Double): Array[Array[Cell]] = { | ||
| val newVals = a.clone() | ||
| for (i <- 0 to size - 1; j <- 0 to size - 1) { | ||
| if(i>=1){newVals(i-1)(j).capacity = newVals(i-1)(j).capacity + (alpha / 8)*a(i)(j).capacity ; newVals(i)(j).capacity = newVals(i)(j).capacity - (alpha / 8)*a(i)(j).capacity } | ||
| if(i<size-1){newVals(i+1)(j).capacity = newVals(i+1)(j).capacity + (alpha / 8)*a(i)(j).capacity ; newVals(i)(j).capacity = newVals(i)(j).capacity - (alpha / 8)*a(i)(j).capacity } | ||
| if(j>=1){newVals(i)(j-1).capacity = newVals(i)(j-1).capacity + (alpha / 8)*a(i)(j).capacity ; newVals(i)(j).capacity = newVals(i)(j).capacity - (alpha / 8)*a(i)(j).capacity } | ||
| if(j<size-1){newVals(i)(j+1).capacity = newVals(i)(j+1).capacity + (alpha / 8)*a(i)(j).capacity ; newVals(i)(j).capacity = newVals(i)(j).capacity - (alpha / 8)*a(i)(j).capacity } | ||
| if(i>=1&&j>=1){newVals(i-1)(j-1).capacity = newVals(i-1)(j-1).capacity + (alpha / 8)*a(i)(j).capacity ; newVals(i)(j).capacity = newVals(i)(j).capacity - (alpha / 8)*a(i)(j).capacity } | ||
| if(i>=1&&j<size-1){newVals(i-1)(j+1).capacity = newVals(i-1)(j+1).capacity + (alpha / 8)*a(i)(j).capacity ; newVals(i)(j).capacity = newVals(i)(j).capacity - (alpha / 8)*a(i)(j).capacity } | ||
| if(i<size-1&&j>=1){newVals(i+1)(j-1).capacity = newVals(i+1)(j-1).capacity + (alpha / 8)*a(i)(j).capacity ; newVals(i)(j).capacity = newVals(i)(j).capacity - (alpha / 8)*a(i)(j).capacity } | ||
| if(i<size-1&&j<size-1){newVals(i+1)(j+1).capacity = newVals(i+1)(j+1).capacity + (alpha / 8)*a(i)(j).capacity ; newVals(i)(j).capacity = newVals(i)(j).capacity - (alpha / 8)*a(i)(j).capacity } | ||
| } | ||
| newVals | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
model/src/main/scala/fr/iscpif/schelling/quantity/initial/RandomContainer.scala
This file contains hidden or 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,22 @@ | ||
| package fr.iscpif.schelling.quantity.initial | ||
|
|
||
| import fr.iscpif.schelling.quantity.Cell | ||
|
|
||
| import scala.util.Random | ||
|
|
||
|
|
||
| trait RandomContainer <: Container { | ||
|
|
||
| def maxCapacity : Int | ||
|
|
||
| var values : Seq[Seq[Cell]] = null | ||
|
|
||
| def container(implicit rng: Random) = { | ||
| values = Seq.fill(size, size) { | ||
| val capacity: Double = rng.nextInt(maxCapacity).toDouble | ||
| Cell(capa = capacity, green = 0, red = 0) | ||
| } | ||
| values | ||
| } | ||
|
|
||
| } |
46 changes: 46 additions & 0 deletions
46
model/src/main/scala/fr/iscpif/schelling/quantity/test/TestContainer.scala
This file contains hidden or 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,46 @@ | ||
| package fr.iscpif.schelling.quantity.test | ||
|
|
||
| import fr.iscpif.schelling.quantity.Schelling | ||
| import fr.iscpif.schelling.quantity.initial._ | ||
| import fr.iscpif.schelling.quantity.move._ | ||
| import fr.iscpif.schelling.quantity.stop._ | ||
|
|
||
| import scala.util.Random | ||
|
|
||
| object TestContainer extends App { | ||
|
|
||
| val t = System.currentTimeMillis() | ||
|
|
||
| implicit val rng = new Random | ||
|
|
||
| val simulation = new Schelling with RandomState with PrefAttDiffusionContainer with RandomMoves with SpeilmanStop { | ||
| override def size: Int = 50 | ||
| override def greenRatio: Double = 0.5 | ||
| override def redRatio: Double = 0.35 | ||
| override def maxCapacity: Int = 50 | ||
| override def similarWanted: Double = 0.4 | ||
|
|
||
| /*// exp Mixture params | ||
| override def kernelRadius = 0.5 | ||
| override def centersNumber = 2 | ||
| */ | ||
|
|
||
| //prefAttdiff | ||
| override def totalCapacity :Double = 10000 | ||
| override def diffusion : Double = 0.02 | ||
| override def diffusionSteps : Int = 2 | ||
| override def growthRate : Int = 100 | ||
| override def alphaAtt : Double = 1.1 | ||
| } | ||
|
|
||
| // generate initial state | ||
| simulation.initialState(rng) | ||
|
|
||
| // print container | ||
| //simulation.printContainer | ||
|
|
||
| println(simulation.values.flatten.map(_.capacity).max) | ||
|
|
||
| println((System.currentTimeMillis()-t)/1000.0) | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be the return of the function container, not a shared mutable value.