CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2012
    Posts
    21

    "The system cannot find the path specified"

    I have two pieces of code:

    Code:
    #include <cmath>;
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <iomanip>
    #include <armadillo>
    using namespace arma;
    using namespace std;
    
    
    
    
    inline void n1234(int nodes, int ny, int nx, double ly, double lx, Mat<int>& N, int l){
    	for (int i = 0; i<nodes; i++){
    		N(i, 0) = ly*((int)(i/ny));
    		N(i, 1) = lx*(i-ny*((int)(i/ny)));
    	}
    	for (int i = 1*(ny-1)/3; i<1+2*(ny-1)/3; i++) {
    		N(i, 2) = 1;
    		N(i, 3) = 1;
    	}
    	N((nx-1)*ny+(ny-1)/2, 5) = -l;
    	return;
    }
    
    
    
    
    inline void doff(vec& dof, int nodes, Mat<int>N){
    	dof(0) = 1;
    	dof(nodes) = 1;
    	for (int i = 0; i<nodes; i++){
    		if (i>=1){
    			dof(i) = (1-N(i, 2))+dof(i-1);
    			dof(i + nodes) = (1-N(i, 3))+dof(i+nodes-1);
    		}
    	}
    	for (int i = 1; i<nodes; i++){
    		if (N(i, 2) == 1){
    			dof(i) = 0;
    		}
    		if (N(i, 3) == 1){
    			dof(i+nodes) = 0;
    		}
    	}
    	for(int i = 0; i<nodes; i++){
    		if (dof(i+nodes) != 0){
    			dof(i+nodes) += dof(nodes-1);
    		}
    	}
    	return;
    }
    
    
    
    
    
    inline void PCLf1(int elems, int nodes, mat& PCL){
    	vec N11(elems);
    	vec N22(elems);
    	mat N1(nodes, nodes);
    	mat N2(nodes, nodes);
    	int ka = 0;
    	int kb = 0;
    	for (int i = 0; i<nodes; i++){
    		for (int k = 0; k<nodes; k++){
    			N1(i, k) = k+1;
    			N2(i, k) = i+1;
    			if (k>=i){
    				N1(i, k) = 0;
    				N2(i, k) = 0;
    			}
    		}
    	}
    	for (int i = 0; i<nodes; i++){
    		for (int k = 0; k<nodes; k++){
    			if (N1(k, i) > 0){
    				N11(kb) = N1(k, i);
    				kb++;
    			}
    			if (N2(k, i) > 0){
    				N22(ka) = N2(k, i);
    				ka++;
    			}
    		}
    	}
    	for (int i = 0; i<elems; i++){
    		PCL(i, 0) = N11(i);
    		PCL(i, 1) = N22(i);
    	}
    	return;
    }
    
    
    
    inline void PCLf2(int elems, int nodes, mat& PCL, Mat<int>N){
    	mat Le(elems, 2);
    	Le.zeros();
    	for (int i = 0; i<elems; i++){
    		Le(i, 0) = N(PCL(i, 0) - 1, 0) - N(PCL(i, 1) - 1, 0);
    		Le(i, 1) = N(PCL(i, 0) - 1, 1) - N(PCL(i, 1) - 1, 1);
    		PCL(i, 2) = sqrt(pow(Le(i, 0), 2) + pow(Le(i, 1), 2));
    	}
    	return;
    }
    
    
    
    
    inline void PCLf3(int elems, int nodes, mat&PCL, double lx, double ly, int& elem_no){
    	double adjacent = 1.01*sqrt(pow(lx, 2)+pow(ly, 2));
    	for (int i = 0; i < elems; i++){
    		if (PCL(i, 2)<adjacent){
    			PCL(i, 3) = 1;
    			elem_no++;
    		}
    	}
    }
    
    
    
    
    
    inline void Ef(mat& E, mat PCL, int nodes, int elems){
    	int kc = 0;
    	for (int i = 0; i<elems; i++){
    		if (PCL(i, 3) == 1){
    			for (int k = 0; k<4; k++){
    				E(kc, k) = PCL(i, k);
    			}
    			kc++;
    		}
    	}
    }
    
    
    
    
    
    void solverr(Mat<int>, mat, int, int, int, int, int, vec);
    void MATtoARR(mat, double**&);
    void ARRtoMAT(int, int, mat&, double**);
    void VECtoARR(vec, double*&);
    
    
    extern int linprog1(double**, int, double*, int, double*, int);
    
    
    int main(){
    
    	cout.precision(6);
    	int stop;
    	int H = 500;
    	int B = 250;
    	int nx = 3;
    	int ny = 7;
    	int l = 1;
    	int sc = 275;
    	int st = 275;
    	int j = 2;
    	const int nodes = nx*ny;
    	const int elems = nodes*(nodes-1)/2;
    	int elem_no = 0;
    	double lx;
    	double ly;
    	double adjacent_distance;
    	int addedmem = 0;
    	int addedmem_no = 1;
    	double disp_size[2];
    	Mat<int> N(nodes, 8); // contains node x position, y position, marks supports, marks external loads
    	vec dof(2*nodes); // cumulative list of  degrees of freedom
    	mat PCL(elems, 4); // first node, second node, distance, accepted
    
    	disp_size[0] = B + 20;
    	disp_size[1] = H + 20;
    	lx = B/(nx-1);
    	ly = H/(ny-1);
    
    	N.zeros();
    	PCL.zeros();
    
    	n1234(nodes, ny, nx, ly, lx, N, l); // function for creating N
    	PCLf1(elems, nodes, PCL); // function for creating the first 2 elements of PCL
    	doff(dof, nodes, N); // Degree of Freedom function
    	PCLf2(elems, nodes, PCL, N); // function for 3rd element of PCL
    	PCLf3(elems, nodes, PCL, lx, ly, elem_no); // function for 4th
    
    	mat E(elem_no, 4); // active members of PCL
    
    	Ef(E, PCL, nodes, elems); // function for E
    	vec nono(N.n_rows);
    	for (int i = 0; i< N.n_rows; i++){
    		nono(i) = N(i, 0);
    	}
    
    	uvec nodes_no = find(nono > 0);
    
    	solverr(N, E, st, sc, j, E.n_rows, nodes_no.size(), dof);
    
    	cin >> stop;
    }
    
    
    
    
    void solverr(Mat<int>N, mat E, int st, int sc, int j, int elem_no, int nodes_no, vec dof){
    	const double PI = 3.14159265359;
    	vec X(elem_no*2); X.zeros();
    	mat B(8*elem_no, 3); // sparse matrix
    	double n1;
    	double n2;
    	int kB = 0;
    	vec coordinate(2);
    	mat sub_matrix(2, 2);
    	for (int elem = 0; elem < elem_no; elem ++){
    		n1 = E(elem, 0); // finds the first out of the pair of nodes
    		n2 = E(elem, 1); // finds the second
    		coordinate(0) = (N(n2 - 1, 0) - N(n1 - 1, 0))/E(elem, 2); // change in x over total change
    		coordinate(1) = (N(n2 - 1, 1) - N(n1 - 1, 1))/E(elem, 2); // change in y over total change
    		for (int i = 0; i<2; i++){
    			for (double k = 0; k<2; k++){
    				sub_matrix(i, k) = coordinate(i)*(1 - 2*cos(k*PI/2));
    			}
    		}
    		if (sub_matrix(0, 0) != 0){
    			B(kB, 0) = n1;
    			B(kB, 1) = elem;
    			B(kB, 2) = sub_matrix(0, 0);
    			//B(n1, elem) = sub_matrix(0, 0);
    			kB += 1;
    			B(kB, 0) = n2;
    			B(kB, 1) = elem;
    			B(kB, 2) = -sub_matrix(0, 0);
    			//B(n2, elem) = -sub_matrix(0, 0);
    			kB += 1;
    		}
    		if (sub_matrix(1, 0) != 0){
    			B(kB, 0) = n1 + nodes_no;
    			B(kB, 1) = elem;
    			B(kB, 2) = sub_matrix(1, 0);
    			//B(n1 + nodes_no, elem) = sub_matrix(1, 0);
    			kB += 1;
    			B(kB, 0) = n2 + nodes_no;
    			B(kB, 1) = elem;
    			B(kB, 2) = -sub_matrix(1, 0);
    			//B(n2 + nodes_no, elem) = -sub_matrix(1, 0);
    			kB += 1;
    		}
    		if (sub_matrix(0, 1) != 0){
    			B(kB, 0) = n1;
    			B(kB, 1) = elem + elem_no;
    			B(kB, 2) = sub_matrix(0, 1);
    			//B(n1, elem + elem_no) = sub_matrix(0, 1);
    			kB += 1;
    			B(kB, 0) = n2;
    			B(kB, 1) = elem + elem_no;
    			B(kB, 2) = -sub_matrix(0, 1);
    			//B(n2, elem + elem_no) = -sub_matrix(0, 1);
    			kB += 1;
    		}
    		if (sub_matrix(1, 1) != 0){
    			B(kB, 0) = n1 + nodes_no;
    			B(kB, 1) = elem + elem_no;
    			B(kB, 2) = sub_matrix(1, 1);
    			//B(n1 + nodes_no, elem + elem_no) = sub_matrix(1, 1);
    			kB += 1;
    			B(kB, 0) = n1 + nodes_no;
    			B(kB, 1) = elem + elem_no;
    			B(kB, 2) = -sub_matrix(1, 1);
    			//B(n2 + nodes_no, elem + elem_no) = -sub_matrix(1, 1);
    			kB += 1;
    		}
    	}
    	vec n3(N.n_rows);
    	uvec n5;
    	int n7;
    	vec n4(N.n_rows);
    	uvec n6;
    	int n8;
    	for (int i = 0; i< N.n_rows; i++){
    		n3(i) = N(i, 2);
    		n4(i) = N(i, 3);
    	}
    	n5 = find(n3>0);
    	n6 = find(n4>0);
    	n7 = n5.size();
    	n8 = n6.size();
    	for (int i = 0; i<B.n_rows; i++){
    		if (B(i, 0) == n7){
    			B.shed_row(i);
    		}
    		if (B(i, 0) == n8 + nodes_no){
    			B.shed_row(i);
    		}
    	}
    	int i = 0;
    	do {
    		if (B(i, 2) <= 0){
    			B.shed_row(i);
    			i--;
    		}
    		i++;
    	} while (i<B.n_rows);
    	for (int i = 0; i<B.n_rows; i++){
    	}
    	double** Barray = new double*[B.n_rows];
    	MATtoARR(B, Barray);
    
    	n5 = find(n3==0);
    	n6 = find(n4==0);
    	vec f(n3.size()*2);
    	vec solverer(n3.size()*2);
    	for (int i = 0; i<n3.size(); i++){
    		f(i) = N(i, 4);
    		f(i + n3.size()) = N(i, 5);
    	}
    	vec OBJ(elem_no*2);
    	for (int i = 1; i<=elem_no; i++){
    		OBJ(2*i-2) = 2*j + (E(i-1, 2)/st);
    		OBJ(2*i-1) = 2*j + (E(i-1, 2)/sc);	
    	}
    
    	//cout << n3.size()*2 << "  " << elem_no*2 << "  " << B.n_rows << endl;
    
    	double* farray = new double[f.size()];
    	VECtoARR(f, farray);
    	double* objarray = new double[OBJ.size()];
    	VECtoARR(OBJ, objarray);
    
    	linprog1(Barray, (int)B.n_rows, farray, (int)f.size(), objarray, (int)OBJ.size());
    }
    void MATtoARR(mat Matr, double**& Arre){
    	for (int i = 0; i<Matr.n_rows; i++){
    		Arre[i] = new double(Matr.n_cols);
    		for (int k = 0; k<Matr.n_cols; k++){
    			Arre[i][k] = Matr(i, k);
    		}
    	}
    }
    void VECtoARR(vec Vect, double*& Arre){
    	for (int i = 0; i<Vect.size(); i++){
    		Arre[i] = Vect(i);
    	}
    }
    void ARRtoMAT(int rows, int cols, mat& Matr, double** Arre){
    	for (int i = 0; i<rows; i++){
    		for (int k = 0; k<cols; k++){
    			Matr(i, k) = Arre[i][k];
    		}
    	}
    }
    and


    Code:
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <glpk.h>
    #include <armadillo>
    using namespace std;
    using namespace arma;
    
    int linprog1(double** Barray, int rowsb, double* farray, int rowsf, double* objarray, int rowsobj) // rowsb = B.n_rows, 
    { glp_prob *lp;
    int* ia = new int[1+rowsb];
    int* ja = new int[1+rowsb];
    double* ar = new double[1+rowsb];
    double z;
    double *x = new double[rowsobj];
    double *p = new double[rowsf];
    
    lp = glp_create_prob();
    glp_set_prob_name(lp, "sample");
    //s3: glp_set_obj_dir(lp, GLP_MAX);
    glp_add_rows(lp, rowsf);
    //glp_set_row_name(lp, 1, "p");
    //glp_set_row_bnds(lp, 1, GLP_FX, 100.0, 100.0);
    //glp_set_row_name(lp, 2, "q");
    //glp_set_row_bnds(lp, 2, GLP_FX, 600.0, 600.0);
    //glp_set_row_name(lp, 3, "r");
    //glp_set_row_bnds(lp, 3, GLP_FX, 300.0, 300.0);
    for (int i = 0; i<rowsf; i++){ // these are the f's
    //glp_set_row_name(lp, i+1, "aaaa");
    glp_set_row_bnds(lp, i+1, GLP_FX, farray[i], farray[i]); 
    }
    
    //glp_add_cols(lp, 3);
    glp_add_cols(lp, rowsobj);
    //glp_set_col_name(lp, 1, "x1");
    //glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);
    //glp_set_obj_coef(lp, 1, 10.0);
    //glp_set_col_name(lp, 2, "x2");
    //glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);
    //glp_set_obj_coef(lp, 2, 6.0);
    //glp_set_col_name(lp, 3, "x3");
    //glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0);
    //glp_set_obj_coef(lp, 3, 4.0);
    for (int i = 0; i<rowsobj; i++){
    //glp_set_col_name(lp, i+1, "bbbb");
    glp_set_col_bnds(lp, i+1, GLP_LO, 0.0, 0.0);
    glp_set_obj_coef(lp, i+1, objarray[i]);
    }
    //ia[1] = 1, ja[1] = 1, ar[1] = 1.0; /* a[1,1] = 1 */
    //ia[2] = 1, ja[2] = 2, ar[2] = 1.0; /* a[1,2] = 1 */
    //ia[3] = 1, ja[3] = 3, ar[3] = 1.0; /* a[1,3] = 1 */
    //ia[4] = 2, ja[4] = 1, ar[4] = 10.0; /* a[2,1] = 10 */
    //ia[5] = 3, ja[5] = 1, ar[5] = 2.0; /* a[3,1] = 2 */
    //ia[6] = 2, ja[6] = 2, ar[6] = 4.0; /* a[2,2] = 4 */
    //ia[7] = 3, ja[7] = 2, ar[7] = 2.0; /* a[3,2] = 2 */
    //ia[8] = 2, ja[8] = 3, ar[8] = 5.0; /* a[2,3] = 5 */
    //ia[9] = 3, ja[9] = 3, ar[9] = 6.0; /* a[3,3] = 6 */
    for (int i = 0; i<rowsb; i++){
    	ia[i+1] = (int)Barray[i][0];
    	ja[i+1] = (int)Barray[i][1];
    	ar[i+1] = (int)Barray[i][2];
    }
    //glp_load_matrix(lp, 9, ia, ja, ar);
     glp_load_matrix(lp, rowsobj*rowsf, ia, ja, ar);
    glp_simplex(lp, NULL);
    z = glp_get_obj_val(lp);
    //x1 = glp_get_col_prim(lp, 1);
    //x2 = glp_get_col_prim(lp, 2);
    //x3 = glp_get_col_prim(lp, 3);
    for (int i = 0; i<rowsobj; i++){
    x[i] = glp_get_col_prim(lp, i+1);
    }
    printf("\nz = %g; x1 = %g; x2 = %g; x3 = %g\n",
    z);//, x1, x2, x3);
    for (int i = 0; i<rowsobj; i++){
    printf("x = ", x[i]);
    }
    glp_delete_prob(lp);
    	 int cat;
    	 cin >> cat;
    return 0;
    }
    /* eof */

    I am using the gnu glpk library to calculate a linear program for my matrices.

    Does anyone know why I get the error message exit code 3 which apparently means "The system cannot find the path specified"?

    Thanks in advance!

  2. #2
    Join Date
    Dec 2012
    Posts
    21

    Re: "The system cannot find the path specified"

    The error only occurs when I call the linprog1 function, so the error should lie in the second code.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: "The system cannot find the path specified"

    Your code is very poorly formatted, making it very hard to read. There are a lot of mistakes in that function that is giving you problems, so I will go through them.
    Code:
    int* ia = new int[1+rowsb];
    int* ja = new int[1+rowsb];
    double* ar = new double[1+rowsb];
    double z;
    double *x = new double[rowsobj];
    double *p = new double[rowsf];
    First, let's look at this above. This code causes a memory leak as you never clean up the memory. If you repeatedly call linprog1() in your main application, you will exhaust the heap of free memory. Nowhere is a delete[] issued for any of those calls to new[]. Then you have this in your solverr() function:
    Code:
    double** Barray = new double*[B.n_rows];
    	MATtoARR(B, Barray);
    //...
    double* farray = new double[f.size()];
    	VECtoARR(f, farray);
    	double* objarray = new double[OBJ.size()];
    	VECtoARR(OBJ, objarray);
    Again, no call to delete[] to clean up the memory. If you were to call this function repeatedly throughout a program, the memory/resource leak adds up here.

    In modern C++ programming, the use of proper container classes instead of the "new[]/delete[]" combination is what is done to implement dynamic arrays. Containers such as std::vector, or if using MFC, CArray, supersedes the usage of using new[]/delete[] in this way. Unless what is written is some sort of memory allocator or you're writing your own container class, new[]/delete[] should not be used if your goal is a dynamic array, unless you can justify its usage.

    The reason why container classes are better than new[]/delete[]:

    1) The memory allocation and deallocation is automatically done without you having to do anything yourself. This allows you to create the object that wraps a dynamic array, use it, and when the function or functional block exits, the object is destroyed via destructor. No need for you to call delete[] anywhere.

    2) If your function returns for any reason, whether it is through a return statement, the end of the function is reached, or an exception is thrown causing the code to leave the function, the container class's destructor cleans up the memory. Using new[]/delete[], you need to stick a delete[] for every single return point in your program, and you also have to write a catch() block for any every exception that may be thrown, since you have to delete[] the memory inside the catch blocks (the latter case makes it almost a requirement to use a container class as opposed to new[]/delete[]).

    So which sounds easier and safer? The new[]/delete[] or the container classes?

    Another problem -- you're calling functions without caring if what is returned is valid. For example:
    Code:
        lp = glp_create_prob();
    You then use "lp" assuming that is valid. How do you know that lp is not NULL or something that can't really be used?

    Also, you should name your variables with descriptive names, not "x" and "p" as you've done in that function.

    Last, are you debugging your code using the debugger? Did you unit test the function you wrote, or did you write the whole thing and expected it to work right out of the box?
    Code:
    #include <vector>
    //..
    std::vector<double*> Barray(B.n_rows);
    	MATtoARR(B, &Barray[0]);
    //...
    std::vector<double> farray(f.size());
    VECtoARR(f, &farray[0]);
    std::vector<double> objarray(OBJ.size());
    VECtoARR(OBJ, &objarray[0]);
    //...
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <glpk.h>
    #include <armadillo>
    #include <vector>
    
    using namespace std;
    using namespace arma;
    
    int linprog1(const std::vector<double*>& Barray, int rowsb, const std::vector<double>& farray, int rowsf, const std::vector<double>& objarray, int rowsobj) // rowsb = B.n_rows, 
    { 
        glp_prob *lp;
        std::vector<int> ia(1+rowsb);
        std::vector<int> ja(1+rowsb);
        std::vector<double> ar(1+rowsb);
        double z;
        std::vector<double> x(rowsobj);
        std::vector<double> p(rowsf);
    
        lp = glp_create_prob();
        glp_set_prob_name(lp, "sample");
        glp_add_rows(lp, rowsf);
        for (int i = 0; i<rowsf; i++)
        {
            glp_set_row_bnds(lp, i+1, GLP_FX, farray[i], farray[i]); 
        }
    
        glp_add_cols(lp, rowsobj);
        for (int i = 0; i<rowsobj; i++)
        {
            glp_set_col_bnds(lp, i+1, GLP_LO, 0.0, 0.0);
            glp_set_obj_coef(lp, i+1, objarray[i]);
        }
        for (int i = 0; i<rowsb; i++)
        {
    	ia[i+1] = (int)Barray[i][0];
    	ja[i+1] = (int)Barray[i][1];
    	ar[i+1] = (int)Barray[i][2];
        }
        glp_load_matrix(lp, rowsobj*rowsf, &ia[0], &ja[0], &ar[0]);
        glp_simplex(lp, NULL);
        z = glp_get_obj_val(lp);
        for (int i = 0; i<rowsobj; i++)
        {
            x[i] = glp_get_col_prim(lp, i+1);
        }
        glp_delete_prob(lp);
        return 0;
    }
    Eliminating the comments and code that just does printing, the above does not cause a memory leak. Now it still doesn't address the issues I mentioned concerning not checking your return values from various functions.

    You need to start using proper container classes such as vector instead of the new[] / delete[], then all or most of that pointer usage is not necessary at all and the program becomes safer and less prone to memory related bugs. As a matter of fact, I bet that even the linprog function parameters itself need not be pointers, but references to vectors (as my code shows). The only thing that needs a pointer are the third-party library functions, and I've demonstrated above how to use vector to "fool" those functions into using a vector as a parameter instead of a pointer (pass a pointer to the first element of the vector).

    Also, check your return values, and debug your code step by step using the debugger.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 8th, 2012 at 11:24 AM.

  4. #4
    Join Date
    Dec 2012
    Posts
    21

    Re: "The system cannot find the path specified"

    Thank you!

    This is all very useful, and the std::Vector (or rather arma::Col) that I am now using thanks to you works great!

    I actually tried to do it this way earlier, but did not add the [0] where I called the variables. Why is this zero necessary? What is it?


    Thanks again, if you don't mind I may find myself uploading one or two more questions to this thread later on in the hope that you may be able to help me again

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: "The system cannot find the path specified"

    Quote Originally Posted by gugge444 View Post
    Thank you!

    This is all very useful, and the std::Vector (or rather arma::Col) that I am now using thanks to you works great!
    Not surprising. A lot of errors become magically eliminated once a vector is used -- the memory leaks clear up, and the hard-to-maintain pointers also become a moot point.

    The only thing that can't be corrected by using vector is if you access an element out-of-bounds using the [] operator. For that, vector has the at() function, but that requires you to change your code to call at() instead of using [].
    I actually tried to do it this way earlier, but did not add the [0] where I called the variables. Why is this zero necessary? What is it?
    The vector has an internal array. To access the internal array, you need to get a pointer to the first element of the array. That's why you need to get the address of the first element. (The vector's [] is actually an overloaded operator that gets to the array element).

    The CArray is the same thing as vector in terms of what they accomplish. All of that code you had now with new[]/delete[] is already done by CArray and vector internally (but with smarts added to it that you may not have even considered). So basically all you were doing is copying what vector/CArray was doing, but you were doing things the naive way while those container classes are much smarter and use new[]/delete[] and free-store properly, safely, and optimally.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Dec 2012
    Posts
    21

    Re: "The system cannot find the path specified"

    I feel the code should make more sense now, but the "The program '[16172] top.exe: Native' has exited with code 3 (0x3)." error still appears... Here is the code now if you feel like having a look.

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <glpk.h>
    #include <armadillo>
    using namespace std;
    using namespace arma;
    
    void linprog1(arma::Mat<double> B, arma::Col<double> f, arma::Col<double> OBJ) // rowsb = B.n_rows (sp),  linprog in matlab: coeff(obj), constraint coeff(B), right hand sides(f), lower bound(X) WHAT I WANT IN PI-SYMBOL LAGRANGE
    {
    	glp_prob *lp;
    	arma::Col<int> ia(1+B.n_rows);
    	arma::Col<int> ja(1+B.n_rows);
    	arma::Col<double> ar(1+B.n_rows);
    	double z;
    	arma::Col<double> x(OBJ.n_rows);
    	arma::Col<double> pok(f.n_rows);
    
    	lp = glp_create_prob();
    	glp_set_prob_name(lp, "sample");
    
    	glp_add_rows(lp, f.n_rows);
    
    	for (int i = 0; i<f.n_rows; i++){
    		glp_set_row_bnds(lp, i+1, GLP_FX, f(i), f(i)); 
    	}
    
    
    	glp_add_cols(lp, OBJ.n_rows);
    
    	for (int i = 0; i<OBJ.n_rows; i++){
    		glp_set_col_bnds(lp, i+1, GLP_LO, 0.0, 0.0);
    		glp_set_obj_coef(lp, i+1, OBJ(i));
    	}
    
    
    	for (int i = 0; i<B.n_rows; i++){
    		ia(i+1) = B(i,0);
    		ja(i+1) = B(i,1);
    		ar(i+1) = B(i,2);
    	}
    	glp_load_matrix(lp, OBJ.n_rows*3, &ia(1), &ja(1), &ar(1));
    	glp_interior(lp, NULL);
    	z = glp_ipt_obj_val(lp);
    	for (int i = 0; i<f.n_rows; i++){
    		pok(i) = glp_ipt_row_dual(lp, i+1);
    	}
    
    	for (int i = 0; i<OBJ.n_rows; i++){
    		x(i) = glp_ipt_col_prim(lp, i+1);
    	}
    
    	glp_delete_prob(lp);
    	int cat;
    	cin >> cat;
    	return;
    }
    /* eof */
    I keep trying to figure out at what point in the code the error appears, but I don't really know where to start with that.

  7. #7
    Join Date
    Dec 2012
    Posts
    21

    Re: "The system cannot find the path specified"

    It may possibly be that I use too much memory?

  8. #8
    Join Date
    Dec 2012
    Posts
    21

    Re: "The system cannot find the path specified"

    I ran a cin >> through the text and found that the error is in the
    Code:
    glp_load_matrix(lp, OBJ.n_rows*3, &ia(1), &ja(1), &ar(1));
    statement... any ideas?

  9. #9
    Join Date
    Dec 2012
    Posts
    21

    Re: "The system cannot find the path specified"

    allright, solved that now, no need to reply

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: "The system cannot find the path specified"

    Quote Originally Posted by gugge444 View Post
    allright, solved that now, no need to reply
    Your new code still has issues:
    Code:
    void linprog1(arma::Mat<double> B, arma::Col<double> f, arma::Col<double> OBJ)
    You're passing these parameters by value. This is not optimal, or even correct.

    You should be passing these by reference or const reference. There are only a few situations where an object must be passed by value, otherwise you should always pass by reference or const reference. Take a look more closely at my edited post above.

    Passing by value is not optimal, as a copy of the data has to be made to the function. Also, passing these objects by value may be an error, especially if your class doesn't have a proper coded copy/assignment operator, and if you're expecting the function to retain the results in the passed-in objects.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Dec 2012
    Posts
    21

    Re: "The system cannot find the path specified"

    Alright, that is very good to know, I thought the variables were only to be called in that way when you desired to modify their value within the function.

    The function now runs, and the Linear Program that it is supposed to create runs fine. The only problem is that the output of the function doesn't make sense, which makes me think that I am making an error in my code somewhere, the code is

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <glpk.h>
    #include <armadillo>
    using namespace std;
    using namespace arma;
    
    void linprog1(arma::Mat<double>& A_sp, arma::Col<double>& b, arma::Col<double>& C) // rowsb = B.n_rows (sp),  linprog in matlab: coeff(C), constraint coeff(B), right hand sides(f), lower bound(X) WHAT I WANT IN PI-SYMBOL LAGRANGE
    {
    	int stopper;
    	glp_prob *lp;
    	arma::Col<int> ia(1+A_sp.n_rows);
    	arma::Col<int> ja(1+A_sp.n_rows);
    	arma::Col<double> ar(1+A_sp.n_rows);
    	double z;
    	arma::Col<double> x(C.n_rows);
    	arma::Col<double> pok(b.n_rows);
    
    	lp = glp_create_prob();
    	glp_set_prob_name(lp, "sample");
    
    	glp_add_rows(lp, b.n_rows);
    
    	for (int i = 0; i<b.n_rows; i++){
    		glp_set_row_bnds(lp, i+1, GLP_FX, b(i), b(i)); 
    	}
    
    	glp_add_cols(lp, C.n_rows);
    
    	for (int i = 0; i<C.n_rows; i++){
    		glp_set_col_bnds(lp, i+1, GLP_LO, 0.0, 0.0);
    		glp_set_obj_coef(lp, i+1, C(i));
    	}
    
    
    	for (int i = 0; i<A_sp.n_rows; i++){
    		ia(i+1) = A_sp(i,0) + 1;
    		ja(i+1) = A_sp(i,1) + 1;
    		ar(i+1) = A_sp(i,2);
    		cout << &ia(1) << " " << i+1 << endl;
    	}
    	//cin >> stopper;
    	glp_load_matrix(lp, A_sp.n_rows - 1, &ia(1), &ja(1), &ar(1));
    	glp_write_mps(lp, GLP_MPS_FILE, NULL, "herro.mps");
    	glp_interior(lp, NULL);
    	glp_write_mps(lp, GLP_MPS_FILE, NULL, "herro2.mps");
    	z = glp_ipt_obj_val(lp);
    	for (int i = 0; i<b.n_rows; i++){
    		pok(i) = glp_ipt_row_dual(lp, i+1);
    	}
    
    	for (int i = 0; i<C.n_rows; i++){
    		x(i) = glp_ipt_col_prim(lp, i+1);
    	}
    
    	glp_delete_prob(lp);
    	cin >> stopper;
    	return;
    }
    /* eof */
    The variables sent to the function are read from row/column 1 rather than 0 which is why I have added ones in certain places, however I am wondering whether I am sending through a variable too many or too few in one place or another... The reason I think so is because of this line:
    Code:
    	glp_load_matrix(lp, A_sp.n_rows - 1, &ia(1), &ja(1), &ar(1));
    this line should have a second value of A_sp.n_rows, but doesn't execute properly unless I subtract one from it.

    As usual I would very much appreciate your help!

    Thank you.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: "The system cannot find the path specified"

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <glpk.h>
    #include <armadillo>
    using namespace std;
    using namespace arma;
    
    void linprog1(arma::Mat<double>& A_sp, arma::Col<double>& b,
    I have no idea what a "Col" or "Mat" is. These are proprietary types as they are not standard C++ types or MFC types.
    however I am wondering whether I am sending through a variable too many or too few in one place or another....
    ...
    Instead of wondering what may be happening, you need to debug your code using the debugger to find out where things go wrong. There is no need to be using cout statements to debug or random guesses and trial/error -- the Visual C++ compiler comes with one of the most powerful debuggers created for PC based C++ systems.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Dec 2012
    Posts
    21

    Re: "The system cannot find the path specified"

    At this point, for time reasons, I have to leave the programming part of my dissertation for a few week.

    I therefore wanted to take this opportunity to thank you for your help and patience. I have saved this conversation to my pc, and have learnt more from it than I have from any text-books.

    Thanks for your help, and you'll see me back on this forum in a month or so!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured