Skip to content

Commit

Permalink
Merge a09c0d8 into f5e1f0b
Browse files Browse the repository at this point in the history
  • Loading branch information
FedorSmirnov89 committed Apr 2, 2018
2 parents f5e1f0b + a09c0d8 commit 4bf43cc
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 16 deletions.
15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
language: java
install: true
dist: trusty

before_install:
- sudo apt-get update
- sudo apt-get install jq
- wget -O ~/codacy-coverage-reporter-assembly-latest.jar $(curl https://api.github.com/repos/codacy/codacy-coverage-reporter/releases/latest | jq -r .assets[0].browser_download_url)

after_success:
- "./gradlew coveralls"
- ./gradlew coveralls
- gradlew jacocoRootReport
- java -jar ~/codacy-coverage-reporter-assembly-latest.jar report -l Java -r build/reports/jacoco/jacocoRootReport/jacocoRootReport.xml

before_deploy:
- "./gradlew assemble website"
Expand All @@ -19,10 +27,7 @@ deploy:
tags: true
- provider: pages
skip-cleanup: true
api_key:
secure: dEolMpa639YJvocR44Qltm3SJ3AyFMNL9W8/ODwI1xI5lmZv4uMVkiiy6YWMjsphvq1+ATNv11fXcHRCCaQyP4Tddz43TLzUhvaOKBfPagnT0N/9mUkUX3XbzaIWT+9kqO9XdcqkLJbppF4Xj/p3ICivwENvqbarQjPzAAUpVax/LLxhf5rMErdktJu1nmd57YJdBElZyq5j4lYzUvXoLNyTuEc7DOKZwPAUGk8qvqcofVbDdoR4obk4y/LsuApf967y6+Re/4I7kcNVNYF4UfA/sGSd5O4yRp5VjpP2UAsYy6D965fCafCVIEb7xobgoIluwp7jbCRSA9PrK/jfvfD0BLKQgVpTSoBItOZMhggrSfdDmE0xn/y9GLcGIscjvaYxmSn0/5TTwhQgFQza9YXSozRZlCzbSjZLth8+CbkM09zler01+pi5B0h/QskYTIaIdbWGC8Ux1TrneCN4N1qEJQarfMtbtYh3bq9tNVuDb83r0bqygpioTWd6kdWjV03ECEG4+TufZYtBduBQPi+BbAcN8mlGjXCRulGdMOAtYtOttuvU/vgSWFWoUCIwrqwt/aGN8tjX5l+MwVWWS7SGLjF1J3cUgYde1axEZPKJHzlghtBjH1+UQ9bwpPdNb1Bhy5AI1Iz45nxjvkyhXrOdpYfvUuHH/cM18tdFUr8=
# was the following instead of api_key:
# github-token: # Set in travis-ci.org dashboard, marked secure
github-token: $GITHUB_TOKEN # Set in travis-ci.org dashboard, marked secure
keep-history: true
local-dir: ./build/website
on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/

package org.opt4j.core.genotype;

import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import org.opt4j.core.Genotype;
Expand All @@ -44,8 +46,8 @@
* Example usage: <blockquote>
*
* <pre>
* SelectMapGenotype&lt;Ball, Color&gt; genotype = new SelectMapGenotype&lt;Ball, Color&gt;(Arrays.asList(ball1, ball2, ball3, ball4,
* ball5), Arrays.asList(Color.BLUE, Color.GREEN, Color.RED));
* SelectMapGenotype&lt;Ball, Color&gt; genotype = new SelectMapGenotype&lt;Ball, Color&gt;(
* Arrays.asList(ball1, ball2, ball3, ball4, ball5), Arrays.asList(Color.BLUE, Color.GREEN, Color.RED));
* genotype.init(new Random());
* </pre>
*
Expand Down Expand Up @@ -74,6 +76,11 @@ protected static class SelectBounds<O, P> implements Bounds<Integer> {
public SelectBounds(List<O> list, Map<O, List<P>> map) {
this.list = list;
this.map = map;
for (Entry<O, List<P>> entry : map.entrySet()) {
if (entry.getValue().isEmpty()) {
throw new IllegalArgumentException("Empty value map provided for key " + entry.getKey());
}
}
}

@Override
Expand All @@ -97,11 +104,20 @@ public Integer getUpperBound(int index) {
*/
public SelectMapGenotype(List<K> keys, Map<K, List<V>> values) {
super(new SelectBounds<K, V>(keys, values));
if (!keys.containsAll(values.keySet()) || !values.keySet().containsAll(keys)) {
throw new IllegalArgumentException("The provided list does not match the provided map");
}
if (new HashSet<K>(keys).size() < keys.size()) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_NON_UNIQUE_KEYS);
}
this.keys = keys;
this.values = values;
}

private static <K, V> Map<K, List<V>> toMap(List<K> keys, List<V> values) {
if (new HashSet<K>(keys).size() < keys.size()) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_NON_UNIQUE_KEYS);
}
Map<K, List<V>> map = new HashMap<K, List<V>>();
for (K key : keys) {
map.put(key, values);
Expand Down Expand Up @@ -142,7 +158,7 @@ public void init(Random random) {
*/
@Override
public void init(Random random, int n) {
throw new UnsupportedOperationException("Use method init(Random) instead");
throw new UnsupportedOperationException(MapGenotype.ERROR_MESSAGE_UNSUPPORTED_INIT);
}

/*
Expand All @@ -162,6 +178,9 @@ public boolean containsKey(K key) {
*/
@Override
public int getIndexOf(K key) {
if (!containsKey(key)) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY);
}
return keys.indexOf(key);
}

Expand All @@ -174,11 +193,7 @@ public int getIndexOf(K key) {
public V getValue(K key) {
int i = getIndexOf(key);
int v = get(i);
assert v <= getUpperBound(i);
assert v >= getLowerBound(i);
List<V> valueList = values.get(key);

assert valueList.size() > v : "index " + v + " unavailable for list of key " + key + ": " + valueList;
return valueList.get(v);
}

Expand All @@ -190,14 +205,16 @@ public V getValue(K key) {
*/
@Override
public void setValue(K key, V value) {
int i = keys.indexOf(key);
int i = getIndexOf(key);
while (size() <= i) {
add(bounds.getLowerBound(i));
}

List<V> valueList = values.get(key);
if (!valueList.contains(value)) {
throw new IllegalArgumentException(
"The list provided for key " + key + " does not contain the value " + value);
}
int v = valueList.indexOf(value);

set(i, v);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package org.opt4j.core.genotype;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.Test;
import org.opt4j.core.genotype.SelectMapGenotype.SelectBounds;

public class SelectMapGenotypeTest {

public class Inputs {
protected List<Integer> list;
protected Map<Integer, List<Integer>> map;
protected List<Integer> first;
protected List<Integer> second;

public Inputs() {
this.list = new ArrayList<Integer>();
list.add(1);
list.add(2);
this.map = new HashMap<Integer, List<Integer>>();
this.first = new ArrayList<Integer>();
first.add(3);
this.second = new ArrayList<Integer>();
second.add(3);
second.add(4);
map.put(1, first);
map.put(2, second);
}
}

@Test(expected=IllegalArgumentException.class)
public void testSetWrongValue(){
Inputs inputs = new Inputs();
SelectMapGenotype<Integer, Integer> genotype = new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map);
genotype.init(new Random());
genotype.setValue(2, 5);
}

@Test
public void testSetValue(){
Inputs inputs = new Inputs();
SelectMapGenotype<Integer, Integer> genotype = new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map);
genotype.init(new Random());
genotype.setValue(2, 3);
assertEquals(3, (long) genotype.getValue(2));
genotype.setValue(2, 4);
assertEquals(4, (long) genotype.getValue(2));
}

@Test
public void testMapConstructor() {
Inputs inputs = new Inputs();
inputs.map.get(2).remove(1);
SelectMapGenotype<Integer, Integer> genotype = new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map);
Collection<Integer> keys = genotype.getKeys();
assertTrue(keys.contains(1));
assertTrue(keys.contains(2));
Random rand = new Random();
genotype.init(rand);
assertEquals("[1=3;2=3;]", genotype.toString());
assertTrue(genotype.containsKey(2));
assertFalse(genotype.containsKey(3));
assertNotEquals(genotype, genotype.newInstance());
assertEquals(0, genotype.getIndexOf(1));
assertEquals(3, (long) genotype.getValue(1));
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidKey() {
Inputs inputs = new Inputs();
SelectMapGenotype<Integer, Integer> geno = new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map);
geno.getIndexOf(4);
}

@Test(expected = IllegalArgumentException.class)
public void testListMapMismatch2() {
Inputs inputs = new Inputs();
inputs.map.put(3, new ArrayList<Integer>());
new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map);
}

@Test(expected = IllegalArgumentException.class)
public void testListMapMismatch1() {
Inputs inputs = new Inputs();
inputs.list.add(3);
new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map);
}

@Test(expected = IllegalArgumentException.class)
public void testNonUniqueKeys2() {
Inputs inputs = new Inputs();
inputs.list.add(1);
new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map.get(1));
}

@Test(expected = IllegalArgumentException.class)
public void testNonUniqueKeys1() {
Inputs inputs = new Inputs();
inputs.list.add(1);
new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map);
}

@Test(expected = UnsupportedOperationException.class)
public void testListConstructor() {
Inputs inputs = new Inputs();
SelectMapGenotype<Integer, Integer> genotype = new SelectMapGenotype<Integer, Integer>(inputs.list,
inputs.map.get(1));
Random rand = new Random();
genotype.init(rand, 2);
}

@Test(expected = IllegalArgumentException.class)
public void testEmptyMap() {
Inputs inputs = new Inputs();
inputs.list.add(3);
inputs.map.put(3, new ArrayList<Integer>());
new SelectBounds<Integer, Integer>(inputs.list, inputs.map);
}

@Test
public void testSelectBounds() {
Inputs inputs = new Inputs();
SelectBounds<Integer, Integer> bounds = new SelectBounds<Integer, Integer>(inputs.list, inputs.map);
assertEquals(0, (long) bounds.getLowerBound(0));
assertEquals(0, (long) bounds.getUpperBound(0));
assertEquals(0, (long) bounds.getLowerBound(1));
assertEquals(1, (long) bounds.getUpperBound(1));
}

}

0 comments on commit 4bf43cc

Please sign in to comment.