Skip to content

Commit

Permalink
Merge pull request #1 from Lessnic/release-ACO
Browse files Browse the repository at this point in the history
Release aco
  • Loading branch information
Lessnic authored and Lessnic committed May 7, 2016
2 parents d2756ba + 9225ed5 commit ee3cd59
Show file tree
Hide file tree
Showing 101 changed files with 3,936 additions and 1,013 deletions.
9 changes: 5 additions & 4 deletions README.md
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.
55 changes: 35 additions & 20 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Computer and algorithm interaction simulation software (CAISS).
* Copyright (C) 2016 Sergei Pomelov
* 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
Expand All @@ -17,11 +17,12 @@
*/

plugins {
id 'java'
id 'idea'
id 'com.github.johnrengelman.shadow' version '1.2.3'
id 'me.champeau.gradle.jmh' version '0.3.0'
id 'net.ltgt.apt' version '0.6'
id "java"
id "idea"
// jmh staff next
id "com.github.johnrengelman.shadow" version "1.2.3"
id "me.champeau.gradle.jmh" version "0.3.0"
id "net.ltgt.apt" version "0.6"
}

repositories {
Expand All @@ -30,29 +31,43 @@ repositories {
}

sourceCompatibility = JavaVersion.VERSION_1_8
//noinspection GroovyUnusedAssignment, while it works don't touch it
//noinspection GroovyUnusedAssignment - false, while it works don't touch it
targetCompatibility = sourceCompatibility

tasks.withType(AbstractCompile)*.options*.encoding = "UTF-8"

jmh.jmhVersion = '1.12'
tasks.jmh.main = 'benchmarks.control.JMHBenchmarksRunner'
jmh.jmhVersion = "1.12"
// the main class where other test classes should be registered.
tasks.jmh.main = "benchmarks.control.JMHBenchmarksRunner"

//noinspection GroovyAssignabilityCheck - false, while it works don"t touch it
sourceSets {
main {
java {
srcDir "src/main/java"
}
resources {
srcDir "src/main/resources"
}
}
}

jmhJar {
//noinspection GroovyAssignabilityCheck, while it works don't touch it
manifest.attributes 'Main-Class': tasks.jmh.main
append 'META-INF/BenchmarkList'
append 'META-INF/CompilerHints'
//noinspection GroovyAssignabilityCheck - false, while it works don"t touch it
manifest.attributes "Main-Class": tasks.jmh.main
append "META-INF/BenchmarkList"
append "META-INF/CompilerHints"
}

dependencies {
apt "org.openjdk.jmh:jmh-generator-annprocess:${jmh.jmhVersion}"
apt "org.openjdk.jmh:jmh-generator-annprocess:${jmh.jmhVersion}" // jmh benchmarking

compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
compile "org.slf4j:slf4j-api:1.7.+"
compile "ch.qos.logback:logback-classic:1.1.+"
compile 'com.google.code.findbugs:jsr305:3.0.+'
compile 'com.google.guava:guava:19.0'
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"// jmh
compile "org.slf4j:slf4j-api:1.7.+" // logging-api
compile "ch.qos.logback:logback-classic:1.1.+" // logger
compile "com.google.code.findbugs:jsr305:3.0.+" // QA annotations
compile "com.google.guava:guava:19.+" // commons lib

testCompile 'junit:junit:4.12'
testCompile "junit:junit:4.+" // main tests lib
testCompile "org.mockito:mockito-core:1.10.+" // mocks
}
105 changes: 105 additions & 0 deletions src/main/java/benchmarks/ants/AntColonyInteraction.java
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);
}

}
80 changes: 80 additions & 0 deletions src/main/java/benchmarks/ants/AntsColonies.java
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;
}
}
Loading

0 comments on commit ee3cd59

Please sign in to comment.