简单字母T/L识别的matlab源代码和C++源代码

来源:互联网 发布:settings是什么软件 编辑:程序博客网 时间:2024/06/01 09:14

设计一个用于解决两关节机械手逆运动学问题的前馈网络,要求末端执行器的运动轨迹是一条直线(AB),具体任务如下:

(1)设计合适的网络结构;

(2)按照轨迹的要求选择21个训练样本 输入(Xi,Yi)输出(Sita1i,Sita2i)  i=1,2......21

二联杆的长度为 ,用BP算法训练网络,使之能够满足控制的要求。

 

C++源代码

//其中,导入的样本空间数据和额定输出数据在后面的txt文件中给出

   #include<iostream>
#include<cmath>
#include<fstream>
#include <windows.h>
#include<atlstr.h>
#include"random.h"  //我建立的随即数生成器头文件,它将在我的另一篇博客中给出源代码

using namespace std;

const double ratio=2;
double fun(double x)  //激活函数
{
    return ratio*x;
}

double fundao(double x) //激活函数的导数
{
    return ratio;   
}

int main()
{
    int i,j,k;
        double precision=0.01;

    double manipulator[21][2]; //样本空间,总共21个样本。
    double d[21][2];       //额定输出

    ifstream inmanipulator,ind;  //导入样本空间数据和额定输出数据
    inmanipulator.open("manipulator.txt");
    ind.open("d.txt");
    for(i=0;i<21;i++)
    {
        for(j=0;j<2;j++)
        {
            inmanipulator>>manipulator[i][j];
            ind>>d[i][j];
        }
    }

    /////
    double w2[2][5],w3[5][2];  //定义权值矩阵并初始化。系统是2-5-2的结构
    RandomInit();
    for(i=0;i<5;i++)
    {
        for(j=0;j<2;j++)
        {
            w2[j][i]=RandomReal(-0.5,0.5);
            w3[i][j]=RandomReal(-0.5,0.5);
        }
    }
    cout<<"input to mask"<<endl;  ////打印训练前的权值矩阵
    for(i=0;i<2;i++)
    {
        for(j=0;j<5;j++)
            cout<<w2[i][j]<<"   ";
        cout<<endl;
    }
    cout<<"mask to output"<<endl;
    for(i=0;i<5;i++)
    {
        for(j=0;j<2;j++)
            cout<<w3[i][j]<<"   ";
        cout<<endl;
    }

    //删除旧的保存误差的文本
    CString   str; 
    str = "J://tl2//tl2//error.txt "; 
    DeleteFile(str);  
    str = "J://tl2//tl2//E.txt "; 
    DeleteFile(str); 

    //新建并打开新的保存误差的文本,将误差e[2]和E保存在文本中。
    ofstream oute,outE;
    oute.open("error.txt");
    outE.open("E.txt");
   
    int count=0;  //训练次数统计
    while(true)  //开始训练,当误差满足精度要求或训练次数达到一定次数时,便推出训练。
    {
        double E=0,e[2]={0,0}; //e[2]是每个样本训练时的输出层误差,E是21个样本的总均方差误差
       
        oute<<count<<endl;
        for(i=0;i<21;i++)//i:npoint
        {
            double in2[5],out2[5];
            double in3[2]={0,0},out3[2]={0,0};
            for(j=0;j<5;j++)
            {
                in2[j]=0;
                out2[j]=0;
            }
           
            //forward 前向
            for(j=0;j<5;j++)
            {
                for(k=0;k<2;k++)
                    in2[j]=in2[j]+manipulator[i][k]*w2[k][j];
                out2[j]=fun(in2[j]);
            }
            for(j=0;j<2;j++)
            {
                for(k=0;k<5;k++)
                    in3[j]=in3[j]+out2[k]*w3[k][j];
                out3[j]=fun(in3[j]);
                e[j]=d[i][j]-out3[j];
                oute<<e[j]<<"   ";
                E=E+e[j]*e[j]/2/21;
            }
            oute<<endl;
           
            //back propagation 误差反向传播
            if(fabs(e[0])>=precision||fabs(e[1])>=precision)
            {
                //delt3[2]和delt2[5]是权值矩阵的修改量中的关键因子
                double delt3[2]={e[0]*fundao(in3[0]),e[1]*fundao(in3[1])};
                double delt2[5];

                for(j=0;j<5;j++) 
                {
                    double sigma=0;
                    for(k=0;k<2;k++)
                        sigma=sigma+delt3[k]*w3[j][k];

                    delt2[j]=fundao(in2[j])*sigma;
                }

                double yita=0.05;  //步长
                for(j=0;j<5;j++)   //修改权值矩阵
                {
                    for(k=0;k<2;k++)
                        w3[j][k]=w3[j][k]+yita*delt3[k]*out2[j];
                }
                for(k=0;k<2;k++)
                {
                    for(j=0;j<5;j++)
                        w2[k][j]=w2[k][j]+yita*delt2[j]*manipulator[i][k];
                }
            }
        }//for(i=0;i<21;i++)//i:npoint


        outE<<count<<"   "<<E<<endl;
        if(E<precision)
            break;
        if(count>1000)
            break;
        count++;
    }//end "while"
    cout<<endl<<"对样本总体共训练了 "<<count<<" 次。"<<endl;
    oute.close();
    outE.close();

    cout<<"input to mask"<<endl;  //打印训练完毕的权值矩阵
    for(i=0;i<2;i++)
    {
        for(j=0;j<5;j++)
            cout<<w2[i][j]<<"   ";
        cout<<endl;
    }
    cout<<"mask to output"<<endl;
    for(i=0;i<5;i++)
    {
        for(j=0;j<2;j++)
            cout<<w3[i][j]<<"   ";
        cout<<endl;
    }

    return 0;
}

manipulator.txt

    0.7071    0.7071
    0.7218    0.6922
    0.7364    0.6766
    0.7510    0.6603
    0.7657    0.6432
    0.7803    0.6254
    0.7950    0.6066
    0.8096    0.5870
    0.8243    0.5662
    0.8389    0.5443
    0.8536    0.5210
    0.8682    0.4962
    0.8828    0.4697
    0.8975    0.4410
    0.9121    0.4099
    0.9268    0.3756
    0.9414    0.3372
    0.9561    0.2932
    0.9707    0.2403
    0.9854    0.1705
    1.0000         0

d.txt


    0.7854    2.0589
    0.7645    2.0642
    0.7431    2.0692
    0.7212    2.0738
    0.6987    2.0781
    0.6756    2.0820
    0.6518    2.0855
    0.6273    2.0885
    0.6019    2.0910
    0.5755    2.0929
    0.5480    2.0941
    0.5192    2.0944
    0.4889    2.0937
    0.4568    2.0917
    0.4223    2.0881
    0.3851    2.0822
    0.3440    2.0731
    0.2975    2.0590
    0.2426    2.0360
    0.1714    1.9921
         0    1.5708

Matlab源代码

clear all;
k=20;
m=( 1.0-sqrt(0.5) )/k;
n=k+1;
x=sqrt(0.5):m:1.0;
y=zeros(1,n);
for i=1:n
    y(i)=sqrt(1-x(i)*x(i));
end

 x=reshape(x,n,1);
 y=reshape(y,n,1);
 
 sita1=zeros(n,1);
 sita2=zeros(n,1);
 sita3=zeros(n,1);
 
 for i=1:n
    sita1(i)=asin(y(i));
    sita3(i)=asin(1-y(i));
    sita2(i)=pi-sita1(i)-sita3(i);
 end
 
% P=zeros(n,2);
P=horzcat(x(:,1),y(:,1));
P=P';

% T=zeros(n,2);
T=horzcat(sita1(:,1),sita2(:,1));
T=T';

% net=newff();
net=newff(minmax(P),[3,4,2],{'purelin','purelin','purelin'},'traingd'); %%网络采用2-3-4-2的结构

net.trainParam.epochs=15000;
net.trainParam.goal=0.01;
LP.lr=0.05; %yita=0.1

[net,tr]=train(net,P,T);

[m,n]=size(tr.perf);
y=tr.perf;
x=1:n;
plot(x,y,'r'), grid on
title('T&L Recognization');
xlabel('X: Training Counter'); 
ylabel('Y: Error');

inputweight=net.iw{1,1}
layerweight1=net.lw{2,1}
layerweight2=net.lw{3,2}

 

原创粉丝点击