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 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败