# preface

Here, I will show some ideas of solving problems with the problem solving function optimproblem I sent before, as well as the corresponding code. Here, I declare that the title used in this article comes from the following articles of dada Zi: Mathematical modeling [Planning Model - linear programming (integer programming, 0-1 programming), nonlinear programming - attached: case analysis, production and sales of dairy products (detailed solution process)]_ Houdezai CSDN blog

# 1, What is linear programming?

In fact, the content of linear programming is divided into many parts. I have sorted out some scattered parts with the mind map (it has not been sorted out, and will be supplemented slowly later). The mind map is as follows (the later articles will be updated with the author's continuous learning and error correction). If there is any error, welcome the reader to correct it # 2, Hands on practical problems

## Type I: course selection system of 0-1 model:  Linear programming problem is to grasp three points: objective function, constraints and decision variables.

2.1 classification issues

Text analysis:

About this problem, the first is to classify the problems. There is no doubt that our course selection is either or not, so if we don't choose, it is 0, and if we choose, it is 1. It is a standard 0-1 planning problem;

Code analysis:

0-1 programming, that is, we use the setting variable x in the optimproblem function to set the range. The lower limit is 0 and the upper limit is 1. Since there are only two values of 0 and 1, we also need to adjust its type, that is, integer integer. At the same time, because there are nine courses, X is a column vector of 9 rows and 1 columns, namely x1, x2...x9. Here, The setting of unknowns is understood. The code is shown below.

`x = optimvar('x',9,'Type','integer','LowerBound',0,'UpperBound',1);`

2.2 find the corresponding three points

Text analysis:

Then let's look for the three points:

First, the decision variable is actually each elective course. Nine different unknowns represent whether to choose the Qi course or not;

Second: constraint conditions. The constraint conditions are at least two mathematics courses, three statistics and two computer courses given by the topic. Under this condition, the preconditions such as calculus and linear algebra need to be selected first if the optimization method is selected;

Third: objective function. Objective function is to have as few elective courses as possible.

Code analysis:

1. The decision variables were mentioned in the previous section and will not be repeated here

2. For constraints, if you don't want to see the text, you can see the figure below!

At least two mathematics courses and three statistics courses in front are very simple, that is, add up no more than two curriculum variables containing mathematics courses and no more than three statistics; You may have to think about what to choose first. In fact, if you take the optimization method x3 as an example, you should first choose calculus x1 and linear algebra X2, that is, x1 > 0 and X2 > 0 as the premise. However, you think that the value range of X is 0-1, so when x1 and X2 are selected, their values are 1, It adds up to 2, that is, I can only choose whether to select x3 when x1+x2=2. If I use 2 * x3 - x1 - x2 < = 0, it will just be satisfied, because it happens that if I select x3, the value is 1. If neither x1 nor X2 is selected, it will be 0, and x3 must also be 0 to meet that condition. When both x1 and X2 are selected, it will just be 2, If x3 is selected, the previous is 2. Subtraction also meets the conditions, so it is just right. In other cases, readers can think about it by themselves, which is also a general idea. The corresponding constraint diagram, train of thought diagram and code are as follows: Constraint picture Train of thought diagram

```con1 = 2*x(3) - x(1) - x(2) <= 0;
con2 = x(4) - x(7) <= 0;
con3 = 2*x(5) - x(1) - x(2)<=0;
con4 = x(6) - x(7) <= 0;
con5 = x(8) - x(5) <= 0;
con6 = 2*x(9) - x(1) - x(2)<=0;
con7 = sum(x(1:5))>=2;
con8 = x(3)+x(5)+x(6)+x(8)+x(9)>=3;
con9 =  x(4)+x(6)+x(7)+x(9)>=2;
cons = [con1;con2;con3;con4;con5;con6];
cons2 = [con7;con8;con9];
pro.Constraints.con1=cons;
pro.Constraints.con2=cons2;```

# two point three   Code example

```clc
clear
tic
pro = optimproblem('ObjectiveSense','min');

x = optimvar('x',9,'Type','integer','LowerBound',0,'UpperBound',1);

pro.Objective = sum(x);
con1 = 2*x(3) - x(1) - x(2) <= 0;
con2 = x(4) - x(7) <= 0;
con3 = 2*x(5) - x(1) - x(2)<=0;
con4 = x(6) - x(7) <= 0;
con5 = x(8) - x(5) <= 0;
con6 = 2*x(9) - x(1) - x(2)<=0;
con7 = sum(x(1:5))>=2;
con8 = x(3)+x(5)+x(6)+x(8)+x(9)>=3;
con9 =  x(4)+x(6)+x(7)+x(9)>=2;
cons = [con1;con2;con3;con4;con5;con6];
cons2 = [con7;con8;con9];
pro.Constraints.con1=cons;
pro.Constraints.con2=cons2;
[sol fval_LL] = solve(pro);
sol.x
fval_LL
toc
``` The final courses to be selected are 6, which are x1 = x2 = x3 = x5 = x6 = x7 =1.

Tags: MATLAB

Posted on Mon, 20 Sep 2021 14:29:35 -0400 by lovasco