Path: blob/main/Homework/Lesson 05 HW - Local Optimization/Homework_05.ipynb
870 views
Homework 05: Local Optimization
When asking questions about homework in Piazza please use a tag in the subject line like HW1.3 to refer to Homework 1, Question 3. So the subject line might be HW1.3 question. Note there are no spaces in "HW1.3". This really helps keep Piazza easily searchable for everyone!
For full credit, all code in this notebook must be both executed in this notebook and copied to the Canvas quiz where indicated.
Hints for 1-3
The code for this problem is very similar to the Wyndor Quadratic Program. The harder part is formulating the objective function and constraints.Questions 1-3
The management of the Albert Hanson Company is trying to determine the best product mix for two new products. Because these products would share the same production facilities, the total number of units produced of the two products combined cannot exceed two per hour. Because of uncertainty about how well these products will sell, the profit from producing each product provides decreasing marginal returns as the production rate is increased. In particular, with a production rate of units per hour, it is estimated that Product 1 would provide a profit (in dollars per hour) of If the production rate of product 2 is units per hour, its estimated profit (in dollars per hour) would be
Question 1 (8 points, manual)
Formulate a quadratic programming model and solve it using Pyomo with the ipopt solver. Put your complete code below and in your CoCalc notebook. We are allowing fractional solutions here, so this is not an integer programming problem.
Question 2 (1 point, auto)
Enter the maximum value of the profit to two decimal places.
Question 3 (1 point, auto)
To achieve the maximum value of the profit function, how many units of product 2 should be introduced each hour? Enter your answer to two decimal places.
Questions 4-7
Consider the nonconvex profit function with
Question 4 (2 points, manual)
Graph the function for in your notebook and determine how many local minima there are. Do not count endpoints. Put your complete code to produce the graph below and in your CoCalc notebook.
Question 5 (1 point, auto)
How many local minima are there? Do not count endpoints.
Question 6 (1 point, auto)
What is the value of at the second-largest local maximum? Enter your answer to two decimal places.
Question 7 (4 points, manual)
Write a multistart procedure that starts from uniform randomly sampled points in [0,5] to locate the absolute maximum value of profit. The algorithm should stop after 20 iterations in which no improvement in the max value has been obtained. Include your code and the output from your code in the space below. Your code should print out the max value and the x-value where it occurs.
However you don't need the dim variable since there is only a single variable x in this problem.
Use x_initial = np.random.uniform(0,5) to get the initial search point in each iteration.
Questions 8-10
To find the line of least squares fit of the form to fit data of the form we minimize a loss function. The loss function is the sum of the squares residuals and only depends on and for fixed -data:
(Be sure to compute the squares before you compute the sum!)
The file age_height.csv contains ages (years) and heights (inches) for 7 children. Write Python code to evaluate the loss function (follow along with the logistic regression example while making suitable changes to the loss function) and use minimize
to identify the coefficients of the line of least-squares fit for predicting height () from (). Include a scatter plot of the data that includes a plot of the line.
Question 8 (10 points, manual)
Include complete Python code in the space below that includes the loss function, shows how to minimize it, prints out the output, and makes a plot of the line on a scatterplot of the data. You should also include your plot (check out savefig or use a cropped screenshot). The same elements should be in your CoCalc notebook. Here is the loss function from the logistic regression problem in the lesson along with a couple of notes to get you started.
Instead of p
and ll
compute ss
the sum of the squares as shown in the formula above and then return(ss)
. Make sure you're computing the sum of the squared residuals and not the sum of the residuals squared ... parentheses matter!
This is also a perfect use case for ChatGPT.
I entered this prompt 'given x and y lists and the equation of a line show how to make a scatterplot with the x and y lists and plot the line on top using python' and got a useful response.
Question 9 (1 point, auto)
What is the value of the intercept, ? Enter your answer to three decimal places.
Question 10 (1 point, auto)
What is the value of the slope, ? Enter your answer to three decimal places.
Questions 11-12
The knapsack problem is a classical combinatorial optimization problem that will be good for practicing with the ideas of discrete local search and multistart. Given a set of items, each with a weight and a value, determine which items to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. In the 0-1 version of the knapsack problem, the decision variables are binary (or boolean) and represent whether to include each item in the collection. We'll start with 20 items. You need to determine the collection of items that maximizes the value and keeps the total weight up to 50 (that is ).
The variables will be a vector of booleans of length num_items
. We could initialize a vector like this and then set the vector to include the 1st, 3rd, and 5th items:
The total weight of the items included in the collection:
The total value of the items included in the collection:
Implement a local search where the search starts with no items included in the collection and generates new states (moves) by randomly choosing one of the booleans in the state vector and toggling it. Like this:
Accept the state if the total weight is is and maximize the value by moving uphill
Question 11 (4 points, manual)
Write a local search algorithm that randomly changes one boolean and accepts the move if it improves the total value. Iterate until no improvements have been made in the last 1000 iterations. Write the algorithm as a function with the values and weights as inputs and that returns the best collection of items to include as well as the value and weight of that collection. Include your code and the results of running the code once in the space below.
You'll have to adapt it work with the knapsack problem.
Question 12 (4 points, manual)
Implement a variation of multistart search from the lesson. Instead of starting from random initial states, start each local search from an empty knapsack. The random moves should include sufficient randomness. You should do 200 local searches. Each local search should call the function you defined in Question 11. Clearly identify the best overall solution. Put complete code to do the searches and display the best results in the space below and in your CoCalc notebook. Do not print out information from each search, just the final results.
Next week we'll see some alternative search techniques that will generally enable us to find better solutions.
Question 13 (2 points, manual)
Enter the maximum total value for knapsack that you found in Question 12. If it's not over 400 then something is likely wrong.
Question 14 (10 points, manual)
The Value Balancing Problem was a Self_Assessment problem near the end of the Lesson_05 notebook. You can see our solution in the Self_Assessement_Soln notebook. Use the locsearch package in this directory to find a local solution to the Value Balancing problem. You can follow the example at the end of Lesson_05. We've copied some code from the lesson to get you started. Remember to set this up so that your decision variables (the group assignments) are stored in the state attribute of your subclass.
In the space below, include your complete code to solve the Value Balancing problem using the LocalSearcher class. You should also include output from a run of the code. The same elements should be in your ÇoCalc notebook.