GetInput.java 5.04 KB
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solver;
import org.chocosolver.solver.constraints.Constraint;
import org.chocosolver.solver.variables.IntVar;

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

public class GetInput {
    public static void giveDomains (Model model, IntVar[] ugs, String dir, IntVar[] woodYield, UG[] nodes){
        int index = 0;
        try {
            File allUg = new File(dir + "/ugs_init.txt");
            Scanner readerUg = new Scanner(allUg);

            File myObjWood = new File(dir + "/wood_total_init.txt");
            Scanner myReaderWood = new Scanner(myObjWood);

            while (readerUg.hasNextLine()) {
                String dataUg = readerUg.nextLine();

                String dataWood = myReaderWood.nextLine();

                if(nodes[index].valid) {
                    String[] str_split = dataUg.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++;
            }

            readerUg.close();
            myReaderWood.close();
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        //TODO: Escrever no output a madeira obtida POR ANO
        //Output da SOILLOSS
        //Output do RISCO

        int areaLimit = Integer.parseInt(args[0]);
        String fileDirectory = args[1];
        int ugLimit = Integer.parseInt(args[2]);

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

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

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

        UG[] nodes = new UG[nUgs];
        UG.fillArray(nodes, fileDirectory, ugLimit);

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

        IntVar[] woodYield = new IntVar[nUgs];

        IntVar[] soillossTotal = new IntVar[nUgs];

        IntVar[] perc_r0_Total = new IntVar[nUgs];

        IntVar[] perc_r5_Total = new IntVar[nUgs];

        IntVar[] perc_r10_Total = new IntVar[nUgs];



        giveDomains(m, ugs, fileDirectory, woodYield, nodes); // reads the ugs_init file and initializes each variable with its possible prescription values as domain


        System.out.println("running");


        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 CustomPropagator2(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_total, prescIndex).post();
            }
            else{
                //System.out.println("UG_"+i + " "+woodYield[i]);
                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 outputPairs = new FileWriter("outputPairs.csv");
            FileWriter woodByYear = new FileWriter("woodByYear.csv");

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

            }
            outputPairs.close();
            System.out.println(woodSum);


        }

    }
}