Produce different errors when I run the same code

Hi:
I meet a weird error!
When I run my same code time after time, it produce different errors below

File "/home/vcdc/flow/flow/envs/merge.py", line 333, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 803, in __init__
    ocp_render_templates(acados_ocp, json_file)
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 634, in ocp_render_templates
    render_template(in_file, out_file, template_dir, json_path)
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/utils.py", line 216, in render_template
    raise Exception('Rendering of {} failed! Exiting.\n'.format(in_file))
Exception: Rendering of main.in.c failed! Exiting.
  File "/home/vcdc/flow/flow/envs/merge.py", line 333, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 803, in __init__
    ocp_render_templates(acados_ocp, json_file)
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 646, in ocp_render_templates
    render_template(in_file, out_file, template_dir, json_path)
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/utils.py", line 216, in render_template
    raise Exception('Rendering of {} failed! Exiting.\n'.format(in_file))
Exception: Rendering of Makefile.in failed! Exiting.
File "/home/vcdc/flow/flow/envs/merge.py", line 333, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 816, in __init__
    self.shared_lib = CDLL(self.shared_lib_name)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/ctypes/__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: c_generated_code/libacados_ocp_solver_casadi_model.so: file too short
File "/home/vcdc/flow/flow/envs/merge.py", line 333, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 816, in __init__
    self.shared_lib = CDLL(self.shared_lib_name)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/ctypes/__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: c_generated_code/libacados_ocp_solver_casadi_model.so: cannot open shared object file: No such file or directory
 File "/home/vcdc/flow/flow/envs/merge.py", line 333, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 803, in __init__
    ocp_render_templates(acados_ocp, json_file)
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 646, in ocp_render_templates
    render_template(in_file, out_file, template_dir, json_path)
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/utils.py", line 216, in render_template
    raise Exception('Rendering of {} failed! Exiting.\n'.format(in_file))
Exception: Rendering of Makefile.in failed! Exiting.

It is very weird~
Anyone knows why it happen?
(By the way,I can run the example minimal_example_ocp.py, so my environment variables should be no problem)

Hi,

can you provide the code or a minimal example how you generate these errors?
Without that, it is really hard to help…

Cheers!

Hi,
thank you for your reply,
these are my all Complete different errors when I run the same code

2021-09-12 10:21:47,304	ERROR trial_runner.py:482 -- Error processing event.
Traceback (most recent call last):
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/tune/trial_runner.py", line 426, in _process_trial
    result = self.trial_executor.fetch_result(trial)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/tune/ray_trial_executor.py", line 378, in fetch_result
    result = ray.get(trial_future[0], DEFAULT_GET_TIMEOUT)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/worker.py", line 1457, in get
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(OSError): ray::PPO.train() (pid=6254, ip=192.168.6.112)
  File "python/ray/_raylet.pyx", line 636, in ray._raylet.execute_task
  File "python/ray/_raylet.pyx", line 619, in ray._raylet.execute_task.function_executor
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/agents/trainer.py", line 444, in train
    raise e
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/agents/trainer.py", line 433, in train
    result = Trainable.train(self)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/tune/trainable.py", line 176, in train
    result = self._train()
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/agents/trainer_template.py", line 129, in _train
    fetches = self.optimizer.step()
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/optimizers/multi_gpu_optimizer.py", line 140, in step
    self.num_envs_per_worker, self.train_batch_size)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/optimizers/rollout.py", line 29, in collect_samples
    next_sample = ray_get_and_free(fut_sample)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/utils/memory.py", line 33, in ray_get_and_free
    result = ray.get(object_ids)
ray.exceptions.RayTaskError(OSError): ray::RolloutWorker.sample() (pid=6330, ip=192.168.6.112)
  File "python/ray/_raylet.pyx", line 636, in ray._raylet.execute_task
  File "python/ray/_raylet.pyx", line 619, in ray._raylet.execute_task.function_executor
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/rollout_worker.py", line 471, in sample
    batches = [self.input_reader.next()]
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 56, in next
    batches = [self.get_data()]
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 99, in get_data
    item = next(self.rollout_provider)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 340, in _env_runner
    base_env.send_actions(actions_to_send)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/env/base_env.py", line 332, in send_actions
    self.vector_env.vector_step(action_vector)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/env/vector_env.py", line 110, in vector_step
    obs, r, done, info = self.envs[i].step(actions[i])
  File "/home/vcdc/flow/flow/envs/base.py", line 364, in step
    self.apply_rl_actions(rl_actions)
  File "/home/vcdc/flow/flow/envs/base.py", line 616, in apply_rl_actions
    self._apply_rl_actions(rl_clipped)
  File "/home/vcdc/flow/flow/envs/merge.py", line 368, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 816, in __init__
    self.shared_lib = CDLL(self.shared_lib_name)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/ctypes/__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: c_generated_code/libacados_ocp_solver_casadi_model.so: file too short

2021-09-12 10:21:57,588	ERROR trial_runner.py:482 -- Error processing event.
Traceback (most recent call last):
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/tune/trial_runner.py", line 426, in _process_trial
    result = self.trial_executor.fetch_result(trial)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/tune/ray_trial_executor.py", line 378, in fetch_result
    result = ray.get(trial_future[0], DEFAULT_GET_TIMEOUT)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/worker.py", line 1457, in get
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(OSError): ray::PPO.train() (pid=6395, ip=192.168.6.112)
  File "python/ray/_raylet.pyx", line 636, in ray._raylet.execute_task
  File "python/ray/_raylet.pyx", line 619, in ray._raylet.execute_task.function_executor
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/agents/trainer.py", line 444, in train
    raise e
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/agents/trainer.py", line 433, in train
    result = Trainable.train(self)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/tune/trainable.py", line 176, in train
    result = self._train()
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/agents/trainer_template.py", line 129, in _train
    fetches = self.optimizer.step()
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/optimizers/multi_gpu_optimizer.py", line 140, in step
    self.num_envs_per_worker, self.train_batch_size)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/optimizers/rollout.py", line 29, in collect_samples
    next_sample = ray_get_and_free(fut_sample)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/utils/memory.py", line 33, in ray_get_and_free
    result = ray.get(object_ids)
ray.exceptions.RayTaskError(OSError): ray::RolloutWorker.sample() (pid=6394, ip=192.168.6.112)
  File "python/ray/_raylet.pyx", line 636, in ray._raylet.execute_task
  File "python/ray/_raylet.pyx", line 619, in ray._raylet.execute_task.function_executor
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/rollout_worker.py", line 471, in sample
    batches = [self.input_reader.next()]
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 56, in next
    batches = [self.get_data()]
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 99, in get_data
    item = next(self.rollout_provider)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 340, in _env_runner
    base_env.send_actions(actions_to_send)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/env/base_env.py", line 332, in send_actions
    self.vector_env.vector_step(action_vector)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/env/vector_env.py", line 110, in vector_step
    obs, r, done, info = self.envs[i].step(actions[i])
  File "/home/vcdc/flow/flow/envs/base.py", line 364, in step
    self.apply_rl_actions(rl_actions)
  File "/home/vcdc/flow/flow/envs/base.py", line 616, in apply_rl_actions
    self._apply_rl_actions(rl_clipped)
  File "/home/vcdc/flow/flow/envs/merge.py", line 368, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 816, in __init__
    self.shared_lib = CDLL(self.shared_lib_name)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/ctypes/__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: c_generated_code/libacados_ocp_solver_casadi_model.so: cannot open shared object file: No such file or directory
2021-09-12 10:22:15,900	ERROR worker.py:994 -- Possible unhandled error from worker: ray::RolloutWorker.sample() (pid=6329, ip=192.168.6.112)
  File "python/ray/_raylet.pyx", line 636, in ray._raylet.execute_task
  File "python/ray/_raylet.pyx", line 619, in ray._raylet.execute_task.function_executor
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/rollout_worker.py", line 471, in sample
    batches = [self.input_reader.next()]
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 56, in next
    batches = [self.get_data()]
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 99, in get_data
    item = next(self.rollout_provider)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 340, in _env_runner
    base_env.send_actions(actions_to_send)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/env/base_env.py", line 332, in send_actions
    self.vector_env.vector_step(action_vector)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/env/vector_env.py", line 110, in vector_step
    obs, r, done, info = self.envs[i].step(actions[i])
  File "/home/vcdc/flow/flow/envs/base.py", line 364, in step
    self.apply_rl_actions(rl_actions)
  File "/home/vcdc/flow/flow/envs/base.py", line 616, in apply_rl_actions
    self._apply_rl_actions(rl_clipped)
  File "/home/vcdc/flow/flow/envs/merge.py", line 368, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 816, in __init__
    self.shared_lib = CDLL(self.shared_lib_name)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/ctypes/__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: c_generated_code/libacados_ocp_solver_casadi_model.so: file too short
2021-09-12 10:22:22,419	ERROR worker.py:994 -- Possible unhandled error from worker: ray::RolloutWorker.sample() (pid=8004, ip=192.168.6.112)
  File "python/ray/_raylet.pyx", line 636, in ray._raylet.execute_task
  File "python/ray/_raylet.pyx", line 619, in ray._raylet.execute_task.function_executor
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/rollout_worker.py", line 471, in sample
    batches = [self.input_reader.next()]
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 56, in next
    batches = [self.get_data()]
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 99, in get_data
    item = next(self.rollout_provider)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/evaluation/sampler.py", line 340, in _env_runner
    base_env.send_actions(actions_to_send)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/env/base_env.py", line 332, in send_actions
    self.vector_env.vector_step(action_vector)
  File "/home/vcdc/anaconda3/envs/flow/lib/python3.7/site-packages/ray/rllib/env/vector_env.py", line 110, in vector_step
    obs, r, done, info = self.envs[i].step(actions[i])
  File "/home/vcdc/flow/flow/envs/base.py", line 364, in step
    self.apply_rl_actions(rl_actions)
  File "/home/vcdc/flow/flow/envs/base.py", line 616, in apply_rl_actions
    self._apply_rl_actions(rl_clipped)
  File "/home/vcdc/flow/flow/envs/merge.py", line 368, in _apply_rl_actions
    ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 803, in __init__
    ocp_render_templates(acados_ocp, json_file)
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/acados_ocp_solver.py", line 646, in ocp_render_templates
    render_template(in_file, out_file, template_dir, json_path)
  File "/home/vcdc/acados/interfaces/acados_template/acados_template/utils.py", line 216, in render_template
    raise Exception('Rendering of {} failed! Exiting.\n'.format(in_file))
Exception: Rendering of Makefile.in failed! Exiting.

I am really grateful for your help.

These error messages don’t give me more insight.
Again:

Hi,this is my interrelated code,but I really do not know how these errors produce from my “Traceback”.

            def casadi_modle():
                model = AcadosModel()
                x_h = ca.SX.sym('x_h')
                v_h = ca.SX.sym('v_h')
                states_ego = ca.vertcat(x_h, v_h)
                a_h = ca.SX.sym('a_h')
                controls = ca.vertcat(a_h)
                rhs = ca.vertcat(v_h, a_h)
                f = ca.Function('f', [states_ego, controls], [rhs], ['input_state', 'control_input'], ['rhs'])
                x_dot = ca.SX.sym('x_dot', 2)
                f_impl = x_dot - f(states_ego, controls)

                model.f_expl_expr = f(states_ego, controls)
                model.f_impl_expr = f_impl
                model.x = states_ego
                model.xdot = x_dot
                model.u = controls
                model.p = []
                model.name = 'casadi_model'


                return model



            # if __name__ == '__main__':
            tf = 10
            T = 1  # sampling time [s]
            N = 10  # prediction horizon
            v_max = 100
            acc_max = 3
            x_h = ca.SX.sym('x_h')
            v_h = ca.SX.sym('v_h')
            states_ego = ca.vertcat(x_h, v_h)
            n_states = states_ego.size()[0]
            a = ca.SX.sym('a')
            controls = ca.vertcat(a)
            n_controls = controls.size()[0]
            rhs = ca.vertcat(v_h, a)
            f = ca.Function('f', [states_ego, controls], [rhs], ['input_state', 'control_input'], ['rhs'])
            # 预测四车道前方车辆的行驶路径
            x_p_0 = p0_leader
            v_p_0 = leader0_1speed
            states_leader_0 = np.array([x_p_0, v_p_0]).reshape(-1, 1)
            a_p_0 = leader0_1_acc
            v_p_0_pre = []
            x_p_0_pre = []
            for i in range(N+1):
                a_p_0_ = a_p_0 * math.exp(-i * T)
                f_value0 = f(states_leader_0, a_p_0_)
                st0 = states_leader_0 + T * f_value0
                v_p_0_pre.append(st0[1])
                x_p_0_pre.append(st0[0])


            x_p_1 = p1_leader
            v_p_1 = leader1_1speed
            states_leader_1 = np.array([x_p_1, v_p_1]).reshape(-1, 1)
            a_p_1 = leader1_1_acc
            v_p_1_pre = []
            x_p_1_pre = []
            for i in range(N+1):
                a_p_1_ = a_p_1 * math.exp(-i * T)
                f_value1 = f(states_leader_1, a_p_1_)
                st1 = states_leader_1 + T * f_value1
                v_p_1_pre.append(st1[1])
                x_p_1_pre.append(st1[0])

            x_p_2 = p2_leader
            v_p_2 = leader2_1speed
            states_leader_2 = np.array([x_p_2, v_p_2]).reshape(-1, 1)
            a_p_2 = leader2_1_acc
            v_p_2_pre = []
            x_p_2_pre = []
            for i in range(N+1):
                a_p_2_ = a_p_2 * math.exp(-i * T)
                f_value2 = f(states_leader_2, a_p_2_)
                st2 = states_leader_2 + T * f_value2
                v_p_2_pre.append(st2[1])
                x_p_2_pre.append(st2[0])

            x_p_3 = p3_leader
            v_p_3 = leader3_1speed
            states_leader_3 = np.array([x_p_3, v_p_3]).reshape(-1, 1)
            a_p_3 = leader3_1_acc
            v_p_3_pre = []
            x_p_3_pre = []
            for i in range(N+1):
                a_p_3_ = a_p_3 * math.exp(-i * T)
                f_value3 = f(states_leader_3, a_p_3_)
                st3 = states_leader_3 + T * f_value3
                v_p_3_pre.append(st3[1])
                x_p_3_pre.append(st3[0])

            # # Ensure current working directory is current folder
            # os.chdir(os.path.dirname(os.path.realpath(__file__)))
            # self.acados_models_dir = './acados_models'
            # safe_mkdir_recursive(os.path.join(os.getcwd(), self.acados_models_dir))
            acados_source_path = os.environ['ACADOS_SOURCE_DIR']
            sys.path.insert(0, acados_source_path)

            model = casadi_modle()
            ocp = AcadosOcp()
            ocp.acados_include_path = acados_source_path + '/include'
            ocp.acados_lib_path = acados_source_path + '/lib'
            ocp.model = model
            ocp.dims.N = N
            ocp.solver_options.tf = tf
            x = ocp.model.x
            u = ocp.model.u
            nx = model.x.size()[0]
            nu = model.u.size()[0]

            ocp.cost.cost_type = 'NONLINEAR_LS'
            ocp.cost.cost_type_e = 'NONLINEAR_LS'
            vx = np.array([[1,2],
                          [0,-1],
                          [0,-55.312]])
            vx_ = np.array([[0,0],[0,0],[0,1.67]])
            vu = np.array([[0],[0],[1281]])
            vz = np.array([[0.5],[17],[840.4]])
            y = vx@x + vx_ @ (x * x) + vu @u + vz
            y_e = vx@x + vx_ @ (x * x) + vz
            Q = np.diag([1,2,3])

            ocp.model.cost_y_expr = y
            ocp.model.cost_y_expr_e = y_e
            ocp.cost.yref = np.zeros(nx+nu)
            ocp.cost.yref_e = np.zeros(nx+nu)
            ocp.cost.W = Q
            ocp.cost.W_e = Q

            # set constraints
            ocp.constraints.lbu = np.array([-5])
            ocp.constraints.ubu = np.array([4])
            ocp.constraints.lbx = np.array([0])
            ocp.constraints.ubx = np.array([30])
            ocp.constraints.idxbu = np.array([0])
            ocp.constraints.idxbx = np.array([1])
            ocp.constraints.x0 = np.array([p_ego, v_ego])

            # set options
            ocp.solver_options.qp_solver = 'PARTIAL_CONDENSING_HPIPM'
            ocp.solver_options.hessian_approx = 'GAUSS_NEWTON'
            # explicit Runge-Kutta integrator
            ocp.solver_options.integrator_type = 'ERK'
            ocp.solver_options.print_level = 0
            ocp.solver_options.nlp_solver_type = 'SQP_RTI'

            ocp_solver = AcadosOcpSolver(ocp, json_file='acados_ocp.json')
            #0车道
            for i in range(N):
                ocp_solver.set(i, 'yref', np.array([[x_p_0_pre[i]],[0.],[0.]]))
            ocp_solver.set(N, 'yref', np.array([[x_p_0_pre[N]],[0.],[0.]]))
            x_current = np.array([p_ego, v_ego])
            cost0 = []
            for i in range(N):
                ocp_solver.set(0, 'lbx', x_current)
                ocp_solver.set(0, 'ubx', x_current)
                # ocp_solver.set(0, 'ubx', x_current)
                status = ocp_solver.solve()
                if status != 0:
                    # ocp_solver.print_statistics()  # encapsulates: stat = ocp_solver.get_stats("statistics")
                    raise Exception('acados returned status {}. Exiting.'.format(status))
                x_current = ocp_solver.get(1, 'x')
                cost0.append(ocp_solver.get_cost())
            cost0 = sum(cost0)

            #1车道,以此类推
            for i in range(N):
                ocp_solver.set(i, 'yref', np.array([[x_p_1_pre[i]],[0.],[0.]]))
            ocp_solver.set(N, 'yref', np.array([[x_p_1_pre[N]], [0.], [0.]]))
            x_current = np.array([p_ego, v_ego])
            cost1 = []
            for i in range(N):
                ocp_solver.set(0, 'lbx', x_current)
                ocp_solver.set(0, 'ubx', x_current)
                # ocp_solver.set(0, 'ubx', x_current)
                status = ocp_solver.solve()
                if status != 0:
                    # ocp_solver.print_statistics()  # encapsulates: stat = ocp_solver.get_stats("statistics")
                    raise Exception('acados returned status {}. Exiting.'.format(status))
                x_current = ocp_solver.get(1, 'x')
                cost1.append(ocp_solver.get_cost())
            cost1 = sum(cost1)

            for i in range(N):
                ocp_solver.set(i, 'yref', np.array([[x_p_2_pre[i]],[0.],[0.]]))
            ocp_solver.set(N, 'yref', np.array([[x_p_2_pre[N]], [0.], [0.]]))
            x_current = np.array([p_ego, v_ego])
            cost2 = []
            for i in range(N):
                ocp_solver.set(0, 'lbx', x_current)
                ocp_solver.set(0, 'ubx', x_current)
                # ocp_solver.set(0, 'ubx', x_current)
                status = ocp_solver.solve()
                if status != 0:
                    # ocp_solver.print_statistics()  # encapsulates: stat = ocp_solver.get_stats("statistics")
                    raise Exception('acados returned status {}. Exiting.'.format(status))
                x_current = ocp_solver.get(1, 'x')
                cost2.append(ocp_solver.get_cost())
            cost2 = sum(cost2)

            for i in range(N):
                ocp_solver.set(i, 'yref', np.array([[x_p_3_pre[i]],[0.],[0.]]))
            ocp_solver.set(N, 'yref', np.array([[x_p_3_pre[N]], [0.], [0.]]))
            x_current = np.array([p_ego, v_ego])
            cost3 = []
            for i in range(N):
                ocp_solver.set(0, 'lbx', x_current)
                ocp_solver.set(0, 'ubx', x_current)
                # ocp_solver.set(0, 'ubx', x_current)
                status = ocp_solver.solve()
                if status != 0:
                    # ocp_solver.print_statistics()  # encapsulates: stat = ocp_solver.get_stats("statistics")
                    raise Exception('acados returned status {}. Exiting.'.format(status))
                x_current = ocp_solver.get(1, 'x')
                cost3.append(ocp_solver.get_cost())
            cost3 = sum(cost3)


            #进行换道
            cost = [cost0, cost1, cost2, cost3]
            print(cost)
            lane_index = self.k.vehicle.get_lane(rl_ids)
            # print('yunxing daoci')
            kp = 0.15  # 为了显示出換道的明显区别,加一个扩大因子
            if lane_index == 0:
                if cost[0] > cost[1] * (1 + kp):
                    self.k.vehicle.apply_lane_change(rl_ids, 1)
            elif lane_index == 3:
                if cost[3] > cost[2] * (1 + kp):
                    self.k.vehicle.apply_lane_change(rl_ids, -1)
            else:
                if cost[lane_index] > cost[lane_index + 1] * (1 + kp) and cost[lane_index - 1] > cost[
                    lane_index + 1]:
                    self.k.vehicle.apply_lane_change(rl_ids, 1)
                elif cost[lane_index] > cost[lane_index - 1] * (1 + kp) and cost[lane_index + 1] > cost[
                    lane_index - 1]:
                    self.k.vehicle.apply_lane_change(rl_ids, -1)