net.sf.opendse.optimization.SATCreatorDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendse-optimization Show documentation
Show all versions of opendse-optimization Show documentation
The optimization module of OpenDSE
/**
* OpenDSE is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* OpenDSE 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenDSE. If not, see http://www.gnu.org/licenses/.
*/
package net.sf.opendse.optimization;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.opendse.model.Link;
import net.sf.opendse.model.Mapping;
import net.sf.opendse.model.Resource;
import net.sf.opendse.model.Specification;
import net.sf.opendse.optimization.encoding.Interpreter;
import net.sf.opendse.optimization.encoding.variables.CR;
import net.sf.opendse.optimization.encoding.variables.EAVI;
import org.opt4j.core.Genotype;
import org.opt4j.core.common.random.Rand;
import org.opt4j.core.optimizer.Control;
import org.opt4j.satdecoding.AbstractSATDecoder;
import org.opt4j.satdecoding.Constraint;
import org.opt4j.satdecoding.Model;
import org.opt4j.satdecoding.SATManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class SATCreatorDecoder extends AbstractSATDecoder {
static List> order = new ArrayList>();
static Map,Double> lb = new HashMap, Double>();
static Map,Double> ub = new HashMap, Double>();
static {
order.add(Resource.class);
order.add(Link.class);
order.add(EAVI.class);
order.add(Mapping.class);
order.add(CR.class);
setBounds(Resource.class, 0.75, 1.0);
setBounds(Link.class, 0.6, 1.0);
setBounds(EAVI.class, 0.25, 0.5);
setBounds(Mapping.class, 0.25, 0.5);
setBounds(CR.class, 0.00, 0.25);
}
static void setBounds(Class> clazz, double lbv, double ubv){
lb.put(clazz, lbv);
ub.put(clazz, ubv);
}
static int indexOf(Object object) {
for (int i = 0; i < order.size(); i++) {
if (order.get(i).isAssignableFrom(object.getClass())) {
return i;
}
}
return -1;
}
protected final SATConstraints constraints;
protected final SpecificationWrapper specificationWrapper;
protected final Interpreter interpreter;
protected final Control control;
@Inject
public SATCreatorDecoder(SATManager manager, Rand random, SATConstraints constraints,
SpecificationWrapper specificationWrapper, Interpreter interpreter, Control control) {
super(manager, random);
this.constraints = constraints;
this.specificationWrapper = specificationWrapper;
this.interpreter = interpreter;
this.control = control;
}
@Override
public ImplementationWrapper convertModel(Model model) {
if (model == null) {
control.doTerminate();
System.err.println("No feasible implementation exists.");
return new ImplementationWrapper(null);
}
model = constraints.decorate(model);
Specification specification = specificationWrapper.getSpecification();
Specification implementation = interpreter.toImplementation(specification, model);
ImplementationWrapper wrapper = new ImplementationWrapper(implementation);
return wrapper;
}
@Override
public Set createConstraints() {
Set constraints = new HashSet(this.constraints.getConstraints());
return constraints;
}
@Override
public void randomize(Collection