CS152 Lab 6: Searching and Optimization

Java Python CS152

Lab 6: Searching and Optimization

The purpose of this lab/project is to get you thinking about automating a scientific experiment and optimizing the parameters of a process.

This is the second part of our study of the elephant population in Kruger National Park in South  Africa. This week we'll be focusing on how varying the parameters will change the management strategy.

Rather than manually exploring the parameter space, we will automate the process and use a search method to find the optimal darting percentage given the simulation parameters.

Setting up the Lab/Project

Create a Lab06 folder and open VS Code in it.

L1. Implement binary search on a sorted list

In your Lab06 folder create a new file, search.py. Then create a function searchSortedList that takes in two parameters, a list and a target value. You can use the following template.

def searchSortedList ( mylist, value ):

# assign to the variable done, the value False

# assign to the variable found, the value False

# assign to the variable count, the value 0

# assign to the variable maxIdx, the one less than the

length of mylist

# assign to the variable minIdx, the value 0

# start a while loop that executes while done is not True

# increment count  (which keeps track of how many times

the loop executes

# assign to testIndex the average of maxIdx and minIdx

(use integer math)

# if the myList value at testIndex is less than

value

# assign to minIdx the value testIndex + 1

# el if the myList value at testIndex is greater

than value

# assign to maxIdx the value testIndex - 1

# else

# set done to True

# set found to True

# if maxIdx is less than minIdx

# set done to True

# set found to False

return (found, count)

Once you have coded that up, import the random package, copy the following test function, and run it to see if your function finds the value 42 in the list (it should). Think about why it can take a different number of steps each time you run the program. Experiment with different values of N and relate the number of steps that the function takes to complete to log2(N).

def test ():

a = []

N = 10**6

for i in range (N):

a.append ( random.rand int (0,N) )

a.append (42)

a.sort ()

print (searchSortedList ( a, 42 ))

if name == " main ":

test ()

This function is practice for creating the optimization function for the elephant simulation. That function will have a similar structure, and it will also execute a binary search, but it will have more parameters and be more flexible in how it works.

L2. Write a function to generate a default parameter list

Create a Project06 folder and copy your elephant.py file from the last project.

Next, add a new function to your elephant.py file. The function should be&nb CS152 Lab 6: Searching and Optimization sp;called

defaultParameters (). It should take no arguments and return a list with all of the necessary parameters for the simulation with their default values.

calvingIn t = 3.1

probDart = 0.0

juvAge = 12

maxAge = 60

probCalfSurvival = 0.85

probAdultSurvival = 0.996

probSeniorSurvival = 0.2

carryingCapacity = 1000

numYears = 200

NOTE: For this project, set the default carrying capacity to 1000.

L3. Write an elephant simulation function

Next, also in your elephant.py file, create a function elephantSim. The arguments to the function should be probDart and a parameter list, which should have a default value of None. In other words, set it up as the following.

def elephantSim ( probDart, inputParameters = None ):

First, set up the parameters. If inputParameters is equal to the value None, then assign to parameters the result of calling your defaultParameters function. Otherwise, assign to parameters the value of inputParameters.

Next, store the parameter value for probDart in the correct location in the parameters list.

Then, declare an empty list named results where we will store the return from the call to runSimulation as follows:

results = results + runSimulation (parameters)

Place this in a loop which will call runSimulation five times and collect all of the results in a single list. The structure of the results list is that of a list of lists. Each sublist consists of the total population and other statistics for a given simulation year.

Finally, loop over the results list and calculate the average total population. Remember, the total population for each year is the first element in each sublist.

elephantSim will return the following crucial piece of information: (carrying capacity) - (average total population of the five simulations). Cast this value as an integer before returning. Just like our cost function in all our search algorithms, this metric will guide the search for the optimal darting probability.

You can think of this return value as specifying whether too many or too few elephants are being darted. If the return value is negative, then the population is too big and we need to tweak the darting probability higher so that the elephant population shrinks; and if the return value is positive, then the population is too small and we need to tweak the darting probability lower so that the elephant population grows.

L4. Test your elephantSim function

Once you have written elephantSim, use the testfile test_elephantSim.py to evaluate its  performance. It tests the percent darted for five different values. The difference should go from negative (darting probability too low) to positive (darting probability too high) for the default simulation parameters.

Example output: (your numbers may be different because of the random and your elephant parameter boundaries are different)

probDarting 0.405 diff -385

probDarting 0.415 diff -331

probDarting 0.425 diff -172

probDarting 0.435 diff -8

probDarting 0         

提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值