-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slots from geometry fix & Solver time limiter (#42)
Co-authored-by: janper <[email protected]>
- Loading branch information
Showing
6 changed files
with
1,186 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ public ComponentSolver( ) : base("Monoceros WFC Solver", | |
"Main") { | ||
} | ||
|
||
public override Guid ComponentGuid => new Guid("DD1A1FA6-ACD4-4202-8B1A-9840949644B3"); | ||
public override Guid ComponentGuid => new Guid("4C19261E-4137-41F5-A118-A977021B2FA2"); | ||
|
||
protected override System.Drawing.Bitmap Icon => Properties.Resources.solver; | ||
|
||
|
@@ -48,6 +48,11 @@ protected override void RegisterInputParams(GH_Component.GH_InputParamManager pM | |
"Maximum Number of Solver Attempts", | ||
GH_ParamAccess.item, | ||
10); | ||
pManager.AddIntegerParameter("Max Time", | ||
"T", | ||
"Maximum Time spent with Attempts (milliseconds). Negative and 0 = infinity", | ||
GH_ParamAccess.item, | ||
0); | ||
pManager.AddBooleanParameter("Use Shannon Entropy", | ||
"E", | ||
"Whether to use Shannon Entropy instead of the simpler linear entropy calculations", | ||
|
@@ -99,6 +104,7 @@ protected override void SolveInstance(IGH_DataAccess DA) { | |
var rulesRaw = new List<Rule>(); | ||
var randomSeed = 42; | ||
var maxAttempts = 10; | ||
var maxTime = 0; | ||
bool useShannonEntropy = false; | ||
var maxObservations = 0; | ||
|
||
|
@@ -126,11 +132,15 @@ protected override void SolveInstance(IGH_DataAccess DA) { | |
return; | ||
} | ||
|
||
if (!DA.GetData(5, ref useShannonEntropy)) { | ||
if (!DA.GetData(5, ref maxTime)) { | ||
return; | ||
} | ||
|
||
if (!DA.GetData(6, ref useShannonEntropy)) { | ||
return; | ||
} | ||
|
||
if (!DA.GetData(6, ref maxObservations)) { | ||
if (!DA.GetData(7, ref maxObservations)) { | ||
return; | ||
} | ||
|
||
|
@@ -456,6 +466,7 @@ protected override void SolveInstance(IGH_DataAccess DA) { | |
worldForSolver.ToList(), | ||
randomSeed, | ||
maxAttempts, | ||
maxTime, | ||
maxObservationsUint, | ||
entropy, | ||
out var solvedSlotPartsTree); | ||
|
@@ -597,6 +608,7 @@ private Stats Solve(List<RuleForSolver> rules, | |
List<Slot> slots, | ||
int randomSeed, | ||
int maxAttemptsInt, | ||
int maxTime, | ||
uint maxObservations, | ||
Entropy entropy, | ||
out List<List<string>> worldSlotPartsTree) { | ||
|
@@ -616,6 +628,8 @@ private Stats Solve(List<RuleForSolver> rules, | |
stats.ruleCount = (uint)rules.Count; | ||
stats.entropy = entropy; | ||
|
||
var limitTime = maxTime > 0; | ||
|
||
// | ||
// -- Adjacency rules -- | ||
// | ||
|
@@ -826,6 +840,8 @@ private Stats Solve(List<RuleForSolver> rules, | |
|
||
uint attempts = 0; | ||
|
||
var timeStart = DateTime.UtcNow; | ||
|
||
unsafe { | ||
while (true) { | ||
observationResult = Native.wfc_observe(wfcWorldStateHandle, | ||
|
@@ -837,7 +853,8 @@ private Stats Solve(List<RuleForSolver> rules, | |
|
||
if (observationResult == WfcObserveResult.Deterministic | ||
|| observationResult == WfcObserveResult.Nondeterministic | ||
|| attempts == maxAttempts) { | ||
|| attempts == maxAttempts | ||
|| limitTime && ((DateTime.UtcNow - timeStart).TotalMilliseconds > maxTime)) { | ||
break; | ||
} | ||
Native.wfc_world_state_clone_from(wfcWorldStateHandle, wfcWorldStateHandleBackup); | ||
|
@@ -863,7 +880,13 @@ private Stats Solve(List<RuleForSolver> rules, | |
stats.solveAttempts = attempts; | ||
|
||
if (stats.contradictory) { | ||
stats.report = "WFC solver failed to find solution within " + maxAttempts + " attempts"; | ||
if (attempts == maxAttempts) { | ||
stats.report = "WFC solver failed to find solution within " + maxAttempts + " attempts"; | ||
} else if (limitTime && attempts < maxAttempts) { | ||
stats.report = "WFC solver failed to find solution within time limit " + maxTime + " milliseconds"; | ||
} else { | ||
stats.report = "WFC solver failed to find solution for unknown reason. Please report this error, including screenshots, Rhino file and Grasshopper file at [email protected]. Thank you!"; | ||
} | ||
} | ||
|
||
unsafe { | ||
|
Oops, something went wrong.