Main.java 8.9 KB
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solution;
import org.chocosolver.solver.Solver;
import org.chocosolver.solver.constraints.Constraint;
import org.chocosolver.solver.objective.ParetoOptimizer;
import org.chocosolver.solver.search.strategy.Search;
import org.chocosolver.solver.variables.IntVar;

import java.io.*;
import java.util.List;
import java.util.Scanner;

import static org.chocosolver.solver.search.strategy.Search.activityBasedSearch;
import static org.chocosolver.solver.search.strategy.Search.intVarSearch;
/*
public class Main {
    public static void giveDomains (Model model, IntVar[] ugs, String dir, UG[] nodes,
                                    float X, float Y, int maxDistance, IntVar[] woodYield){
        int index = 0;
        try {
            File myObj = new File(dir + "/ugs_init");
            Scanner myReader = new Scanner(myObj);

            File myObjWood = new File(dir + "/wood_init");
            Scanner myReaderWood = new Scanner(myObjWood);

            while (myReader.hasNextLine()) {

                double d = Math.sqrt(Math.pow (nodes[index].mediumX - X, 2) + Math.pow (nodes[index].mediumY - Y, 2));
                String data = myReader.nextLine();

                String dataWood = myReaderWood.nextLine();

                if(d <= maxDistance) {

                    String[] str_split = data.split(",", 0);

                    String[] str_split_wood = dataWood.split(",", 0);

                    int size = str_split.length;
                    int[] toInsert = new int[size];
                    int[] toInsertWood = new int[size];
                    for (int i = 0; i < size; i++) {
                        toInsert[i] = Integer.parseInt(str_split[i]);
                        toInsertWood[i] = (int) Float.parseFloat(str_split_wood[i]);
                    }

                    ugs[index] = model.intVar("UG_"+(index), toInsert);

                    woodYield[index] = model.intVar("Wood_Yield_"+(index), toInsertWood);

                }
                else {
                    ugs[index] = model.intVar("UG_"+(index), 0);
                    woodYield[index] = model.intVar("Wood_Yield_"+(index), 0);
                }
                index++;
            }
            myReader.close();
            myReaderWood.close();
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }

    public static void giveDomainsWithRegion (Model model, IntVar[] ugs, String dir, UG[] nodes,
                                     IntVar[] woodYield, String region){
        int index = 0;
        try {
            File myObj = new File(dir + "/ugs_init");
            Scanner myReader = new Scanner(myObj);

            File myObjWood = new File(dir + "/wood_init");
            Scanner myReaderWood = new Scanner(myObjWood);

            while (myReader.hasNextLine()) {

                String data = myReader.nextLine();

                String dataWood = myReaderWood.nextLine();

                if(region.equalsIgnoreCase(nodes[index].region)) {

                    String[] str_split = data.split(",", 0);

                    String[] str_split_wood = dataWood.split(",", 0);

                    int size = str_split.length;
                    int[] toInsert = new int[size];
                    int[] toInsertWood = new int[size];
                    for (int i = 0; i < size; i++) {
                        toInsert[i] = Integer.parseInt(str_split[i]);
                        toInsertWood[i] = (int) Float.parseFloat(str_split_wood[i]);
                    }

                    ugs[index] = model.intVar("UG_"+(index), toInsert);

                    woodYield[index] = model.intVar("Wood_Yield_"+(index), toInsertWood);

                }
                else {
                    ugs[index] = model.intVar("UG_"+(index), 0);
                    woodYield[index] = model.intVar("Wood_Yield_"+(index), 0);
                }
                index++;
            }
            myReader.close();
            myReaderWood.close();
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }


    public static void main (String[] args) throws IOException, InterruptedException {

        int areaLimit = Integer.parseInt(args[0]);
        String fileDirectory = args[1];
        int startingPointIndex = 0, maxDistFromPoint = 9999999;
        String region = "";
        float X = 0, Y = 0;

        Model m = new Model("Forest Management");


        BufferedReader reader = new BufferedReader(new FileReader(fileDirectory + "/ugs_init"));

        int nUgs = 0;
        while (reader.readLine() != null) nUgs++;
        reader.close();

        UG[] nodes = new UG[nUgs]; // initializes the nodes array with the number of total ugs in the input files (every line)

        IntVar[] ugs = new IntVar[nUgs]; // same for the constraint variable array

        IntVar[] woodYield = new IntVar[nUgs];




        if(args.length < 5)
        {
            startingPointIndex = Integer.parseInt(args[2]);
            maxDistFromPoint = Integer.parseInt(args[3]);

            BufferedReader xV = new BufferedReader(new FileReader(fileDirectory + "/xvertex_init"));
            Scanner myReaderXVertex = new Scanner(xV);
            BufferedReader yV = new BufferedReader(new FileReader(fileDirectory + "/yvertex_init"));
            Scanner myReaderYVertex = new Scanner(yV);

            int current = 0;

            while (myReaderXVertex.hasNextLine() && myReaderYVertex.hasNextLine()){
                X = Float.parseFloat(myReaderXVertex.nextLine());
                Y = Float.parseFloat(myReaderYVertex.nextLine());
                if(current == startingPointIndex){
                    break;
                }
                current++;
            }
            myReaderXVertex.close();
            myReaderYVertex.close();

           // UG.fillArray(nodes, fileDirectory, X, Y, maxDistFromPoint);
            giveDomains(m, ugs, fileDirectory, nodes, X, Y, maxDistFromPoint, woodYield); // reads the ugs_init file and initializes each variable with its possible prescription values as domain
        }
        else{
            region = args[4];
            UG.fillArray(nodes, fileDirectory, region);
            giveDomainsWithRegion(m, ugs, fileDirectory, nodes, woodYield, region);
        }



        //System.out.println(areaLimit);
        for(int ugIndex = 0; ugIndex < nodes.length; ugIndex++) { //loops through every UG
            //the propagator takes as parameters the index of the UG we are starting out from
            //the nodes with all the info, the constraint variable array and the area limit
            if(nodes[ugIndex].valid) {
                new Constraint("Area Limit Constraint", new CustomPropagator(ugIndex, nodes, ugs, areaLimit, woodYield)).post();
            }
        }


        Solver s = m.getSolver();


        int valids = 0;
        for(int i = 0; i < nodes.length; i++){
            if(nodes[i].valid) {
                //System.out.println(ugs[i]);
                valids++;
            }
        }


        for(int i = 0; i < nodes.length; i++){
            if(nodes[i].valid) {
                IntVar prescIndex = m.intVar(0, 255);

                m.element(ugs[i], nodes[i].presc, prescIndex).post();
                m.element(woodYield[i], nodes[i].wood, prescIndex).post();
            }
            else{
                m.arithm(woodYield[i], "=", 0).post();
            }
        }

        IntVar woodSum = m.intVar("SUM", 0, 99999999);

        m.sum(woodYield,"=", woodSum).post();


        // Single Criterion Optimization

        m.setObjective(Model.MAXIMIZE, woodSum);

        if (s.solve()) {

            FileWriter myWriter = new FileWriter("outputPairs.csv");
            for (int i = 0; i < ugs.length; i++) {
                if (nodes[i].valid) {
                    //System.out.println(woodYield[i]);
                    //myWriter.write(nodes[i].external + "," + prescIdConverter[ugs[i].getValue()] + "\n");
                }

            }
            myWriter.close();
            System.out.println(woodSum);
            System.out.println(region);
        }


        /*Multi Criterion with Pareto
        IntVar testSum = m.intVar("presc nmbers", 0, 10000);
        m.sum(ugs, "=", testSum).post();

        //VER IMAGENS
        //IntVar a = m.intVar("test", 0, 3);
        ParetoOptimizer po = new ParetoOptimizer(Model.MAXIMIZE,new IntVar[]{woodSum,testSum});

        Solver solver = m.getSolver();
        solver.plugMonitor(po);

        // optimization
        while(solver.solve());

        List<Solution> paretoFront = po.getParetoFront();
        System.out.println("The pareto front has " + paretoFront.size() + " solutions : ");
        for (Solution so : paretoFront) {
            System.out.println("a = " + so.getIntVal(woodSum) + " and b = " + so.getIntVal(testSum));
        }

        // retrieve the pareto front


    }
}*/