Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/felixreimann/opt4j
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Reimann committed Mar 7, 2018
2 parents aa6ae55 + 493dc64 commit 3e934d2
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
* 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.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.opt4j.core.Genotype;

Expand All @@ -42,8 +44,8 @@
* Example usage: <blockquote>
*
* <pre>
* BooleanMapGenotype&lt;Switch&gt; genotype = new BooleanMapGenotype&lt;Switch&gt;(Arrays.asList(switch1, switch2, switch3, switch4,
* switch5));
* BooleanMapGenotype&lt;Switch&gt; genotype = new BooleanMapGenotype&lt;Switch&gt;(
* Arrays.asList(switch1, switch2, switch3, switch4, switch5));
* genotype.init(new Random());
* </pre>
*
Expand All @@ -69,6 +71,10 @@ public class BooleanMapGenotype<K> extends BooleanGenotype implements MapGenotyp
*/
public BooleanMapGenotype(List<K> list) {
super();
Set<K> uniqueKeys = new HashSet<K>(list);
if (uniqueKeys.size() < list.size()) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_NON_UNIQUE_KEYS);
}
this.list = list;
}

Expand All @@ -89,7 +95,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 @@ -99,6 +105,9 @@ public void init(Random random, int n) {
*/
@Override
public Boolean getValue(K key) {
if (!containsKey(key)) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY);
}
int i = list.indexOf(key);
return get(i);
}
Expand All @@ -111,6 +120,9 @@ public Boolean getValue(K key) {
*/
@Override
public void setValue(K key, Boolean value) {
if (!containsKey(key)) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY);
}
int i = list.indexOf(key);
while (size() <= i) {
add(false);
Expand Down Expand Up @@ -151,13 +163,15 @@ public <G extends Genotype> G newInstance() {
*/
@Override
public String toString() {
String s = "[";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (int i = 0; i < size(); i++) {
K key = list.get(i);
boolean value = this.get(i);
s += key + "=" + value + ";";
stringBuilder.append(key + "=" + value + ";");
}
return s + "]";
stringBuilder.append("]");
return stringBuilder.toString();
}

/*
Expand All @@ -167,6 +181,9 @@ public String toString() {
*/
@Override
public int getIndexOf(K key) {
if (!containsKey(key)){
throw new IllegalArgumentException(ERROR_MESSAGE_INVALID_KEY);
}
return list.indexOf(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
* 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.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.opt4j.core.Genotype;

Expand All @@ -42,8 +44,8 @@
* Example usage: <blockquote>
*
* <pre>
* DoubleMapGenotype&lt;Bottle&gt; genotype = new DoubleMapGenotype&lt;Bottle&gt;(Arrays.asList(bottle1, bottle2, bottle3, bottle4,
* bottle5));
* DoubleMapGenotype&lt;Bottle&gt; genotype = new DoubleMapGenotype&lt;Bottle&gt;(
* Arrays.asList(bottle1, bottle2, bottle3, bottle4, bottle5));
* genotype.init(new Random());
* </pre>
*
Expand Down Expand Up @@ -72,6 +74,10 @@ public class DoubleMapGenotype<K> extends DoubleGenotype implements MapGenotype<
*/
public DoubleMapGenotype(List<K> keys, Bounds<Double> bounds) {
super(bounds);
Set<K> uniqueKeys = new HashSet<K>(keys);
if (uniqueKeys.size() < keys.size()) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_NON_UNIQUE_KEYS);
}
this.keys = keys;
}

Expand All @@ -93,7 +99,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 Down Expand Up @@ -125,7 +131,12 @@ public Double getValue(K key) {
*/
@Override
public void setValue(K key, Double value) {
if (!containsKey(key)) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY);
}
int i = keys.indexOf(key);
if (value < bounds.getLowerBound(i) || value > bounds.getUpperBound(i))
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_OUT_OF_BOUNDS);
while (size() <= i) {
add(bounds.getLowerBound(i));
}
Expand Down Expand Up @@ -155,13 +166,15 @@ public <G extends Genotype> G newInstance() {
*/
@Override
public String toString() {
String s = "[";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (int i = 0; i < size(); i++) {
K key = keys.get(i);
double value = this.get(i);
s += key + "=" + value + ";";
stringBuilder.append(key + "=" + value + ";");
}
return s + "]";
stringBuilder.append("]");
return stringBuilder.toString();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public IntegerMapGenotype(List<K> list, Bounds<Integer> bounds) {
super(bounds);
Set<K> uniqueKeys = new HashSet<K>(list);
if (uniqueKeys.size() < list.size()) {
throw new IllegalArgumentException("The provided key objects have to be unique");
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_NON_UNIQUE_KEYS);
}
this.list = list;
}
Expand All @@ -89,6 +89,10 @@ public IntegerMapGenotype(List<K> list, Bounds<Integer> bounds) {
*/
public IntegerMapGenotype(List<K> list, int lowerBound, int upperBound) {
super(lowerBound, upperBound);
Set<K> uniqueKeys = new HashSet<K>(list);
if (uniqueKeys.size() < list.size()) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_NON_UNIQUE_KEYS);
}
this.list = list;
}

Expand All @@ -110,7 +114,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 @@ -130,6 +134,9 @@ public boolean containsKey(K key) {
*/
@Override
public Integer getValue(K key) {
if (!containsKey(key)) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY);
}
int i = list.indexOf(key);
return get(i);
}
Expand All @@ -142,16 +149,15 @@ public Integer getValue(K key) {
*/
@Override
public void setValue(K key, Integer value) {
if (!list.contains(key)) {
throw new IllegalArgumentException("Invalid key");
if (!containsKey(key)) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY);
}
int i = list.indexOf(key);
while (size() <= i) {
add(bounds.getLowerBound(size()));
}
if (bounds.getLowerBound(i) > value || bounds.getUpperBound(i) < value) {
throw new IllegalArgumentException(
"The provided value does not lie within the bounds for the provided key");
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_OUT_OF_BOUNDS);
}
set(i, value);
}
Expand Down Expand Up @@ -183,7 +189,7 @@ public String toString() {
stringBuilder.append("[");
for (int i = 0; i < size(); i++) {
K key = list.get(i);
double value = this.get(i);
int value = this.get(i);
stringBuilder.append(key + "=" + value + ";");
}
stringBuilder.append("]");
Expand All @@ -197,8 +203,8 @@ public String toString() {
*/
@Override
public int getIndexOf(K key) {
if (!list.contains(key)) {
throw new IllegalArgumentException("Invalid key");
if (!containsKey(key)) {
throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY);
}
return list.indexOf(key);
}
Expand Down
15 changes: 11 additions & 4 deletions opt4j-core/src/main/java/org/opt4j/core/genotype/MapGenotype.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/


package org.opt4j.core.genotype;

Expand All @@ -39,6 +38,11 @@
*/
public interface MapGenotype<K, V> {

static final String ERROR_MESSAGE_NON_UNIQUE_KEYS = "The provided key objects have to be unique";
static final String ERROR_MESSAGE_INVALID_KEY = "Invalid key";
static final String ERROR_MESSAGE_OUT_OF_BOUNDS = "The provided value does not lie within the bounds for the provided key";
static final String ERROR_MESSAGE_UNSUPPORTED_INIT = "Use method init(Random) instead";

/**
* Return all keys.
*
Expand All @@ -47,7 +51,8 @@ public interface MapGenotype<K, V> {
public Collection<K> getKeys();

/**
* Returns the value for the specified key.
* Returns the value for the specified key. Throws an exception if the key
* is not contained.
*
* @see #setValue
* @param key
Expand All @@ -57,7 +62,8 @@ public interface MapGenotype<K, V> {
public V getValue(K key);

/**
* Sets the value for the specified key.
* Sets the value for the specified key. Throws an exception if the key is
* not contained.
*
* @see #getValue
* @param key
Expand All @@ -77,7 +83,8 @@ public interface MapGenotype<K, V> {
public boolean containsKey(K key);

/**
* Returns the index of the key.
* Returns the index of the key. Throws an exception if the key is not
* contained.
*
* @param key
* the key
Expand Down
Loading

0 comments on commit 3e934d2

Please sign in to comment.