001/******************************************************************************* 002 * Copyright (c) 2014 Opt4J 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 005 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 006 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 007 * permit persons to whom the Software is furnished to do so, subject to the following conditions: 008 * 009 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 010 * Software. 011 * 012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 013 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 014 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 015 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 016 *******************************************************************************/ 017 018package org.opt4j.benchmarks.knapsack; 019 020import static org.opt4j.core.config.annotations.Citation.PublicationMonth.NOVEMBER; 021 022import org.opt4j.core.config.annotations.Citation; 023import org.opt4j.core.config.annotations.File; 024import org.opt4j.core.config.annotations.Info; 025import org.opt4j.core.config.annotations.Required; 026import org.opt4j.core.problem.ProblemModule; 027import org.opt4j.core.start.Constant; 028 029/** 030 * The multiobjective 0/1 ILP knapsack problem as proposed in Zitzler and Thiele 031 * 1999. Either one of the nine benchmark problems from Zitzler and Thiele 1999 032 * can be selected or the number of knapsacks and items can be set manually. 033 * 034 * @see <a href= 035 * "http://www.tik.ee.ethz.ch/sop/download/supplementary/testProblemSuite/">http://www.tik.ee.ethz.ch/sop/download/supplementary/testProblemSuite/</a> 036 * 037 * @author reimann, lukasiewycz 038 * 039 */ 040@Citation(authors = "Eckart Zitzler and Lothar Thiele", title = "Multiobjective evolutionary algorithms: A comparative case study and the strength Pareto approach", journal = "IEEE Transactions on Evolutionary Computation", volume = 3, number = 4, pageFirst = 257, pageLast = 271, month = NOVEMBER, year = 1999) 041public class KnapsackModule extends ProblemModule { 042 043 protected int knapsacks = 5; 044 045 protected int items = 250; 046 047 @Required(property = "testCase", elements = { "MANUAL", "ZT1", "ZT2", "ZT3", "ZT4", "ZT5", "ZT6", "ZT7", "ZT8", 048 "ZT9" }) 049 @Constant(value = "seed", namespace = KnapsackProblemRandom.class) 050 protected int seed = 0; 051 052 @File 053 @Required(property = "testCase", elements = { "FILE" }) 054 @Constant(value = "filename", namespace = KnapsackProblemFile.class) 055 protected String filename = ""; 056 057 protected Representation representation = Representation.BITSTRING; 058 059 protected TestCase testCase = TestCase.MANUAL; 060 061 public enum TestCase { 062 FILE, @Info("use test data from an input file") 063 MANUAL, @Info("2 knapsacks, 250 items") 064 ZT1, @Info("3 knapsacks, 250 items") 065 ZT2, @Info("5 knapsacks, 250 items") 066 ZT3, @Info("2 knapsacks, 500 items") 067 ZT4, @Info("3 knapsacks, 500 items") 068 ZT5, @Info("5 knapsacks, 500 items") 069 ZT6, @Info("2 knapsacks, 750 items") 070 ZT7, @Info("3 knapsacks, 750 items") 071 ZT8, @Info("5 knapsacks, 750 items") 072 ZT9; 073 } 074 075 public enum Representation { 076 @Info("Bitstring genotype") 077 BITSTRING, @Info("SAT decoding") 078 SAT; 079 } 080 081 @Override 082 protected void config() { 083 switch (testCase) { 084 case ZT1: 085 knapsacks = 2; 086 items = 250; 087 break; 088 case ZT2: 089 knapsacks = 3; 090 items = 250; 091 break; 092 case ZT3: 093 knapsacks = 5; 094 items = 250; 095 break; 096 case ZT4: 097 knapsacks = 2; 098 items = 500; 099 break; 100 case ZT5: 101 knapsacks = 3; 102 items = 500; 103 break; 104 case ZT6: 105 knapsacks = 5; 106 items = 500; 107 break; 108 case ZT7: 109 knapsacks = 2; 110 items = 750; 111 break; 112 case ZT8: 113 knapsacks = 3; 114 items = 750; 115 break; 116 case ZT9: 117 knapsacks = 5; 118 items = 750; 119 break; 120 default: 121 break; 122 } 123 124 bindConstant("items", KnapsackProblemRandom.class).to(items); 125 bindConstant("knapsacks", KnapsackProblemRandom.class).to(knapsacks); 126 127 if (testCase == TestCase.FILE) { 128 bind(KnapsackProblem.class).to(KnapsackProblemFile.class).in(SINGLETON); 129 } else { 130 bind(KnapsackProblem.class).to(KnapsackProblemRandom.class).in(SINGLETON); 131 } 132 133 switch (representation) { 134 default: // BITSTRING 135 bindProblem(KnapsackBinaryCreatorDecoder.class, KnapsackBinaryCreatorDecoder.class, 136 KnapsackProfitEvaluator.class); 137 break; 138 case SAT: 139 bindProblem(KnapsackSATCreatorDecoder.class, KnapsackSATCreatorDecoder.class, 140 KnapsackProfitEvaluator.class); 141 break; 142 } 143 addEvaluator(KnapsackOverloadEvaluator.class); 144 } 145 146 @Required(property = "testCase", elements = { "MANUAL" }) 147 public int getKnapsacks() { 148 return knapsacks; 149 } 150 151 public void setKnapsacks(int knapsacks) { 152 this.knapsacks = knapsacks; 153 } 154 155 @Required(property = "testCase", elements = { "MANUAL" }) 156 public int getItems() { 157 return items; 158 } 159 160 public void setItems(int items) { 161 this.items = items; 162 } 163 164 public Representation getRepresentation() { 165 return representation; 166 } 167 168 public void setRepresentation(Representation representation) { 169 this.representation = representation; 170 } 171 172 public TestCase getTestCase() { 173 return testCase; 174 } 175 176 public void setTestCase(TestCase testCase) { 177 this.testCase = testCase; 178 } 179 180 public int getSeed() { 181 return seed; 182 } 183 184 public void setSeed(int seed) { 185 this.seed = seed; 186 } 187 188 public String getFilename() { 189 return filename; 190 } 191 192 public void setFilename(String filename) { 193 this.filename = filename; 194 } 195 196}