/* * Copyright 2002, 2003, 2004, 2005 - Koalog */ package com.koalog.jcs.examples; import java.util.Map; import java.util.HashMap; import java.util.Arrays; import java.util.List; import java.util.ArrayList; import org.apache.log4j.PropertyConfigurator; import com.koalog.jcs.domain.SparseDomain; import com.koalog.jcs.variable.IntegerVariable; import com.koalog.jcs.constraint.BaseProblem; import com.koalog.jcs.constraint.arithmetic.Element; import com.koalog.jcs.constraint.arithmetic.Cycle; import com.koalog.jcs.constraint.arithmetic.Permutation; import com.koalog.jcs.constraint.arithmetic.Sum; /** * This is the Travelling Salesman Problem. * It consists in finding an hamiltonian path of minimal weight * in a weighted graph. * * @author Yan Georget */ public class TSPProblem extends BaseProblem { //------------------------------------------------------------------------ // PROPERTIES //------------------------------------------------------------------------ /** The weight of the cycle. */ IntegerVariable sum; /** A map mapping a variable to an array of costs (weights). */ Map costs; //------------------------------------------------------------------------ // CONSTRUCTORS //------------------------------------------------------------------------ /** * Sole constructor. * @param graph the graph * @param lowerBound a lower bound of the weight of the cycle * @param upperBound an upper bound of the weight of the cycle */ public TSPProblem(int[][] graph, int lowerBound, int upperBound) { super(); // variables & constraints definitions sum = new IntegerVariable("sum", lowerBound, upperBound); costs = new HashMap(graph.length); // next[i] is the successor of node i IntegerVariable next[] = new IntegerVariable[graph.length]; // prev[j] is the predecessor of node j IntegerVariable prev[] = new IntegerVariable[graph.length]; // nweight[i] is the weight of edge i -> next[i] IntegerVariable nweight[] = new IntegerVariable[graph.length]; // pweight[j] is the weight of edge prev[j] -> j IntegerVariable pweight[] = new IntegerVariable[graph.length]; for (int i=0; i max) { max = a[j]; } } return max; } private static int getMin(int[] a) { int min = Integer.MAX_VALUE; for (int j=0; j