Quick lpsolve question

Hi Guys,

Hopefully this is a quicky but I am struggling to figure it out. I have code using lpsolve to solve a linear equation. Everything works great and I get the correct output when I run the code as it is. I want the code to solve for integer values though and as it is currently setup all the variables are double*. I don't know how to make it so that I solve for integer values only. Please help if you can!! the code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

int main( int argc, char* argv[])
{
	lprec *lp;
	REAL solution[4];

	if ((lp = make_lp(0,4)) == NULL);

	{

		//first constraint		10*Ax + 12*Bx + 8*Cx + 18*Dx >= 55000
		//Second constrant		10*Ax + 12*Bx + 8*Cx + 18*Dx <= 65000
		{
			double row[] = {0,10,12,8,18};
			add_constraint(lp,row,GE,55000);
		}
		{
			double row[] = {0,10,12,8,18};
			add_constraint(lp,row,LE,65000);
		}


		//Thrid constraint		14*Ax + 11*Bx + 10*Cx + 17*Dx >= 60000
		//Fourth constraint		14*Ax + 11*Bx + 10*Cx + 17*Dx <= 69000
		{
			double row[] = {0,14,11,10,17};
			add_constraint(lp,row,GE,60000);
		}
		{
			double row[] = {0,14,11,10,17};
			add_constraint(lp,row,LE,69000);
		}


		//Fifth constraint		13*Ax + 10*Bx + 11*Cx + 20*Dx >= 62000
		//Sixth constraint		13*Ax + 10*Bx + 11*Cx + 20*Dx >= 68000
		{
			double row[] = {0,13,10,11,20};
			add_constraint(lp,row,GE,62000);
		}
		{
			double row[] = {0,13,10,11,20};
			add_constraint(lp,row,LE,68000);
		}

		
		//Seventh constraint	Ax >= 650
		//Eight constraint		AX <= 1000
		{
			double col[] = {0,1,0,0,0};
			add_constraint(lp,col,GE,650);
		}
		{
			double col[] = {0,1,0,0,0};
			add_constraint(lp,col,LE,1000);
		}


		//Ninth constraint		Bx >= 1700
		//Tenth constraint		Bx <= 2200
		{
			double col[] = {0,0,1,0,0};
			add_constraint(lp,col,GE,1700);
		}
		{
			double col[] = {0,0,1,0,0};
			add_constraint(lp,col,LE,2200);
		}


		//Eleventh constraint	Cx >= 1100
		//Twelfth constraint	Cx <= 1400
		{
			double col[] = {0,0,0,1,0};
			add_constraint(lp,col,GE,1100);
		}
		{
			double col[] = {0,0,0,1,0};
			add_constraint(lp,col,LE,1400);
		}


		//Thirteenth constraint	Dx >= 880
		//Fourteenth constraint	Dx >= 1300
		{
			double col[] = {0,0,0,0,1};
			add_constraint(lp,col,GE,880);
		}
		{
			double col[] = {0,0,0,0,1};
			add_constraint(lp,col,LE,1300);
		}


		// Maximize the Objective Function
		//Objective Function 4.5*Ax +5.5*Bx + 6.5*Cx + 7*Dx
		{
			double row[] = {0, -4.5, -5.5, -6.5, -7};
			set_obj_fn(lp,row);
		}

		solve(lp);

		cout << "Optimal Value is: " << -1*get_objective(lp) << endl;

		get_variables(lp, solution);
		cout << "Optimal solution: " << endl;
		for (int i=0; i < 4; i++)
				cout << "x[" << i+1 << "] = " << solution[i] << endl;

		delete_lp(lp);

		return(0);
	}
}
Can anyone help with this?
Please?
I am not familiar with the library, but have you looked at its set_int function?

set_int

Set the type of the variable. Integer or floating point.

unsigned char set_int(lprec *lp, int column, unsigned char must_be_int);
That won't work because it isn't compatible with the lpsolve library. Thanks for the suggestion though.
I'd like to help if I can, but will need to download the same library you have. Are you using the lp_solve from sourceforge?

http://sourceforge.net/projects/lpsolve/

Which version?
I'm using one from sourceforge but not from that link exactly.

I got lp solve 5.5.2.0 source.tar.gz from the following:

http://sourceforge.net/projects/lpsolve/files/lpsolve/5.5.2.0/

Once you have that you need to link some different libraries and such.
Last edited on
I downloaded and compiled lpsolve 5.5.2.0.

I was able to add set_int just before the call to solve() and retrieved all integer solutions.

1
2
3
4
5
6
7
8
9
10
11
		// set variables to be int
		set_int(lp, 1, 1);
		set_int(lp, 2, 1);
		set_int(lp, 3, 1);
		set_int(lp, 4, 1);

		int retVal = solve(lp);

		cout << "Solve returned " << retVal << endl;

		cout << "Optimal Value is: " << -1*get_objective(lp) << endl;


Is this not what you're looking for?
Topic archived. No new replies allowed.