DE算法是遗传算法中一种比较流行的算法,这种算法比较简单,速度也比较快,下面给出一份示例代码
clear all; close all; clc 2 %Function to be minimized 3 D=2; 4 objf=inline(’4*x1^2é2.1*x1^4+(x1^6)/3+x1*x2é4*x2^2+4*x2^4’,’x1’,’x2’); 5 objf=vectorize(objf); 6 %Initialization of DE parameters 7 N=20; %population size (total function evaluations will be itmax*N, must be >=5) 8 itmax=30; 9 F=0.8; CR=0.5; %mutation and crossover ratio 10 %Problem bounds 11 a(1:N,1)=é1.9; b(1:N,1)=1.9; %bounds on variable x1 12 a(1:N,2)=é1.1; b(1:N,2)=1.1; %bounds on variable x2 13 d=(béa); 14 basemat=repmat(int16(linspace(1,N,N)),N,1); %used later 15 basej=repmat(int16(linspace(1,D,D)),N,1); %used later 16 %Random initialization of positions 17 x=a+d.*rand(N,D); 18 %Evaluate objective for all particles 19 fx=objf(x(:,1),x(:,2)); 20 %Find best 21 [fxbest,ixbest]=min(fx); 22 xbest=x(ixbest,1:D); 23 %Iterate 24 for it=1:itmax; 25 permat=bsxfun(@(x,y) x(randperm(y(1))),basemat’,N(ones(N,1)))’; 26 %Generate donors by mutation 27 v(1:N,1:D)=repmat(xbest,N,1)+F*(x(permat(1:N,1),1:D)éx(permat(1:N,2),1: D)); 28 %Perform recombination 29 r=repmat(randi([1 D],N,1),1,D); 30 muv = ((rand(N,D)<CR) + (basej==r)) ~= 0; 31 mux = 1émuv; 32 u(1:N,1:D)=x(1:N,1:D).*mux(1:N,1:D)+v(1:N,1:D).*muv(1:N,1:D); 33 %Greedy selection 34 fu=objf(u(:,1),u(:,2)); 35 idx=fu<fx; 36 fx(idx)=fu(idx); 37 x(idx,1:D)=u(idx,1:D); 38 %Find best 39 [fxbest,ixbest]=min(fx); 40 xbest=x(ixbest,1:D); 41 end %end loop on iterations 42 [xbest,fxbest]
github是一种开源的版本控制工具,现在已经得到很多人的应用。所以想介绍一下github的一些使用。
github安装 github提供了桌面客户端,我们也可以通过命令行的方式来进行控制。 windows https://windows.github.com mac https://mac.github.com
配置工具 对于本地版本配置用户信息
git config --global user.name "username" git config --global user.email "email" 上面的分别是设置用户名和邮箱
建立版本库 git init project-name //create a new local repost with the specified name git clone url //download a project and its entire version history 提交变化版本 git status // list all new of modified files to be committed git diff //show file differences not yet staged git add file //snapshot the file in preparation for versioning git diff --staged //show file difference between staging and the last file version git reset file //unstage the file, but preserve its contents git commit -m "description message" 群组版本控制 git branch //list all local branches in the current respority git branch branch-name //create a new branch git checkout branch-name //switch to the specific branch and update the working directory git merge branch //combine the specified branch's history into the current branch git branch -d branch-name //delete the specified branch 重构文件名 git rm [file] //delete the file from the working directory and stage the deletion git rm --cached [file] //remove the file from version control but pressure the file locally git mv [file-origin] [file-renamed] //change the file name and prepare it for commit 排除版本控制 *.log build/ temp-* 以.log为结尾的文件都不会被进行版本控制
在javascript里面看到javascript的继承模式和传统的继承模式是有区别的,就想查资料看一下到底有区别,就看到了这篇文章,觉得讲得还可以,暂时先放上来,以后有别的东西再补充: http://segmentfault.com/a/1190000000766541
基本模式 var Parent = function(){ this.name = 'parent'; }; Parent.prototype.getName = function(){ return this.name; }; Parent.prototype.obj = {a:1}; var Child = function(){ this.name = 'child'; } Child.protytype = new Parent(); var parent = new Parent(); var child = new Child(); console.log(parent.getName());//parent console.log(child.getName());//child 这种事最简单实现原型继承的方法,直接把父类的对象复制给子类的构造函数的原型,这样子类的对象就可以访问到父类以及父类构造函数的protytype 这种方法的优点就是实现起来比较简单,不需要任何特殊的操作;同时他的缺点也很明显,如果子类需要做跟父类构造函数中相同的初始化动作,那么就得在子类构造函数中再重复一遍父类中的操作:
var Parent = function(name){ this.name = name || 'parent'; }; Parent.prototype.getName = function(){ return this.name; }; Parent.prototype.obj = {a:1}; var Child = function(name) { this.name = name || 'child'; }; Child.prototype = new Parent(); var parent = new Parent('myParent'); var child = new Child('myChild'); console.log(parent.getName());//myParent console.log(child.getName());//myChild 上述还只是初始化name属性,如果初始化工作不断增加,这种方式也不是很方便。
css里面的盒子模型里面设置padding,margin的上下或者左右的大小有很多方式,下面说说两种不同的方式: original method:
padding-top:0px padding-right:20px padding-bottom:30px padding-left:10px new method:
padding:0px 20px 30px 10px // top right bottom left respectively 同理:
margin-top:0px margin-right:20px margin-bottom:30px margin-left:10px margin:0px 20px 30px 10px 如果上下左右的值都是一样的话,那我们可以这样设置: the old method:
padding-top:20px padding-right:20px padding-bottom:20px padding-left:20px the new method:
padding:20px 如果上下值和左右值分别一样呢: the old method:
margin-top:0px margin-right:20px margin-bottom:0px margin-left:20px the new method
margin:0px 20px // top and bottom right and left respectively border的属性设置: the old method
border-width:thin border-style:solid boder-color black the new method:
border:thin solid black //width style color respectively border的属性设置更加灵活多变:
pythonanywhere是一个免费的托管python的代码,可以测试你的web应用,用起来还是比较方便的,现在就来介绍如何在pythonanywhere部署你的应用。 下载你的代码 我的代码是托管在github,我们首先从github下代码:
git clone https://github.com/<username>/my-first-blog.git 产生一个virtualenv
cd my-first-blog // create virtualenv virtualenv myvenv // activate vitalness . myvenv/bin/activate 数据库什么的我就不说了,pythonanywhere支持两种数据库,另外由于django本身就是支持sqlite数据库的,所以这里我们就不说了。 这里讲一下如何发布你的应用: 在他那个dashboard里面的vitualenv里面设置路径: /home//my-first-blog/myvenv/. 配置wsgi文件:
import os import sys path = '/home/<your-username>/my-first-blog' # use your own username here if path not in sys.path: sys.path.append(path) os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.core.wsgi import get_wsgi_application from whitenoise.django import DjangoWhiteNoise application = DjangoWhiteNoise(get_wsgi_application()) ok,可以访问你的网站了,网站地址:http://neal1991.pythonanywhere.com。那个是我的用户名,你可以设置成你自己的用户名。
经常在购物网站,看到那种图片轮滑的效果,所以看到有人实现了,所以我也就学习下了。 首先贴出html代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>document</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="javascript.js"></script> </head> <body> <div id="flash"> <ul id="pic"> <li style="display:block"><img src=""></li> <li><img src="" ></li> <li><img src=""></li> <li><img src=""></li> <li><img src=""></li> <li><img src=""></li> </ul> <ol id="num"> <li class="activate">1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ol> <a href="javascript:;" class="arrow" id="left"><</a> <a href="javascript:;" class="arrow" id="right">></a> </div> </body> </html> 图像的原路径我就不制定了,css文件
* { margin: 0; padding: 0; list-style: none; } a { text-decoration: none; color: #fff; } #flash { width: 730px; height: 454px; margin: 100px auto; position: relative; cursor: pointer; } #pic li { position: absolute; top: 0; left: 0; z-index: 1; display: none; } #num { position: absolute; left: 40%; bottom: 10px; z-index: 2; cursor:default; } #num li { float: left; width: 20px; height: 20px; border-radius: 50%; background: #666; margin: 3px; line-height: 20px; text-align: center; color: #fff; cursor: pointer; } #num li.active { background: #f00; } .arrow{ height: 60px; width: 30px; line-height: 60px; text-align: center; display: block; position: absolute; top:45%; background-color: rgba(0,0,0,0.3); z-index: 3; display: none; } .arrow:hover { background: rgba(0,0,0,0.7); } #flash:hover .arrow { display: block; } #left { left: 2%; } #right { right: 2%; } js代码:
定义一个空的类型,里面没有任何成员函数和成员变量,对该类型求sizeof,得到的结果是多少? 答案是1。空类型中的实例中不包含任何信息,本来求sizeof应该是0,但是当我们声明该类型的实例的时候,他必须在内存中占有一定的空间,否则无法使用这些实例。至于占用多少内存,由编译器决定。Visual Studio中每个空类型的实例占用1字节的空间。
如果在该类型中添加一个构造函数和析构函数,在对该类型求sizeof,得到的结果又是多少? 和前面一样,还是1.调用构造函数和析构函数只需要知道函数的地址即可,而这些函数的地址止于类型相关,而与类型的实例无关,编译器也不会因为这两个函数而在实例内添加任何额外的信息。
那如果把析构函数标记为虚函数呢? C++的编译器一旦发现一个类型中有虚函数,就会为该类型生成虚函数表,并在该类型中的每一个实例中添加一个指向虚函数表的指针。在32位的机器上,一个指针占4字节的空间,因此求sizeof是4;如果是64位的机器,那么结果就是8.
分析下面代码运行结果:
class A { private: int value; public: A(int n) { value = n; } A(A other) { value = other.value; } void print() { std::cout<<value<<endl; } }; int main() { A a = 10; A b = a; b.print(); } 在上述代码中,复制构造函数A(A other)传入的参数是A的一个实例。由于是传值参数,我们把形参复制到实参会调用复制构造函数。因此如果允许复制构造函数传值,就会在复制构造函数内调用复制构造函数,就会形成永无止境的递归调用从而导致栈溢出。因此C++的标准不允许复制构造函数传递参数,因此会编译出错。要解决这个问题,我们可以把构造函数修改为A(const A&other),也就是把传值参数改为常量引用。
C++中可以使用struct和class来定义类型,这两种类型有什么区别? 如果没有标明成员函数或者成员变量的访问权限级别,在struct中默认的是public,而在class中默认的是private。 那么在C#中呢? C#和C++不一样。在C#中如果没有标明成员函数或者成员变量的访问权限,struct和class都是private。struct和class的区别是struct定义的是值类型,值类型的实例在栈上分配内存;而class定义的是引用类型,引用类型的实例是在堆上分配内存的。
Matlab的调试总体分为,直接调试和间接调试。 1.直接调试: (1)去掉句末的分号; (2)单独调试一个函数:将第一行的函数声明注释掉,并定义输入量,以脚本方式执行 M 文件; (3)适当地方添加输出变量值的语句; (4)添加keyboard命令;
2.工具调试: 1.)以命令行为主的调试: (1)设置断点: dbstop in mfile:在文件名为mfile的M文件第一个可执行语句前设断点; dbstop in mfile at lineno:在mfile的第lineno行设断点; dbstop in mfile at subfun:当程序执行到子程序subfun时,暂时中止执行,并设断点; dbstop if error:遇到错误时,终止M文件运行,并停在错误行(不包括try…catch语句中检测到的的错误,不能在错误后重新开始运行); dbstop if all error:遇到任何类型错误均停止(包括try…catch语句中检测到的的错误); dbstop if warning:程序可恢复运行; dbstop if caught error:当try…catch检测到运行时间错误是,停止M文件执行,可恢复运行; dbstop if naninf 或 dbstop if infnan (2)断点清除: dbclear all:清除所有M文件中的所有断点; dbclear all in mfile:清除文件名为mfile的文件中的所有断点; dbclear in mfile:清除文件名为mfile中第一个可执行语句前的断点; dbclear in mfile at lineno: dbclear in mfile at subfun: dbclear if error/warning/naninf/infnan: (3)恢复运行: dbcount:从断点处恢复程序的执行,直到下一个断点或错误后返回Matlab基本工作空间; (4)调用堆栈: dbstack: 1.)dbstack(N) 2.)dbstack(’-completenames’) (5)列出所有断点: dbstatus s=dbstatus:返回值为M×1结构体 其中字段: name-函数名; line-断点行向量; expression_r-与line中相对应的断点条件表达字符串; cond-条件字符串,如error,caught error,warning,或naninf; identifier-当条件字符串是error,caught error,warning,或naninf时,改字段是Matlab的 信息指示字符串; dbstatus mfile:列车制定M文件中所有断点设置,mfile必须为M文件函数或有效路径;
柯西分布的概率密度函数是: t是location parameter,s是scale parameter.当t=0以及s=1的时候称为标准柯西分布,标准柯西分布的密度函数是: 下面的是标准柯西分布概率密度函数分布图:
接着上一节的内容来说。我们将继续关注与上一节制作的polls应用以及Django自动产生额度管理网站。
产生一个管理员用户 首先我们需要产生一个管理员用户,运行如下命令; python manage.py createsuperuser 下面会让你输入用户名,邮箱以及用户密码,按照要求填写就可以了,这样我们就产生了一个管理员账户了。
开发服务器 Django的管理员网站是默认激活的,我们可以通过上节讲到的方式激活服务器: python manage.py runserver 现在打开浏览器,输入http://localhost:8000/admin/你就可进入管理员登录界面了,输入用户名和密码就可以登录了。
进入管理员网站 当你以超级管理员的身份进去管理员网站,你就可以看到管理员的默认界面了。
在管理员中修改poll应用 在默认管理员界面中我们看不到我们的poll应用。我们需要高速管理员Question对象具有一个管理员接口,打开polls/admin.py
from django.contrid import admin from .models import Question admin.site.register(Question) 定制管理员表单 现在我们来开始定制管理员表单,打卡polls/admin.py
from django.contrib import admin from .models import Question,Choice //Register your models here. class ChoiceInline(admin.TabularInline): model = Choice extra = 3 class QuestionAdmin(admin.ModelAdmin): #fields = ['pub_date','question_text'] list_display = ('question_text','pub_date','was_published_recently') list_filter = ['pub_date'] search_fields = ['question_text'] fieldsets = [ (None,{'fields':['question_text']}), ('Date information',{'fields':['pub_date'],'classes':['collapse']}), ] inlines = [ChoiceInline] admin.site.register(Question,QuestionAdmin) 这里面他做了很多细节的改变,他是一个个的加进去,好麻烦,我这给的就是最终的一个版本,主要里面增加一个收缩的功能。