-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Lessnic/release-ACO
Release aco
- Loading branch information
Showing
101 changed files
with
3,936 additions
and
1,013 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
# Computer and algorithm interaction simulation software (CAISS). | ||
Simulation software contains in core a representation of architecture and algorithm. | ||
Intended for simulation their interactions and listing their results for optimization | ||
purposes. | ||
Simulation software contains inside its core implemented with respect to mathematical model | ||
representations of architecture and algorithm. Intended for simulation their interactions and | ||
listing results for optimization purposes. It, in addition, includes a few benchmarks to comparison | ||
with a real performance with simulation results for improving and testing purposes. | ||
|
||
### Running in IDEA | ||
Select the `benchmarks.{desired runnable}` and `Run as a console Java application`. | ||
Just select the runnable classes and run as a console Java application. | ||
|
||
# License | ||
GNU GENERAL PUBLIC LICENSE Version 3. |
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
105 changes: 105 additions & 0 deletions
105
src/main/java/benchmarks/ants/AntColonyInteraction.java
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,105 @@ | ||
/* | ||
* Computer and algorithm interaction simulation software (CAISS). | ||
* Copyright (C) 2016 Sergey Pomelov. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU 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 benchmarks.ants; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import benchmarks.ants.ant.AntRunResult; | ||
import benchmarks.ants.ant.RunningAnt; | ||
|
||
import static benchmarks.ants.PheromonesApplier.applyPheromones; | ||
|
||
/** | ||
* @author Sergey Pomelov on 04/05/2016. | ||
*/ | ||
@SuppressWarnings("StaticMethodOnlyUsedInOneClass") | ||
@ParametersAreNonnullByDefault | ||
final class AntColonyInteraction { | ||
|
||
private AntColonyInteraction() { /* package local utility class*/ } | ||
|
||
static Callable<Long> interactionProcedure( | ||
AntsSettings settings, float[][] trails, AntsStatistics statistics, | ||
Collection<Integer> bestRunVertexes, AtomicBoolean gotNewSolution) { | ||
|
||
return () -> { | ||
final AntRunResult runResult = | ||
new RunningAnt(settings.getGraph(), trails).getRunResult(); | ||
final String runJournal = runResult.getJournal(); | ||
final long runLength = runResult.getLength(); | ||
//log.debug("Colony {}, ant run {} {}.", id, runJournal, statistics.getBestRunLength()); | ||
statistics.addFinishedRun(runLength); | ||
takeActionsIfSolutionTheBest(runResult, runLength, statistics, bestRunVertexes, | ||
gotNewSolution, false, runJournal, settings, trails); | ||
return runLength; | ||
}; | ||
} | ||
|
||
static void takeActionsIfSolutionTheBest(AntRunResult runResult, | ||
AntsStatistics statistics, | ||
Collection<Integer> bestRunVertexes, | ||
AtomicBoolean gotNewSolution, | ||
boolean gotOutside, | ||
AntsSettings settings, | ||
float[][] trails) { | ||
takeActionsIfSolutionTheBest(runResult, runResult.getLength(), statistics, bestRunVertexes, | ||
gotNewSolution, gotOutside, runResult.getJournal(), settings, trails); | ||
} | ||
|
||
private static void takeActionsIfSolutionTheBest(AntRunResult runResult, | ||
long runLength, | ||
AntsStatistics statistics, | ||
Collection<Integer> bestRunVertexes, | ||
AtomicBoolean gotNewSolution, | ||
boolean gotOutside, | ||
String runJournal, | ||
AntsSettings settings, | ||
float[][] trails) { | ||
if (runResult.isSuccess()) { | ||
if (runLength < statistics.getBestRunLength()) { | ||
changeTheBestSolution(runResult, bestRunVertexes, gotNewSolution, | ||
statistics, gotOutside, runJournal); | ||
} | ||
applyPheromones(runResult, settings, trails); | ||
} | ||
} | ||
|
||
private static void changeTheBestSolution(AntRunResult runResult, | ||
Collection<Integer> bestRunVertexes, | ||
AtomicBoolean gotNewSolution, | ||
AntsStatistics statistics, | ||
boolean gotOutside, | ||
String runJournal) { | ||
|
||
final int[] currentRunTour = runResult.getTour(); | ||
bestRunVertexes.clear(); | ||
for (final int vertex : currentRunTour) { | ||
bestRunVertexes.add(vertex); | ||
} | ||
if (!gotOutside) { | ||
gotNewSolution.set(true); | ||
} | ||
statistics.setNewBestRun(runResult, runJournal); | ||
} | ||
|
||
} |
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,80 @@ | ||
/* | ||
* Computer and algorithm interaction simulation software (CAISS). | ||
* Copyright (C) 2016 Sergey Pomelov. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU 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 benchmarks.ants; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.annotation.Nonnegative; | ||
import javax.annotation.Nonnull; | ||
|
||
import benchmarks.ants.parallelisation.ParallelExecutor; | ||
import util.Restrictions; | ||
|
||
/** | ||
* @author Sergey Pomelov on 02/05/2016. | ||
*/ | ||
final class AntsColonies { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AntsColonies.class); | ||
|
||
private AntsColonies() { /* utility class */ } | ||
|
||
static Long runCalculations(int coloniesAmount, int antsPerColony, | ||
AntsSettings settings) { | ||
Restrictions.ifContainsNullFastFail(settings); | ||
Restrictions.ifNotOnlyPositivesFastFail(coloniesAmount, antsPerColony); | ||
return generateSolutions(coloniesAmount, settings).first(); | ||
} | ||
|
||
private static SortedSet<Long> generateSolutions(@Nonnegative int coloniesAmount, | ||
@Nonnull AntsSettings settings) { | ||
final SortedSet<Long> solutions = new TreeSet<>(); | ||
ParallelExecutor.runOnce( | ||
generateAgents(coloniesAmount, settings).stream() | ||
.map(colony -> (Runnable) () -> | ||
solutions.add(colony.run(settings.getRunPeriodNanos()))) | ||
.collect(Collectors.toList()), | ||
"start", "colony"); | ||
if (log.isDebugEnabled()) { | ||
log.debug("Solutions: {}, the best {}.", solutions, solutions.first()); | ||
} | ||
return solutions; | ||
} | ||
|
||
private static Collection<IAntsColony> generateAgents(@Nonnegative int coloniesAmount, | ||
@Nonnull AntsSettings settings) { | ||
final List<IAntsColony> colonies = new ArrayList<>(coloniesAmount); | ||
for (int i = 0; i < coloniesAmount; i++) { | ||
//noinspection ObjectAllocationInLoop, bydesign | ||
final IAntsColony colony = new AntsColony(String.valueOf(i + 1), coloniesAmount, | ||
settings); | ||
colonies.add(colony); | ||
} | ||
colonies.forEach(colony -> colony.addNeighbours(colonies)); | ||
return colonies; | ||
} | ||
} |
Oops, something went wrong.