Skip to content

Commit

Permalink
Merge e007254 into 28b44c3
Browse files Browse the repository at this point in the history
  • Loading branch information
felixreimann committed Jun 12, 2018
2 parents 28b44c3 + e007254 commit 9ee484f
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 71 deletions.
71 changes: 30 additions & 41 deletions opt4j-core/src/main/java/org/opt4j/core/Objectives.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
/*******************************************************************************
* Copyright (c) 2014 Opt4J
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/


package org.opt4j.core;

Expand All @@ -34,8 +28,7 @@
import org.opt4j.core.Objective.Sign;

/**
* The {@link Objectives} contains the {@link Objective}-{@link Value}s pairs of
* an {@link Individual}.
* The {@link Objectives} contains the {@link Objective}-{@link Value}s pairs of an {@link Individual}.
*
* @see Value
* @see Objective
Expand All @@ -59,8 +52,8 @@ public Iterator<Entry<Objective, Value<?>>> iterator() {
}

/**
* Returns an array of all values which all have to be minimized. Do not
* call this method before all objectives were added!
* Returns an array of all values which all have to be minimized. Do not call this method before all objectives were
* added!
*
* @see Value#getDouble()
* @return an array containing values which have to be minimized
Expand Down Expand Up @@ -127,8 +120,7 @@ public Collection<Value<?>> getValues() {
}

/**
* Returns the value that is assigned to the given objective. Returns
* {@code null} if the objective does not exist.
* Returns the value that is assigned to the given objective. Returns {@code null} if the objective does not exist.
*
* @param objective
* the given objective
Expand All @@ -139,8 +131,7 @@ public Value<?> get(Objective objective) {
}

/**
* Returns the objective that is assigned to the given value. Returns
* {@code null} if the value does not exist.
* Returns the objective that is assigned to the given value. Returns {@code null} if the value does not exist.
*
* @param value
* the given value
Expand Down Expand Up @@ -201,7 +192,7 @@ public void add(String name, Sign sign, Value<?> value) {
public void add(Objective objective, double value) {
add(objective, new DoubleValue(value));
}

/**
* Adds the objective with the specified value.
*
Expand All @@ -227,7 +218,7 @@ public void add(String name, Sign sign, double value) {
public void add(Objective objective, int value) {
add(objective, new IntegerValue(value));
}

/**
* Adds the objective with the specified value.
*
Expand All @@ -243,8 +234,7 @@ public void add(String name, Sign sign, int value) {
}

/**
* Adds all objective with the specified value specified in
* {@link Objectives}.
* Adds all objective with the specified value specified in {@link Objectives}.
*
* @param objectives
* the objectives
Expand All @@ -255,13 +245,12 @@ public void addAll(Objectives objectives) {
}

/**
* Returns {@code true} if this objectives weakly dominates the specified
* objectives. This comparison is based on the {@link #array()} values.
* Returns {@code true} if this objectives weakly dominates the specified objectives. This comparison is based on
* the {@link #array()} values.
*
* @param opponent
* other objectives
* @return {@code true} if this objectives weakly dominate the
* {@code opponent}
* @return {@code true} if this objectives weakly dominate the {@code opponent}
*/
public boolean weaklyDominates(Objectives opponent) {
double[] va = this.array();
Expand All @@ -275,8 +264,8 @@ public boolean weaklyDominates(Objectives opponent) {
}

/**
* Returns {@code true} if this objectives dominate the specified
* objectives. This comparison is based on the {@link #array()} values.
* Returns {@code true} if this objectives dominate the specified objectives. This comparison is based on the
* {@link #array()} values.
*
* @param opponent
* other objectives
Expand All @@ -298,8 +287,8 @@ public boolean dominates(Objectives opponent) {
}

/**
* Returns {@code true} if this objectives are equal to the specified
* objectives. This comparison is based on the {@link #array()} values.
* Returns {@code true} if this objectives are equal to the specified objectives. This comparison is based on the
* {@link #array()} values.
*
* @param opponent
* other objectives
Expand All @@ -318,8 +307,8 @@ public boolean isEqual(Objectives opponent) {
}

/**
* Calculates the euclidean distance of two {@link Objectives}. This
* calculation is based on the {@link #array()} values.
* Calculates the euclidean distance of two {@link Objectives}. This calculation is based on the {@link #array()}
* values.
*
* @param other
* the second objectives
Expand All @@ -344,13 +333,13 @@ public double distance(Objectives other) {
*/
@Override
public String toString() {
String s = "";
StringBuilder s = new StringBuilder("");
for (Entry<Objective, Value<?>> entry : map.entrySet()) {
Objective o = entry.getKey();
Value<?> v = entry.getValue();
s += o + "=" + v + " ";
s.append(o).append("=").append(v).append(" ");
}
return s;
return s.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
/*******************************************************************************
* Copyright (c) 2014 Opt4J
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/


package org.opt4j.core.genotype;

Expand All @@ -34,14 +28,12 @@

/**
* <p>
* The {@link CompositeGenotype} is a base class for {@link Genotype} classes
* that consist of multiple {@link Genotype}s. The method
* {@link CompositeGenotype#size()} returns the sum of the sizes of the
* contained {@link Genotype}s.
* The {@link CompositeGenotype} is a base class for {@link Genotype} classes that consist of multiple
* {@link Genotype}s. The method {@link CompositeGenotype#size()} returns the sum of the sizes of the contained
* {@link Genotype}s.
* </p>
* A specific {@link CompositeGenotype} has to add each contained
* {@link Genotype} by calling the method {@link #put(Object, Genotype)} where
* {@link Object} is an arbitrary identifier.
* A specific {@link CompositeGenotype} has to add each contained {@link Genotype} by calling the method
* {@link #put(Object, Genotype)} where {@link Object} is an arbitrary identifier.
*
* <p>
* Example:
Expand Down Expand Up @@ -110,8 +102,7 @@ public int size() {
}

/**
* Returns the {@link Genotype} for the given key with an implicit cast to
* the specific {@link Genotype} type.
* Returns the {@link Genotype} for the given key with an implicit cast to the specific {@link Genotype} type.
*
* @param <G>
* the type of genotype
Expand Down Expand Up @@ -154,8 +145,7 @@ public Set<K> keySet() {
}

/**
* Returns all {@code values} which are the contained {@link Genotype}
* objects.
* Returns all {@code values} which are the contained {@link Genotype} objects.
*
* @return all {@code values}
*/
Expand Down Expand Up @@ -197,12 +187,12 @@ public <G extends Genotype> G newInstance() {
*/
@Override
public String toString() {
String s = "[";
StringBuilder s = new StringBuilder("[");
for (Entry<K, V> entry : this) {
K key = entry.getKey();
V value = entry.getValue();
s += key + "=" + value + ";";
s.append(key).append("=").append(value).append(";");
}
return s + "]";
return s.append("]").toString();
}
}
80 changes: 80 additions & 0 deletions opt4j-core/src/test/java/org/opt4j/core/ObjectivesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,80 @@

import org.junit.Assert;
import org.junit.Test;
import org.opt4j.core.Objective.Sign;

public class ObjectivesTest {
@Test
public void getValuesTest() {
Objectives objectives0 = new Objectives();
objectives0.add(new Objective("a"), 0);
objectives0.add(new Objective("b"), 1);
Assert.assertEquals(2, objectives0.getValues().size());
Assert.assertTrue(objectives0.getValues().contains(new IntegerValue(0)));
Assert.assertTrue(objectives0.getValues().contains(new IntegerValue(1)));
}

@Test
public void getTest() {
Objectives objectives0 = new Objectives();
Objective obj = new Objective("a");
objectives0.add(obj, 0);
objectives0.add(new Objective("b"), 1);
Assert.assertEquals(obj, objectives0.get(new IntegerValue(0)));
Assert.assertNull(objectives0.get(new IntegerValue(3)));
}

@Test
public void addNamedTest() {
Objectives objectives0 = new Objectives();
Objective obj = new Objective("a");
objectives0.add("a", Sign.MIN, 2);
Assert.assertEquals(obj, objectives0.iterator().next().getKey());
Assert.assertEquals(new IntegerValue(2), objectives0.iterator().next().getValue());
}

@Test
public void addValueTest() {
Objectives objectives0 = new Objectives();
Objective obj = new Objective("a");
objectives0.add("a", Sign.MIN, new DoubleValue(2.0));
Assert.assertEquals(obj, objectives0.iterator().next().getKey());
Assert.assertEquals(new DoubleValue(2.0), objectives0.iterator().next().getValue());
}

@Test
public void addDoubleTest() {
Objectives objectives0 = new Objectives();
Objective obj = new Objective("a");
objectives0.add("a", Sign.MIN, 2.0);
Assert.assertEquals(obj, objectives0.iterator().next().getKey());
Assert.assertEquals(new DoubleValue(2.0), objectives0.iterator().next().getValue());
}

@Test
public void addIntTest() {
Objectives objectives0 = new Objectives();
Objective obj = new Objective("a");
objectives0.add("a", Sign.MIN, 3);
Assert.assertEquals(obj, objectives0.iterator().next().getKey());
Assert.assertEquals(new IntegerValue(3), objectives0.iterator().next().getValue());
}

@Test
public void isEqualTest() {
Objectives objectives0 = new Objectives();
objectives0.add("a", Sign.MIN, 3);

Objectives objectives1 = new Objectives();
objectives1.add("a", Sign.MIN, 3);

Objectives objectives2 = new Objectives();
objectives2.add("a", Sign.MIN, 4);

Assert.assertTrue(objectives0.isEqual(objectives1));
Assert.assertFalse(objectives0.isEqual(objectives2));
}

@Test
public void sizeTest() {
Objectives objectives = new Objectives();
Expand All @@ -26,4 +98,12 @@ public void distanceTest() {
Assert.assertEquals(objectives0.distance(objectives1), objectives1.distance(objectives0), 0.0001);
Assert.assertEquals(0.0, objectives0.distance(objectives0), 0.0001);
}

@Test
public void toStringTest() {
Objectives objectives0 = new Objectives();
objectives0.add("a", Sign.MIN, 3);

Assert.assertEquals("a(MIN)=3 ", objectives0.toString());
}
}

0 comments on commit 9ee484f

Please sign in to comment.