douou6696 2018-08-24 20:22
浏览 79
已采纳

“无法获得未返回的生成器的返回值”php回溯

I am trying to do backtracking to solved a problem but I get this error:

Error: genator hasn´t returned

Fisrt I will introduce the main problem that the software must resolve:

I have been trying to create the software for a hospital, this software must create a schedule with the doctors for each day. Each day must have 5 assigned doctors, one for a specialty called "trauma", another for "ucr" and another for "box13".

Before assigning doctors, the user must enter the number of holes of each type that doctor can do that month. So I know how many "trauma", "ucr" or "box13" have any doctor that month.

Aaah! one more thing, you can not assign a doctor two days in a row.

So I decided to create a backtracking algorithm, the problem is that I am getting a Generator object at the end of the execution but I don´t get a valid return.

function bt($ps){
    if($ps->is_solution()){
        return $ps->get_solution();
    }
    else{   
        yield from $ps->successors();
    }
}


class Briker_vc_PS{
        public function __construct( $decisiones, $diasTotalFinal, $fechaInicio, $box13, $ucr, $trauma, $numGuardiasAsig){            
            $this->decisiones= $decisiones;
            $this->diasTotalFinal = $diasTotalFinal;
            $this->fechaInicio = $fechaInicio;
            $this->box13 = $box13;
            $this->ucr = $ucr;
            $this->trauma = $trauma;
            $this->numGuardiasAsig = $numGuardiasAsig;
        }   

        public function is_solution(){

            return $this->numGuardiasAsig == $this->diasTotalFinal*5;                
        }

        public function get_solution(){
            return $this->decisiones;
        }

        public function state(){ 
            return $this->decisiones[$this->fechaInicio];
        }

        public function  successors(){
            if( array_key_exists( $this->fechaInicio, $this->decisiones) == false ){
                $this->decisiones[$this->fechaInicio] = [];
            }

            if( array_key_exists( 'ids', $this->decisiones[$this->fechaInicio]) == false ){
                $this->decisiones[$this->fechaInicio]['ids'] = [];
            }


            if( array_key_exists( $this->fechaInicio ,$this->decisiones) and array_key_exists( 'box13' ,$this->decisiones) and array_key_exists( 'trauma' ,$this->decisiones) and array_key_exists( 'ucr' ,$this->decisiones)){
                if( (count($this->decisiones['trauma'])==1) and (count($this->decisiones['ucr'])==2) and (count($this->decisiones['box13'])==2) ){
                    $this->fechaInicio = date("Y-m-d",strtotime($this->fechaInicio." +1 day"));
                    $this->decisiones[$this->fechaInicio]['date'] = $this->fechaInicio;
                }               
            }

            $anterior = date("Y-m-d",strtotime($this->fechaInicio." -1 day"));

            $Lista=[];

            if( array_key_exists( 'trauma' ,$this->decisiones) == false ){
                foreach($this->trauma as $key => $val){
                    if( (in_array($key, $this->decisiones[$this->fechaInicio]['ids']) == false) and (in_array($key, $this->decisiones[$anterior]['ids']) == false) ){
                        $decisions= $this->decisiones;

                        $decisions[$this->fechaInicio]['trauma'] = [$key];
                        $auxtra= $this->trauma;

                        if($auxtra[$key] -1 == 0){
                            unset($auxtra[$key]);
                        }else{
                            $auxtra[$key] = $auxtra[$key] -1;
                        }

                        $num = $this->numGuardiasAsig +1 ;

                        $decisions[$this->fechaInicio]['ids'][] = $key;

                        yield from bt(new Briker_vc_PS( $decisions, $this->diasTotalFinal, $this->fechaInicio, $this->box13, $this->ucr, $auxtra, $num));

                    }
                }
            }

            if( (array_key_exists( 'box13' ,$this->decisiones) == false) or (count($this->decisiones['box13'])<2) ){
                foreach($this->box13 as $key => $val){
                    if( (in_array($key, $this->decisiones[$this->fechaInicio]['ids']) == false) and (in_array($key, $this->decisiones[$anterior]['ids']) == false) ){
                        $decisions= $this->decisiones;

                        if(array_key_exists( 'box13' ,$this->decisiones) == false){
                            $decisions[$this->fechaInicio]['box13'] = array();
                        }

                        $decisions[$this->fechaInicio]['box13'][] = $key;
                        $auxbox13= $this->box13;

                        if($auxbox13[$key] -1 == 0){
                            unset($auxbox13[$key]);
                        }else{
                            $auxbox13[$key] = $auxbox13[$key] -1;
                        }

                        $num = $this->numGuardiasAsig +1 ;

                        $decisions[$this->fechaInicio]['ids'][] = $key;

                        yield from bt( new Briker_vc_PS( $decisions, $this->diasTotalFinal, $this->fechaInicio, $auxbox13, $this->ucr, $this->trauma, $num));

                    }
                }
            }

            if( (array_key_exists( 'ucr' ,$this->decisiones) == false) or (count($this->decisiones['ucr'])<2) ){
                foreach($this->ucr as $key => $val){
                    if( (in_array($key, $this->decisiones[$this->fechaInicio]['ids']) == false) and (in_array($key, $this->decisiones[$anterior]['ids']) == false) ){
                        $decisions= $this->decisiones;

                        if(array_key_exists( 'ucr' ,$this->decisiones) == false){
                            $decisions[$this->fechaInicio]['ucr'] = array();
                        }   

                        $decisions[$this->fechaInicio]['ucr'][] = $key;
                        $auxucr= $this->ucr;

                        if($auxucr[$key] -1 == 0){
                            unset($auxucr[$key]);
                        }else{
                            $auxucr[$key] = $auxucr[$key] -1;
                        }

                        $decisions[$this->fechaInicio]['ids'][] = $key;

                        $num = $this->numGuardiasAsig +1 ;

                        yield from bt(new Briker_vc_PS( $decisions, $this->diasTotalFinal, $this->fechaInicio, $this->box13, $auxucr, $this->trauma, $num));

                    }
                }
            }

        }

        protected $GuardiasMedico;
        protected $decisiones;

 }

And this is the main program

      $month = $_REQUEST['mes'];
        $year = $_REQUEST['any'];

        $fecha_inicio = "01-".$month."-".$year;


 // fisrt i get the number of "trauma", "ucr" and "box13" for each doctor
// And i create a array with it with key the doctor_id

        $db = new SQLite3($dbname);

        $result = $db->query('SELECT m.id, m.nombre, m.apellido, mgm.ucr, mgm.trauma, mgm.box13 FROM medico AS m INNER JOIN MedicoGuardiaMes AS mgm ON m.id = mgm.doctor_id WHERE m.borrado = 0 AND mgm.mes = '.$month.' AND mgm.any = '.$year);


        $box13 = [];
        $ucr = [];
        $trauma = [];

        while($res = $result->fetchArray()){

            $box13[$res['id']] =  $res['box13'];
            $ucr[$res['id']] =  $res['ucr'];
            $trauma[$res['id']] =  $res['trauma'];

        }

        // I create the solution array and add the last day from the previous month

        $dataBaseDecisiones = [];

        $anterior = date("Y-m-d",strtotime($fecha_inicio." -1 day"));

        $dataBaseDecisiones[$anterior] = [];
        $dataBaseDecisiones[$anterior]['ids'] = [];

        $diasTotalFinal=cal_days_in_month(CAL_GREGORIAN, $month, $year);


       // Finally I create the initial object and starts the backtracking


        $initial_ps = new Briker_vc_PS($dataBaseDecisiones, $diasTotalFinal, $fecha_inicio, $box13, $ucr, $trauma, $numGuardiasAsig);

        var_dump(  bt($initial_ps)->getReturn() );

I don´t know why this code is not working or if I am using yield the right way.

  • 写回答

1条回答 默认 最新

  • doulong4169 2018-08-24 20:59
    关注

    The problem that is reported happens because of the last line in your code:

    bt($initial_ps)->getReturn();
    

    When bt executes the else part it performs a recursive search until there is a return, and then it yields that value. But that does not finish the iterator's results and this yielded value is not considered the iterator's return value. So getReturn() triggers the exception you got.

    You don't explain what you intend to get as output in that var_dump. If you want to output all the yielded results, then collect all those results first, for instance with iterator_to_array:

    $iter = bt($initial_ps);
    print_r(iterator_to_array($iter));
    

    And only then you can execute:

    print_r($iter->getReturn());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP