// // Processor allocation problem. // we are given a number of tasks to perform, and for each task a set of task variants // that can perform that task. Each task variant puts a specific load on a processor and pairs of tasks // might need to communicate and generate load on interprocessor connections. // We have a number of processors, each with a specified capacity. Also, links between processors // also have a bandwidth capacity. Problem is then to place task variants onto processors, // respecting processor and link bandwidths // // import java.util.*; import java.io.*; import java.io.File; import java.io.IOException; import java.util.Scanner; import org.chocosolver.solver.*; import org.chocosolver.solver.variables.*; import org.chocosolver.solver.constraints.*; import org.chocosolver.solver.search.strategy.*; import org.chocosolver.solver.search.loop.monitors.*; public class AllocationProblem { int nTasks; // number of tasks int nVariants; // number of task variants int nProcessors; // number of processors ArrayList[] task; // task[i] is an array list of task variants int[] utilisations; // utilisation (load) of each task variant int[] capacities; // capacities of each processor, including processor 0 int[][] links; // the communication bandwidth (capacity) between pairs of processors int[][] linkId; // an integer to identify a link, later used as a value of and index for a constrained integer variable int[][] bandwidths; // the bandwidth between pairs of tasks ArrayList[] permittedProcessors; // for each task variant, the processors it may be placed on ... including zero int totalUtilisations; // sum of all utilisations of task variants int totalBandwidth; // sum of bandwidth load between all pairs of tasks int[][] interVariantLoad; // load between variants int totalCommunicationsLoad; // to be allowed on link zero Stack sameProcessor; // Solver solver; // the solver IntVar[] IVtaskVariant; // task variants, domains are permitted processors and zero! IntVar[] IVprocessor; // the load placed on a processor, where processor[0] has capacity totalUtilisation IntVar[][] IVtaskPair; // a pair of task variants assigned (possibly) to a pair of processors, i.e. a link IntVar[][] IVlink; // IVlink[i][j] is a bin and is the load on a link between a pair of processors i and j public AllocationProblem(String fname) throws IOException { BufferedReader fin = new BufferedReader(new FileReader(fname)); StringTokenizer st = null; String b = ""; b = fin.readLine(); st = new StringTokenizer(b," "); System.out.println(st.nextToken()); nTasks = Integer.parseInt(st.nextToken()); b = fin.readLine(); st = new StringTokenizer(b," "); System.out.println(st.nextToken()); nVariants = Integer.parseInt(st.nextToken()); b = fin.readLine(); st = new StringTokenizer(b," "); System.out.println(st.nextToken()); nProcessors = Integer.parseInt(st.nextToken()); // task to variants System.out.println(fin.readLine()); task = new ArrayList[nTasks]; for (int i=0;i(); System.out.println(fin.readLine()); b = fin.readLine(); while (b != null){ st = new StringTokenizer(b," ,"); while (st.hasMoreTokens()) sameProcessor.push(Integer.parseInt(st.nextToken())); b = fin.readLine(); } System.out.println(sameProcessor); fin.close(); interVariantLoad = new int[nVariants][nVariants]; for (int i=0;i)task[i]) for (int j=i+1;j)task[j]){ interVariantLoad[variant_i-1][variant_j-1] = interVariantLoad[variant_j-1][variant_i-1] = bandwidths[i][j]; totalCommunicationsLoad = totalCommunicationsLoad + bandwidths[i][j]; } System.out.println("inter-variant load"); for (int i=0;i)task[i]){ int n = permittedProcessors[variant-1].size(); int[] domain = new int[n]; for (int j=0;j)task[i]) for (int j=i+1;j)task[j]){ IVtaskPair[variant_i-1][variant_j-1] = VF.bounded("taskPair_"+ variant_i +","+ variant_j,0,numberOfLinks,solver); for (int processor_i : (ArrayList)permittedProcessors[variant_i-1]) for (int processor_j : (ArrayList)permittedProcessors[variant_j-1]){ //System.out.println("("+ variant_i +"/"+ processor_i +","+ // variant_j +"/"+ processor_j +") = linkId: "+ // linkId[processor_i][processor_j]); Constraint c1 = ICF.arithm(IVtaskVariant[variant_i-1],"=",processor_i); Constraint c2 = ICF.arithm(IVtaskVariant[variant_j-1],"=",processor_j); Constraint c3 = ICF.arithm(IVtaskPair[variant_i-1][variant_j-1],"=",linkId[processor_i][processor_j]); LCF.ifThen(LCF.and(new Constraint[]{c1,c2}),c3); } } // // // IVlink = new IntVar[nProcessors+1][nProcessors+1]; // bandwidth between pairs of processors for (int i=1;i)task[i]) for (int j=i+1;j)task[j]) if (IVtaskPair[variant_i-1][variant_j-1] != null){ //System.out.println("("+ i +"-"+ variant_i +","+ j +"-"+ variant_j +")"); IVflatTaskPair[k] = IVtaskPair[variant_i-1][variant_j-1]; flatLoad[k] = interVariantLoad[variant_i-1][variant_j-1]; //bandwidths[i][j]; k++; } //for (int i=0;i)task[task_i-1]) for (int j : (ArrayList)task[task_j-1]){ Constraint c1 = ICF.arithm(IVtaskVariant[i-1],"!=",0); Constraint c2 = ICF.arithm(IVtaskVariant[j-1],"!=",0); Constraint c3 = ICF.arithm(IVtaskVariant[i-1],"=",IVtaskVariant[j-1]); LCF.ifThen(LCF.and(new Constraint[]{c1,c2}),c3); //System.out.println(c1 +" and "+ c2 +" -> "+ c3); } } } public static void main(String[] args) throws IOException { AllocationProblem p = new AllocationProblem(args[0]); p.build(); p.solver.findSolution(); // // NOTE: variable ordering over decision variables wich will be IVtaskVariant // also deliver run time statistics // for (IntVar variant : p.IVtaskVariant) System.out.println(variant); } }