⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cqtcontroller.cpp

📁 强化学习算法(R-Learning)难得的珍贵资料
💻 CPP
字号:
// Copyright (C) 2003
// Gerhard Neumann (gerhard@igi.tu-graz.ac.at)

//                
// This file is part of RL Toolbox.
// http://www.igi.tugraz.at/ril_toolbox
//
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
//    derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "cqtcontroller.h"

#ifdef RL_TOOLBOX_USE_QT

#include <qgrid.h>
#include <qapplication.h>
#include <qpushbutton.h>
#include <qcheckbox.h>
#include <qfont.h>
#include <qlayout.h>
#include <qlabel.h>
#include <math.h>
#include <qgroupbox.h>
#include <qlistbox.h>

CQTContinuousActionController::CQTContinuousActionController(CContinuousAction *contAction, int *lkeys, QWidget *parent, const char *name) : QWidget(parent, name), CContinuousActionController(contAction)
{
	buttonMap = new std::map<int, QButton *>(); 
	downList = new std::list<QButton *>();
	upList = new std::list<QButton *>();

	buttonMutex = new QMutex();

	int *keys = lkeys;
	int standardKeys[10] = {Key_Left, Key_Right, Key_Down, Key_Up, Key_A, Key_D, Key_S, Key_W, Key_Y, Key_X};
	
	if (lkeys == NULL)
	{
		keys = standardKeys;
	}

	this->standardValues = new rlt_real[contAction->getNumDimensions()];

	for (int i = 0; i < contAction->getNumDimensions(); i ++)
	{
		QGroupBox *GroupBox = new QGroupBox( this, NULL );
		GroupBox->setFixedSize(180, 100); 
		QFont GroupBox_font(  GroupBox->font() );
		GroupBox_font.setBold( TRUE );
		GroupBox->setFont( GroupBox_font ); 
		GroupBox->setTitle( tr( "Action Control " + QString::number(i)));

		QPushButton *left = new QPushButton( GroupBox, "left");
		left->setGeometry( QRect( 20, 30, 60, 50 ) ); 
		QFont left_font(  left->font() );
		left_font.setPointSize( 20 );
		left->setFont( left_font ); 
		left->setFocusPolicy( QPushButton::NoFocus );
		left->setText( tr( "<" ) );

		QPushButton *right = new QPushButton( GroupBox, "right" );
		right->setGeometry( QRect( 100, 30, 60, 50 ) ); 
		QFont right_font(  right->font() );
		right_font.setPointSize( 20 );
		right->setFont( right_font ); 
		right->setFocusPolicy( QPushButton::NoFocus );
		right->setText( tr( ">" ) );

		downList->push_back(left);
		upList->push_back(right);

		(*buttonMap)[keys[2 * i]] = left;
		(*buttonMap)[keys[2 * i + 1]] = right;


		if (contAction->getContinuousActionProperties()->getMinActionValue(i) > 0)
		{
			standardValues[i] = contAction->getContinuousActionProperties()->getMinActionValue(i);
		}
		else
		{
			standardValues[i] = 0.0;
		}
	}
	this->adjustSize();

}

CQTContinuousActionController::~CQTContinuousActionController()
{
	delete buttonMap;
	delete downList;
	delete upList;
	delete [] standardValues;
	delete actions;
	delete buttonMutex;
}

const QSizePolicy CQTContinuousActionController::sizePolicy()
{
	return QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
}


void CQTContinuousActionController::getNextContinuousAction(CStateCollection *state, CContinuousActionData *actionData)
{
	std::list<QButton *>::iterator it1, it2;
	unsigned int i = 0 ;

	buttonMutex->lock();
	for (i = 0, it1 = downList->begin(),it2 = upList->begin(); i < getContinuousActionProperties()->getNumActionValues(); i ++, it1 ++,it2 ++)
	{
		if  (strcmp((*it1)->name(), "left") != 0 || strcmp((*it2)->name(), "right") != 0)
		{
			qDebug ("Button: %s, %s\n", (*it1)->name(), (*it2)->name());
		}
		if ((*it1)->isDown() && !(*it2)->isDown())
		{
			actionData->setActionValue(i, getContinuousActionProperties()->getMinActionValue(i));
		}
		else
		{
			if (!(*it1)->isDown() && (*it2)->isDown())
			{
				actionData->setActionValue(i, getContinuousActionProperties()->getMaxActionValue(i));
			}
			else
			{
				actionData->setActionValue(i, standardValues[i]);
			}
		}
	}
	

	buttonMutex->unlock();
}


void CQTContinuousActionController::keyPressEvent(QKeyEvent *e)
{
	if ((*buttonMap)[e->key()] != NULL)
	{
		buttonMutex->lock();
		int key = e->key();
		QButton *button = (*buttonMap)[key];
		button->setDown(true);
		buttonMutex->unlock();
	}
}

void CQTContinuousActionController::keyReleaseEvent(QKeyEvent *e)
{
	if ((*buttonMap)[e->key()] != NULL)
	{
		buttonMutex->lock();		
		int key = e->key();
		QButton *button = (*buttonMap)[key];
		button->setDown(false);

		buttonMutex->unlock();
	}
}

void CQTContinuousActionController::setStandardActionValue(int index, rlt_real value)
{
	standardValues[index] = value;
}

rlt_real CQTContinuousActionController::getStandardActionValue(int index)
{
	return standardValues[index];
}

CQTController::CQTController(CActionSet *actions, CAction *standardAction, int *lkeys, QWidget *parent, const char *name) : QWidget(parent, name),CAgentController(actions)
{
	buttonMap = new std::map<int, QButton *>(); 
	buttons = new std::list<QButton *>();

	int *keys = lkeys;
	int standardKeys[10] = {Key_1, Key_2, Key_3, Key_4, Key_5, Key_6, Key_7, Key_8, Key_9, Key_0};

	if (lkeys == NULL)
	{
		keys = standardKeys;
	}

	this->standardAction = standardAction;

	QBoxLayout *box = new QHBoxLayout( this );


	for (unsigned int i = 0; i < actions->size(); i ++)
	{
		QPushButton *button = new QPushButton( this, NULL );
		button->setGeometry( QRect( 20, 30, 60, 50 ) ); 
		QFont button_font(  button->font() );
		button_font.setPointSize( 20 );
		button->setFont( button_font ); 
		button->setFocusPolicy( QPushButton::NoFocus );
		button->setText( tr( "Action " + QString::number(i)));

		box->addWidget(button);

		buttons->push_back(button);

		(*buttonMap)[keys[i]] = button;
	}
}

CQTController::~CQTController()
{
	delete buttonMap;
	delete buttons;
}

CAction* CQTController::getNextAction(CStateCollection *state, CActionDataSet *dataSet)
{
	std::list<QButton *>::iterator it;
	CActionSet::iterator itActions;

	CAction *action = standardAction;

	for (it = buttons->begin(),itActions = actions->begin(); itActions != actions->begin(); it ++, itActions ++)
	{
		if ((*it)->isDown())
		{
			action = *itActions;
			break;
		}
	}
	return action; 
}

void CQTController::keyPressEvent(QKeyEvent *e)
{
	if ((*buttonMap)[e->key()] != NULL)
	{
		(*buttonMap)[e->key()]->setDown(true);
	}
}

void CQTController::keyReleaseEvent(QKeyEvent *e)
{
	if ((*buttonMap)[e->key()] != NULL)
	{
		(*buttonMap)[e->key()]->setDown(false);
	}
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -