0

I tried to apply the simulation of the URL below by changing it to "SouthKorea" and "Seoul" using Mathematica 12.1.

https://community.wolfram.com/groups/-/m/t/1901002

The part where the error occurs in this code is:

enter code here 
statecolor = Association[0 -> Lighter@Gray, 1 -> Red, 2 -> Pink, 3 -> Darker@Red, 4 -> Brown, 10 -> Darker@Green, 11 -> Black];
color = Table[statecolor[[i]], {i, 1, 7}];
$infectedstates = 1 | 2 | 3 | 4;
Options[prepareRegion] = {}
prepareRegion[r : _Region | _Entity | _Polygon, targetarea_] := Module[{region, area, newregion}, 
If[MatchQ[r, _Entity], 
region = 
  EntityValue[r, "Polygon"] /. 
   g_GeoPosition :> g["LongitudeLatitude"];, region = r;];
   area = Area[region];
   If[! NumericQ[area], Return[{$Failed, $Failed}];];  newregion = RegionResize[region, 
 Abs[Subtract @@ RegionBounds[region][[1]]] Sqrt[targetarea/area]]; {newregion, RegionMember[newregion],  RescalingTransform[RegionBounds[region], 
 RegionBounds[newregion]]}];
createPositions[n_, region_, regionq_ : Automatic] := Module[{memberq = 
 Replace[regionq, Automatic :> RegionMember[region]], positions, 
i = 0}, positions = {};
   While[Length[positions] < n && i++ < n, 
positions = 
  DeleteDuplicates[
   Join[positions, 
    Select[Round@RandomPoint[region, n - Length[positions]], 
     memberq]]];];   If[Length[positions] < n, $Failed, positions]];   
createStates[n_, infected : {__Integer} | _Integer | UpTo[_Integer], isolationProp_] :=   Module[{states},  states = Table[{0, 0, RandomChoice[{isolationProp, 1 - isolationProp} -> {0, 1}]}, n];  If[! TrueQ[And @@ MatchQ[infected, _List]], Return[$Failed]];   Switch[infected, _List,    states[[infected]] = 
  ConstantArray[{1, 0, 1}, Length[infected]];, _UpTo, 
states[[;; infected[[1]]]] =  ConstantArray[{1, 0, 1}, infected[[1]]];, _, 
states[[RandomSample[Range[n], infected]]] =  ConstantArray[{1, 0, 1}, infected];]; states];
searchInfectionFocus[posisitions_, point_, transformation_, i_] := Nearest[posisitions -> "Index", transformation[point], i];
Options[createSimulation] := {"Infected" -> 1, "Density" -> Automatic,    "InfectionPoint" -> None, "Isolation" -> 0.1}; 
createSimulation[n_, r : _Region | _Entity | _Polygon, 
OptionsPattern[]] :=  Module[{region, regionq, positions, transform, infected, 
density = OptionValue["Density"] /. Automatic -> 1/10, ninfected, 
isolation, states}, {region, regionq, transform} = 
prepareRegion[r, n/density];   positions = createPositions[n, region];   
ninfected = OptionValue["Infected"];  Which[MatchQ[OptionValue["InfectionPoint"], _Entity], 
infected = 
  searchInfectionFocus[positions, 
   EntityValue[OptionValue["InfectionPoint"], "Position"][
    "LongitudeLatitude"], transform, ninfected];, 
MatchQ[OptionValue["InfectionPoint"], {_?NumericQ, _?NumericQ}], 
infected = 
  searchInfectionFocus[positions, OptionValue["InfectionPoint"], 
   transform, ninfected];, True, infected = ninfected;];   
isolation = OptionValue["Isolation"];   
states = createStates[n, infected, isolation];   
{region, regionq, positions, states}];
{region, regionq, initialposCase1, initialstatesCase1} = createSimulation[200, Entity["Country", "SouthKorea"], "Infected" -> 5, "Density" -> Automatic, 
 "InfectionPoint" ->  Entity["City", {"Seoul", "Seoul", "SouthKorea"}], "Isolation" -> 0]

When the above code is executed, the kernel often crashes. However, very occasionally, the same run will yield normal output. I'd like some help with what's wrong with the code above.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Milk
  • 1,688
  • 10
  • 9
  • 1
    It is unclear what you mean by "the kernel is initialized". Please clarify, and please add appropriate tags to the post. Do not create new tags, use existing tags instead. Do no create tags for function names. – Szabolcs Aug 09 '21 at 16:01
  • @Szabolcs I'm sorry. My expression was not good enough. When I execute the above, the results of all calculations that have already been executed are removed. This is equivalent to Remove["Global`*"]. – Milk Aug 10 '21 at 19:35
  • Got it. Normally we say that the kernel "crashed". I updated the post to clarify this. – Szabolcs Aug 10 '21 at 20:41

0 Answers0