Exemplo n.º 1
0
        /// <summary>
        /// Initializza la DLL. Utilizzato la prima volta dal costruttore.
        /// e richiamato le volte successive per gestione del wrapper e memoria unsafe
        /// </summary>
        /// <param name="_problem_name"></param>
        /// <param name="_CPLEX"></param>
        private void InitDLL(string _problem_name = "PMedProb", bool _CPLEX = false)
        {
            if (loaded)
            {
                wrapper.unloadProblem(problem);
                loaded = false;
            }
            if (inited)
            {
                wrapper.closeSolver(_CPLEX);
                inited = false;
            }
            wrapper    = new Wrapper();
            this.CPLEX = _CPLEX;
            loaded     = false;
            inited     = false;

            //usato perchè se si utilizza la dll in "directory corrente"
            //l'openfiledilalog dell' os, cambia la "directory corrente"
            //a volte.
            //si può anche rimuovere (forse)
            string dir = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(CurDir);

            //commentato perchè modificato il wrapper.
            //da testare se problemi con dll scommentare.
            //if ((!CPLEX) && (File.Exists(CurDir+"\\CoinMP.dll")))
            //    wrapper.initSolver(false);
            //else if ((CPLEX) && (File.Exists("CPLEX101.dll")))
            //else if (CPLEX)
            //wrapper.initSolver(true);
            // else
            //     throw new DllNotFoundException();
            wrapper.initSolver(_CPLEX);

            problem = wrapper.createProblem(problem_name = _problem_name, _CPLEX);
            inited  = true;

            //ripristino la dir corrente.
            Directory.SetCurrentDirectory(dir);
        }
Exemplo n.º 2
0
        private static void TestCOinMPWrapper()
        {
            //test risoluzione :
            //MAX z=x1+x2+x3+x4
            //s.t
            //x1 + x2 <=2
            //x2 + x3 <=3
            //x3 + x4 <=2

            //La matrice completa dovrebbe essere:
            //      x1	x2	x3	x4 | rhs
            //(1)	1	1	0	0  | 2
            //(2)	0	1	1	0  | 3
            //(3)	0	0	1	1  | 2

            Wrapper w = new Wrapper();
            //CCS ccs = new CCS();

            int objectSense = -1;

            int nzc    = 6;
            int colnum = 4;
            int rownum = 3;

            //i coefficenti sono il peso delle xi, quindi essendo che ogni variabile a coefficente 1 ...
            double[] objectCoeffs = new double[4] {
                1, 1, 1, 1
            };
            String rowType = "LLL";

            double[] rhsValues = new double[3] {
                2, 3, 2
            };

            int [] matrixBegin = new int[4 + 1] {
                0, 1, 3, 5, 6
            };
            int [] matrixCount = new int[4] {
                1, 2, 2, 1
            };
            int [] matrixindex = new int[6] {
                0, 0, 1, 1, 2, 2
            };
            double[] matrixval = new double[6] {
                1, 1, 1, 1, 1, 1
            };

            w.initSolver(false);

            WrapProblem prob = w.createProblem("prob", false);

            w.loadProblem(prob, colnum, rownum, nzc, 0, objectSense, 0, objectCoeffs, null, null, rowType, rhsValues, null, matrixBegin, matrixCount, matrixindex, matrixval, null, null, "");

            w.optimizeLpProblem(prob);

            double obj = w.getObjectValue(prob);

            double[] solvars = new double[colnum]; //Vettore deputato a contenere i valori della soluzione
            w.getSolutionValues(prob, solvars, null, null, null);
            for (int i = 0; i < solvars.Length; i++)
            {
                Console.WriteLine("{0} solvar {1}", solvars[i], i);
            }
            Console.WriteLine("VALORE OTTIMO TROVATO :  {0} ", obj);
        }
Exemplo n.º 3
0
        private static void TestCOinMPWrapper_DOC()
        {
            Wrapper w = new Wrapper();

            String problemName  = "CoinTest";
            int    colCount     = 8;
            int    rowCount     = 5;
            int    nonZeroCount = 14;
            String objectName   = "obj";
            int    objectSense  = -1;

            double[] objectCoeffs = new double[8] {
                1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
            };
            double[] lowerBounds = new double[8] {
                0, 0, 0, 0, 0, 0, 0, 0
            };
            double[] upperBounds = new double[8] {
                1000000.0, 1000000.0, 1000000.0, 1000000.0, 1000000.0, 1000000.0, 1000000.0, 1000000.0
            };
            String rowType = "LLLLL";

            double[] rhsValues = new double[5] {
                14.0, 80.0, 50.0, 50.0, 50.0
            };
            int[] matrixBegin = new int[8 + 1] {
                0, 2, 4, 6, 8, 10, 11, 12, 14
            };
            int[] matrixCount = new int[8] {
                2, 2, 2, 2, 2, 1, 1, 2
            };
            int[] matrixIndex = new int[14] {
                0, 4, 0, 1, 1, 2, 0, 3, 0, 4, 2, 3, 0, 4
            };
            double[] matrixValues = new double[14] {
                3.0, 5.6, 1.0, 2.0, 1.1, 1.0, -2.0, 2.8, -1.0, 1.0, 1.0, -1.2, -1.0, 1.9
            };
            String[] colNames = new String[8] {
                "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8"
            };
            String[] rowNames = new String[5] {
                "r1", "r2", "r3", "r4", "r5"
            };


            w.initSolver(false);


            WrapProblem prob = w.createProblem(problemName, false);

            w.loadProblem(prob, colCount, rowCount, nonZeroCount, 0, objectSense, 0, objectCoeffs, lowerBounds, upperBounds, rowType, rhsValues, null, matrixBegin, matrixCount, matrixIndex, matrixValues, colNames, rowNames, objectName);

            Console.WriteLine("OPTIMIZZA!!!");
            w.optimizeLpProblem(prob);
            Console.WriteLine("FINE OTTIMIZZA!!!");
            double obj = w.getObjectValue(prob);

            double[] solvars = new double[colCount]; //Vettore deputato a contenere i valori della soluzione
            w.getSolutionValues(prob, solvars, null, null, null);
            for (int i = 0; i < solvars.Length; i++)
            {
                Console.WriteLine("{0} solvar {1}", solvars[i], i);
            }
            Console.WriteLine("VALORE OTTIMO TROVATO :  {0} ", obj);
        }
Exemplo n.º 4
0
        private static void TestPMedWrapper3()
        {
            //Problema da testare :
            //ora proviamo con costo cluster...

            //Sol0 = {4,2} ; {1,0,3}
            //Sol1 = {3,1} ; {4,2,0}

            //C(Sol0) = {0,16}=16 ; {0,15,40}=55  = 71
            //C(Sol1) = {0,40}=40 ; {0,16,24}=40  = 80

            //min z = 16*S0c0 + 55*S0c1+ 40*s1c0 + 40*s1c1
            //s.t.

            //    s0c0	s0c1	s1c0	s1c1
            //z	 16		55		40		40

            //0	 0		1		0		1
            //1	 0		1		1		0
            //2	 1		0		0		1
            //3	 0		1		1		0
            //4	 1		0		0		1


            Wrapper w = new Wrapper();
            //da fare in soluzione come calcolerebbe l'algoritmo e poi testare la CCS...
            //dopo che si è riuscito a farlo a "mano"...
            //CCS ccs = new CCS();

            int objectSense = 1;

            int nzc    = 10;
            int colnum = 4;
            int rownum = 5;

            //i coefficenti sono il peso delle xi, quindi essendo che ogni variabile a coefficente 1 ...
            double[] objectCoeffs = new double[4] {
                16, 55, 40, 40,
            };
            String rowType = "GGGGG";

            double[] rhsValues = new double[5] {
                1, 1, 1, 1, 1
            };

            int[] matrixBegin = new int[4 + 1] {
                0, 2, 5, 7, 10
            };
            int[] matrixCount = new int[4] {
                2, 3, 2, 3
            };
            int[] matrixindex = new int[10] {
                2, 4, 0, 1, 3, 1, 3, 0, 2, 4
            };                                                                 // notare che sono i clusters ordinati cresenti
            double[] matrixval = new double[10] {
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1
            };

            w.initSolver(false);

            WrapProblem prob = w.createProblem("prob", false);

            w.loadProblem(prob, colnum, rownum, nzc, 0, objectSense, 0, objectCoeffs, null, null, rowType, rhsValues, null, matrixBegin, matrixCount, matrixindex, matrixval, null, null, "");

            w.optimizeLpProblem(prob);

            double obj = w.getObjectValue(prob);

            double[] solvars = new double[4]; //Vettore deputato a contenere i valori della soluzione
            w.getSolutionValues(prob, solvars, null, null, null);
            for (int i = 0; i < solvars.Length; i++)
            {
                Console.WriteLine("{0} solvar {1}", solvars[i], i);
            }
            Console.WriteLine("VALORE OTTIMO TROVATO :  {0} ", obj);
        }