Ver Fonte

Terminate solver job when no sub jobs generated

Henrik Kunzelmann há 3 anos atrás
pai
commit
9458959761
1 ficheiros alterados com 74 adições e 74 exclusões
  1. 74 74
      src/holeg/HolegPowerFlow.java

+ 74 - 74
src/holeg/HolegPowerFlow.java

@@ -103,92 +103,92 @@ public class HolegPowerFlow {
                 if (Thread.interrupted())
                     return GridSolverResult.Interrupted;
 
-                boolean[] resultUsed = new boolean[jobs.length];
-
-                // Start maximum number of threads
-                int jobThreadCount = Math.min(jobs.length, Math.max(1, settings.maxSolverThreads));
-                for (int i = 0; i < jobThreadCount; i++)
-                    jobs[i].start();
-
-                int nextJob = jobThreadCount;
-                while(true) {
-                    // Wait for any job to finish
-                    try {
-                        semaphore.acquire();
-                    }
-                    catch(InterruptedException ignored) {
-                        for (Thread job : jobs) job.interrupt();
-                        return GridSolverResult.Interrupted;
-                    }
+                // Check if actually we have jobs to run
+                if (jobs.length > 0) {
+                    boolean[] resultUsed = new boolean[jobs.length];
+
+                    // Start maximum number of threads
+                    int jobThreadCount = Math.min(jobs.length, Math.max(1, settings.maxSolverThreads));
+                    for (int i = 0; i < jobThreadCount; i++)
+                        jobs[i].start();
+
+                    int nextJob = jobThreadCount;
+                    while (true) {
+                        // Wait for any job to finish
+                        try {
+                            semaphore.acquire();
+                        } catch (InterruptedException ignored) {
+                            for (Thread job : jobs) job.interrupt();
+                            return GridSolverResult.Interrupted;
+                        }
 
-                    // Check results of all jobs
-                    boolean allSolved = true;
-
-                    synchronized (results) {
-                        for (int i = 0; i < jobs.length; i++) {
-                            // Already checked?
-                            if (resultUsed[i])
-                                continue;
-
-                            // Check result of the job
-                            PowerFlowProblem problem = problems[i];
-                            SolverResult result = results[i];
-                            if (result != null) {
-                                // Debug message
-                                if (PowerFlowAnalysisMenu.getInstance().shouldShowDebug() && (result.solved || i == solveTryCount - 1))
-                                    debugShowResultAsMessageBox(problem, result);
-
-                                // Solver was interrupted
-                                if (result.error == SolverError.Interrupted)
-                                    return GridSolverResult.Interrupted;
-
-                                // Solver was successful?
-                                if (result.solved) {
-                                    // Simply use the first solved result
-                                    if (settings.slackNodePlacementStrategy != SlackNodePlacementStrategy.MinimizeSlack) {
-                                        solved = true;
-                                        bestProblem = problem;
-                                        bestResult = result;
-                                        break;
-                                    }
+                        // Check results of all jobs
+                        boolean allSolved = true;
 
-                                    // Is this result better?
-                                    double slackPower = result.flowData.busInjection[0].lenSquared();
-                                    if (slackPower < minSlackPower) {
-                                        minSlackPower = slackPower;
-                                        bestProblem = problem;
-                                        bestResult = result;
+                        synchronized (results) {
+                            for (int i = 0; i < jobs.length; i++) {
+                                // Already checked?
+                                if (resultUsed[i])
+                                    continue;
+
+                                // Check result of the job
+                                PowerFlowProblem problem = problems[i];
+                                SolverResult result = results[i];
+                                if (result != null) {
+                                    // Debug message
+                                    if (PowerFlowAnalysisMenu.getInstance().shouldShowDebug() && (result.solved || i == solveTryCount - 1))
+                                        debugShowResultAsMessageBox(problem, result);
+
+                                    // Solver was interrupted
+                                    if (result.error == SolverError.Interrupted)
+                                        return GridSolverResult.Interrupted;
+
+                                    // Solver was successful?
+                                    if (result.solved) {
+                                        // Simply use the first solved result
+                                        if (settings.slackNodePlacementStrategy != SlackNodePlacementStrategy.MinimizeSlack) {
+                                            solved = true;
+                                            bestProblem = problem;
+                                            bestResult = result;
+                                            break;
+                                        }
+
+                                        // Is this result better?
+                                        double slackPower = result.flowData.busInjection[0].lenSquared();
+                                        if (slackPower < minSlackPower) {
+                                            minSlackPower = slackPower;
+                                            bestProblem = problem;
+                                            bestResult = result;
+                                        }
                                     }
-                                }
-                                resultUsed[i] = true;
+                                    resultUsed[i] = true;
 
-                            } else
-                                allSolved = false;
+                                } else
+                                    allSolved = false;
+                            }
                         }
-                    }
 
-                    // Either all are solved or we solved one job
-                    if (allSolved)
-                        solved = true;
-                    if (solved)
-                        break;
+                        // Either all are solved or we solved one job
+                        if (solved || allSolved)
+                            break;
 
-                    // Check if we should have stopped
-                    if (Thread.interrupted()) {
-                        for (Thread job : jobs) job.interrupt();
-                        return GridSolverResult.Interrupted;
-                    }
+                        // Check if we should have stopped
+                        if (Thread.interrupted()) {
+                            for (Thread job : jobs) job.interrupt();
+                            return GridSolverResult.Interrupted;
+                        }
 
-                    // Start next thread
-                    if (nextJob < jobs.length)
-                        jobs[nextJob++].start();
+                        // Start next thread
+                        if (nextJob < jobs.length)
+                            jobs[nextJob++].start();
+                    }
+                    // Stop every job
+                    for (Thread job : jobs) job.interrupt();
                 }
-                // Stop every job
-                for (Thread job : jobs) job.interrupt();
             }
 
             // Update grid if solved
-            if (solved) {
+            if (solved || bestResult != null) {
                 // Check limits
                 boolean error = false;
                 if (bestResult != null) {