s1.python

# coding=utf-8

import httplib2, time, json

INTERVAL = 10

class OdlUtil:
    url = ''
    def __init__(self, addr):
        self.url = addr
	

    def get_load(self,sw , port, container_name='default',username="admin", password="admin"):
		http = httplib2.Http()
		http.add_credentials(username, password)
		headers = {'Accept': 'application/json'}
		flow_name = 'flow_' + str(int(time.time()*1000))
		#h1h8body1 = r'{"flow": [{"id": "1","match": {"ethernet-match":{"ethernet-type": {"type": "2048"}},"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},"instructions": {"instruction": [{"order": "0","apply-actions": {"action": [{"output-action": {"output-node-connector": "4"},"order": "0"}]}}]},"priority": "1111","cookie": "1","table_id": "0"}]}'
		headers = {'Content-type': 'application/json'}
		response, content = http.request(uri='http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:%d/node-connector/openflow:%d:%d'%(sw,sw,port), method='GET',headers=headers)
		#print (content)
		data = json.loads(content)
		load = data["node-connector"][0]["opendaylight-port-statistics:flow-capable-node-connector-statistics"]["bytes"]["received"]
		return load

class Spy_port:

	def __init__(self):
		self.last_cost = 0
		self.cost = 0

		self.has_links = []

class Util:

	# init
	def __init__(self):
		# start
		print("start\n")

		# init odl
		self.odl = OdlUtil('http://127.0.0.1:8181')

		# init info
		self.all_ports = {}
		self.all_ports["port-1"] = Spy_port()
		self.all_ports["port-1"].has_links = [5]
		self.all_ports["port-2"] = Spy_port()
		self.all_ports["port-2"].has_links = [4]
		self.all_ports["port-3"] = Spy_port()
		self.all_ports["port-3"].has_links = [6]
		self.all_ports["port-4"] = Server_port()
	def balance(self):
		sw = 1
		while True:
			time.sleep(1)
		
			for i in range(1, 5):
				index = "port-" + str(i)

				if self.all_ports[index].last_cost == 0:
					self.all_ports[index].last_cost = self.odl.get_load(sw, i)
					print "init port " + str(i)
					continue

				last = self.all_ports[index].last_cost
				now = self.odl.get_load(sw, i)
				cost = (now - last)
				self.all_ports[index].cost = cost

				#if now - last == 0:
					#print "=="
					#continue

				print "port " + str(i) + ":" + str(cost)
				self.all_ports[index].last_cost = now		

		print "\n\n"

util = Util()
util.balance()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在你的代码中,`feasible_solutions` 是一个全局变量,用于存储找到的可行解。然而,在每次调用 `mycallback` 函数时,你都将 `feasible_solutions` 重新初始化为空列表。 为了解决这个问题,你可以将 `feasible_solutions` 的初始化移动到 `sp1` 函数之外,并将其作为参数传递给 `mycallback` 函数。 以下是修改后的代码示例: ```python def mycallback(s1, where, feasible_solutions): x = s1._x # 获取变量x if where == GRB.Callback.MULTIOBJ: # 获取当前得到的解 x_sol = s1.cbGetSolution(x) feasible_solutions.append(x_sol) # 将解添加到列表中 elif where == GRB.Callback.MIPSOL: # 当找到下一个可行解时,也将其添加到列表中 obj_bound =s1.cbGet(GRB.Callback.MIPSOL_OBJBND) if obj_bound is not None and obj_bound < GRB.INFINITY: feasible_solutions.append(s1.cbGetSolution(x)) # 子问题1没超时 def sp1(pi, perovertimecost, normal_mean, numpatient, patient_sequence): s1 = gp.Model("sp1") m = 5 bnewplan1 = s1.addVars(range(m), range(numpatient), vtype=GRB.BINARY, name='bnewplan1') s1._x = bnewplan1 # 设置目标函数、约束条件 sp1obj = gp.quicksum(pi[i] * bnewplan1[q, i] for q in range(m) for i in range(numpatient)) s1.setObjective(sp1obj, GRB.MAXIMIZE) s1.addConstrs(gp.quicksum(bnewplan1[q, i] * normal_mean[i] for i in range(numpatient)) + 80 - optime <= 0 for q in range(m)) feasible_solutions = [] # 初始化列表 # 设置回调函数并传递 feasible_solutions 列表作为参数 s1.setParam(GRB.Param.SolutionLimit, 1e3) s1.params.outputFlag = 0 # 关闭输出 s1.optimize(lambda s1, where: mycallback(s1, where, feasible_solutions)) s1.optimize() sp_obj = s1.objval print('子问题的最优解为', sp_obj) print('feasible_solutions', feasible_solutions) return sp_obj sp_obj = sp1(pi, perovertimecost, normal_mean, numpatient, patient_sequence) ``` 在这个示例中,我们将 `feasible_solutions` 作为参数传递给 `mycallback` 函数,并在回调函数中使用它来存储找到的可行解。这样,在每次调用 `mycallback` 函数时,`feasible_solutions` 都会保持为先前存储的值,而不会被重新初始化为空列表。 请注意,在调用 `s1.optimize()` 时,我们使用了一个匿名函数(lambda 函数)来传递 `feasible_solutions` 列表给回调函数。这样,我们可以在匿名函数中调用 `mycallback` 函数,并传递正确的参数。 这样修改后,你应该能够正确地将找到的解存储在 `feasible_solutions` 列表中并进行打印。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值